Assigning Teams Randomly on Jython - jython

I need to write a function that randomly assigns names I put in a parameter to teams I put in another parameter. The function name looks like: def random(team,name)
and it should come out like this:
def random(["blue","russia","hat"],["bob","sue","kim"])
bob is on the russia team
sue is on the hat team
kim is on the blue team

At first do not use random for function name. It is standard Python module name and you should read about this module. In random module you can find choice() function which you can use like this:
def print_random_team(teams, names):
for name in names:
team = random.choice(teams)
print('%s is on the %s team' % (name, team))
print_random_team(["blue","russia","hat"],["bob","sue","kim"])

Related

How to find a specific word within a phrase?

I wish to know how to find a specific word within a phrase. I am trying to find the word "Pizza" within a set of keywords, however there is no keyword that only has "Pizza". There are keywords such as "Pizza Delivery" and "Pizza Delivery Boy", however they won't show up! How can I do this?
Desired output:
MOVIE KEYWORD
----------------------------------- ----------------------------------
Drive Angry Waitress
Taken France
Saving Private Ryan France
30 Minutes or Less Pizza Delivery
30 Minutes or Less Pizza Delivery Boy
My script:
SELECT MovieTitle AS "MOVIE", KEYWORDDESC AS "KEYWORD"
FROM TBLMOVIE
JOIN TBLKEYWORDDETAIL ON TBLMOVIE.MOVIEID = TBLKEYWORDDETAIL.MOVIEID
JOIN TBLKEYWORD ON TBLKEYWORDDETAIL.KEYWORDID = TBLKEYWORD.KEYWORDID
WHERE TBLKEYWORD.KEYWORDDESC IN ('France', 'Waitress', 'Pizza');
My output:
MOVIE KEYWORD
----------------------------------- ----------------------------------
Drive Angry Waitress
Taken France
Saving Private Ryan France
One method uses LIKE:
WHERE TBLKEYWORD.KEYWORDDESC LIKE '%France%' OR
TBLKEYWORD.KEYWORDDESC LIKE '%Waitress%' OR
TBLKEYWORD.KEYWORDDESC LIKE '%Pizza%'
Another method uses REGEXP_LIKE():
WHERE REGEXP_LIKE(TBLKEYWORD.KEYWORDDESC, 'France|Waitress|Pizza')
If you use REGEXP_LIKE() you should spend a little bit of time learning about regular expressions and how to use them.

How do you query only part of the data in the row of a column - Microsoft SQL Server

I have a column called NAME, I have 2000 rows in that column that are filled with people's full names, e.g. ANN SMITH. How do I do a query that will list all the people whose first name is ANN? There are about 20 different names whose first name is ANN but the surname is different.
I tried
and (NAME = 'ANN')
but it returned zero results.
I have to enter the FULL name and (NAME = 'ANN SMITH') ANN SMITH to even get a result .
I just want to list all the people with there first name as ANN
Try in your where clause:
Where Name like 'ANN %'
Should work mate.
ANN% will find all results where ANN is first then anything after.
%ANN% will find the 3 letters ANN in any part of that rows field.
Hope it helps
Also usually Name is separated into First names and second name columns.
this will save Having to use wild cards in your SQL and provide A bit more normalized data.
SELECT NAME
FROM NAMES
WHERE NAME LIKE 'ANN %'
This should wildcard select anything that begins with 'ANN' followed by a space.

SSIS - export from SQL table to multiple flat files based on first column values

Here is an example of what the SQL Table looks like:
Name Class Grade
Jesse English A
Jesse Math C
Jesse History A
Scott Math B
Scott History B
Scott English A
Mike History A
Mike English D
I am trying to get SSIS to dynamically create a flat file for each person. Example:
Flat File name: Jesse
Name Class Grade
Jesse English A
Jesse Math C
Jesse History A
Flat File name: Scott
Name Class Grade
Scott Math B
Scott History B
Scott English A
Flat File name: Mike
Name Class Grade
Mike History A
Mike English D
I can easily create a static link between the sql table and the flat file but I plan on adding a lot of people to the table which would otherwise cause me to create a data flow task for each. This would not be ideal. I was hoping for a for each loop that identified the distinct values within the Name column and then output the qualified rows into a flat file.
This is how your package should look like :
right click->variable 1.student-->object
2.students-->string(for holding all names of students giving you required) result
In the data flow -- connection managers--right click-- flat file connection-->properties-->expression mention it as something like this ::"C:\\Users\\user\\Desktop\\ssis_stuff_from_stackoverflow\\citys.txt-"+ #[User::student] +".txt"
the package succesfully executes affecting 3 rows and adding 3 flat files in the folder
path
here is one good example
First run a query to find a recordset of unique students:
select distinct name from myTable
Then use the foreach loop to loop though and run the following parameterized query:
SELECT class, grade
FROM myTable
WHERE name = ?
Use a derived column to include the name to the resultset.
Put this in a flat file destination. The connectionstring for the output file will be dynamic.
These are the steps. If you get stuck, there are plenty of examples online, or feel free to ask.

Get matching names from SQL Server

My problem is, I have a database of people names and their achievements. Now, I have some paragraphs which contains the person names. I need to extract the names from those paragraphs. The web-end of the application will append a hyperlink of the extracted names with their activities.
Data in my database might look like:
Name | Achievement
----------------------------------------
Steve Jobs | Founder of Apple
Bill Gates | Founder of Microsoft
Now I have string like: After saving up some money, Steve Jobs took off for India in the search of enlightenment.
I need to find Steve Jobs from the above string and add to hyper link to that.
Any idea how to do this?
SQL Server really isn't the best place to do this, because a hyperlink is by definition HTML, but...
declare #s varchar(500) = 'After saving up some money, Steve Jobs took off for India in the search of enlightenment.'
select
#s = REPLACE(#s, name, ''+name+'')
from yourtable
where #s like '%'+name+'%'
select #s
You should save your data in some pre defined format for example, you can use *** at the start & end of person name like.
After saving up some money, ***Steve Jobs*** took off for India in the search of enlightenment.
Then you can extract the Person Names by finding *** through sql
EDIT:
I myself will try to find some other way to do it. For example i will save the Person Names in separate columns.

Getting distinct rows based on a certain field from a database in Django

I need to construct a query in Django, and I'm wondering if this is somehow possible (it may be really obvious but I'm missing it...).
I have a normal query Model.objects.filter(x=True)[:5] which can return results like this:
FirstName LastName Country
Bob Jones UK
Bill Thompson UK
David Smith USA
I need to only grab rows which are distinct based on the Country field, something like Model.objects.filter(x=True).distinct('Country')[:5] would be ideal but that's not possible with Django.
The rows I want the query to grab ultimately are:
FirstName LastName Country
Bob Jones UK
David Smith USA
I also need the query to use the same ordering as set in the model's Meta class (ie. I can't override the ordering in any way).
How would I go about doing this?
Thanks a lot.
I haven't tested this, but it seems to me a dict should do the job, although ordering could be off then:
d = {}
for x in Model.objects.all():
d[x.country] = x
records_with_distinct_countries = d.values()
countries = [f.country in Model.objects.all()]
for c in countries:
try:
print Model.objects.filter(country=c)
except Model.DoesNotExist:
pass
I think that #skrobul is on the right track, but a little bit off.
I don't think you'll be able to do this with a single query, because the distinct() method adds the SELECT DISTINCT modifier to the query, which acts on the entire row. You'll likely have to create a list of countries and then return limited QuerySets based on iterating that list.
Something like this:
maxrows = 5
countries = set([x.country for x in Model.objects.all()])
rows = []
count = 0
for c in countries:
if count >= maxrows:
break
try:
rows.append(Model.objects.filter(country=c)[0])
except Model.DoesNotExist:
pass
count += 1
This is a very generic example, but it gives the intended result.
Can you post the raw SQL that returns what you want from the source database? I have a hunch that the actual problem here is the query/data structure...