I am learning basic python programing on Kaggle and here is a question that I don't understand its answer.
Problem
We're using lists to record people who attended our party and what order they arrived in
party_attendees = ['Adela', 'Fleda', 'Owen', 'May', 'Mona', 'Gilbert', 'Ford']
A guest is considered 'fashionably late' if they arrived after at least half of the party's guests. However, they must not be the very last guest
Answer
def fashionably_late(arrivals, name):
order = arrivals.index(name)
return order >= len(arrivals) / 2 and order != len(arrivals) - 1
would you please explain the answer
Please everytime use one example to understand how any function works.
Here we take one example,
Attendees = ['Adela', 'Fleda', 'Owen', 'May', 'Mona', 'Gilbert', 'Ford']
Name = Mona
We check Mona is fashionably late or not
fashionably_late(arrivals,name) --> when function called we pass name as Mona and arrivals is list of attendees
order = arrivals.index(name) --> it return index of Mona here is '4'
return order >= len(arrivals) / 2 and order != len(arrivals) - 1
Order >= len(arrivals) / 2 --> len() returns length of list and /2 is devide by 2 as fashionably late is who come after half of attendees here 7/2
So it returns true as mona's index is greater than 7/2
order != len(arrivals) - 1 --> this condition check Mona is very last guest or not here it return true as Mona is not last guest
and --> 'true and true' return true so finally function returns true means Mona is fashionably late.
party = ['Adela', 'Fleda', 'Owen', 'May', 'Mona', 'Gilbert', 'Ford']
name = 'Mona'
index = party.index(name)
def f(x):
if index >= len(party)/2 and index != len(party)-1:
return True
else:
return False
f(x)
Mona is at index 4 in the list. [0,1,2...]
The length of the list is 7.
index 4 >= 7/2 and 4 is not last in the list (index 6 is last 7-1)
Therefore Mona returns True.
Only the names Mona and Gilbert will return as True.
return ((arrivals.index(name)+1)>round(len(arrivals)/2) and arrivals[-1] != name)
Related
What's the best way to write this?
I'm running through a couple of scenarios with a CASE WHEN statement. When matched on both and school then return 1. If there is no match, then match on name and school = NULL and return 2. If still no match then return 0.
ie:
CASE WHEN (x.name = x.name and x.school = y.school) THEN 1
WHEN (x.name = x.name and x.school = compare and resolve to null if not matched) THEN 2
ELSE 0
Problem 1: How to write line 2. -- I think I have this resolved.
CASE WHEN (x.name = x.name and x.school = y.school) THEN 1
WHEN (x.name = x.name and y.school is null) THEN 2
ELSE 0
Problem 2: Once I figure out how to write the comparison for line 2, the true resolve of branch 2 will need to pick the lowest tier of the 2 null school values. In the image below, say there is no match on name and school, so the logic moves on to branch 2 comparing (name and school is null). In this case, there are two possibilities for the combination - tier 2 and tier 3.
How do I write a THEN argument to pick the lowest tier of the twe for branch 2?
Is this what you want?
case
when x.name = y.name and x.school = y.school then 1
when x.name = y.name and x.school is null and y.school is null then 2
else 0
end
The first branch succeeds if both name and school match. The second matches on names that are equal and both schools having null values.
I am using sql server management studio 2012 and have to make a query to show which subject a student has failed(condition for failing is point<5.0) the most for the first time from this table
StudentID | SubjectID | First/Second_Time | Point.
1 | 02 | 1 | 5.0
2 | 04 | 2 | 7.0
3 | 03 | 2 | 9
... etc
Here are my teacher's query:
SELECT SubjectID
FROM Result(NAME OF the TABLE)
WHERE [First/Second_Time] = 1 AND Point < 5
GROUP BY SubjectID
HAVING count(point) >= ALL
(
SELECT count(point)
FROM Result
WHERE [First/Second_Time] = 1 AND point < 5
GROUP BY SubjectID
)
I don't understand the reason for making the having query. Because Count(point) is always >=all(select count(point)
from Result
where First/Second_Time=1 and point<5
group by SubjectID), isnt it ?
and it doesn't show that the subject has most student fail for the first time. Thanks in advance and sorry for my bad english
The subquery is returning a list of the number of times a subject was failed (on the first attempt). It might be easier for you to see what it's doing if you run it like this:
SELECT SubjectID, count(point)
FROM Result
WHERE [First/Second_Time] = 1 AND point < 5
GROUP BY SubjectID
So if someone failed math twice and science once, the subquery would return:
2
1
You want to know which subject was failed the most (in this case, which subject was failed 2 or more times, since that is the highest number of failures in your subquery). So you count again (also grouping by subject), and use having to return only subjects with 2 or more failures (greater than or equal to the highest value in your subquery).
SELECT SubjectID
FROM Result
WHERE [First/Second_Time] = 1 AND Point < 5
GROUP BY SubjectID
HAVING count(point)...
See https://msdn.microsoft.com/en-us/library/ms178543.aspx for more examples.
Sounds like you are working on a project for a class, so I'm not even sure I should answer this, but here goes. The question is why the having clause. Have you read the descriptions for having and all ?
All "Compares a scalar value with a single-column set of values".
The scalar value in this case is count(point) or the number of occurrences of a subject id with point less than 5. The single-column set in this case is a list of the number of occurrences of every subject that has less than 5 points.
The net result of the comparison is in the ">=". "All" will only evaluate to true if it is true for every value in the subquery. The subquery returns a set of counts of all subjects meeting the <5 and 1st time requirement. If you have three subjects that meet the <5 and 1st time criteria, and they have a frequency of 1,2,3 times respectively, then the main query will have three "having" results; 1,2,3. Each of the main query results has to be >= each of the subquery results for that main value to evaluate true. So going through step by step, First main value 1 is >= 1, but isn't >= 2 so 1 drops because the "having" is false. Second main value 2 is >=1, is >= 2, but is not >= 3 so it drops. Third value, 3, evaluates true as >= 1, 2, and 3, so you end up returning the subject with the highest frequency.
This is fairly clear in the "remarks" section of the MSDN discussion of "All" keyword, but not as relates to your specific application.
Remember, MSDN is our friend!
This one is driving me mad - I must be missing something very simple here but have been Googling for 2 hours and have got nowhere. I'm building a Rails app with Postgresql 9.5.1 that handles appointments for barbers. One of the columns is available - if the appointment is available to book or not - it's a boolean. I need to get the next 5 records for a given appointment ID.
select *
from appointments
where id > 13
and barber_id = 1
limit 5
This returns 5 records, but the available field is all true, even though records 14-18 exist with attributes of available = FALSE.
If I use the query
select * from appointments where id > 13 and barber_id = 1 and (available = false) limit 5
Then it returns only records with available = FALSE, but I want all records that match, whether TRUE or FALSE.
I have also tried
select * from appointments where id > 13 and barber_id = 1 and (available = false or available = true) limit 5
But this also only returns available = TRUE records.
Tearing my hair out here - what am I missing?! A default or something?
If your id's are not in sorted order, you might want to sort them and return
select * from appointments where id > 13 and barber_id = 1 and
(available = false or available = true) order by id asc limit 5
And your boolean thing can be completely removable if it is a not null column, since you are checking for both true and false :)
I want to create a computed column in SAS enterprise guide where it’ll show car items if it meets certain conditions.
There are 75 car items (T.TS_Items) such as windscreen, tyres, steering wheel etc.
Each item has a unique ID (T.TS_NUM) so T.TS_NUM =1 to T.TS_NUM =75.
And I want to then give each new car item a label such as labelNo_01 to labelNo_75.
So, if the vehicle type is Honda (T.TS_F_NUM = 2), type is hatchback(T.TS_TYPE = I) and T.TS_NUM =1 then the new column name is LabelName_01 with a item such as windscreen.
and if the vehicle type changes for example - the vehicle type is Toyota (T.TS_F_NUM = 1)
but the rest is the same it will give me the 75 car item for Toyota.
My code:
PROC SQL;
CREATE TABLE WORK.MotorVehicle AS
SELECT
T.TS_VEHICLE_RDES,
/* FI_Label_01 */
Case When
T.TS_F_NUM in (1,2) And
T.TS_TYPE = I And
T.TS_NUM =1 Then T.T_Item
else T.T_Item
End
AS FI_Label_01
FROM T.T_ITEM
WHERE T.TS_F_NUM = 41
ORDER BY T.TS_NUM ;
QUIT;
The above is simple and works but I am not sure how to add an Else or Else if statement. Basically when I use the above code and hard code the vehicle type to 1 (toyota) in the where statement its working and its gives me only Toyota but its also giving me all the types instead of just hatchback. I want to add a condition where its only giving the type hatchback but I dont know how or where in the above code it’ll go in.
Also a loop would help so I dont have to repeat the process for each vehicle type. Hope this information helps.
Some Data.
T.TS_F_NUM T.TS_TYPE T.T_Item T.TS_NUM
1 I windscreen 1
2 I side mirror 2
1 C Side mirror 3
2 C passenger door 4
1 I dashboard 5
2 I gear box 6
Yeah you're pretty close, I've got some similar code and mine looks like this and works :)
This obviously has sub sub rules but all the principles are the same for ELSE, I don't think there is an ELSE IF...
CASE WHEN
RULE_1 = TRUE THEN
CASE WHEN RULE_2 = TRUE AND RULE_3 = TRUE THEN
CASE WHEN
RULE_4 = TRUE
THEN
ITEM_1
ELSE
ITEM_2
END
ELSE
CASE WHEN RULE_5 THEN
ITEM_3
ELSE
CASE WHEN RULE_6 = TRUE THEN
ITEM_4
ELSE
ITEM_5
END
END
END
END
i'm faced with a situation, where i have to find the best matches for a users search request. I will provide an example (a little abstract):
We have a table with lawyers:
Name Location Royality Family Law Criminal Law
-------------------------------------------------------------
Lawyer A Berlin 100 €/hour false true
Lawyer B Amsterdam 150 €/hour true true
A user should now be able to search by several features. The weight of each feature should be some kind of parameter. In my case the table contains much more of such features (Location, Royality, 20+ boolean values). Of course the result should provide all "good" results but ordered by some kind of "score", so the best result appears at the top.
I'm not looking for a out of the box solution rather than some introduction to this topic.
Kind regards,
matt
A generic approach is to assign a weight to each item, and add them up when they match. This will cause a full table scan to score every single record.
Assuming inputs of Berlin, >100/hr, Criminal Law=true, family law = null (no criteria)
And Location match carries a weight of 5
select *
from (
select *,
case when location = 'berlin' then 5 else 0 end +
case when royality >= 100 then 1 else 0 end +
case when familylaw = null then 1 else 0 end +
case when criminallaw = true then 1 else 0 end as score
from tbl
) scored
order by score desc
You may be able to make use of SOUNDEX functions in your particular RDBMS. These compare two strings and give a numeric value for "how alike they sound".
You can then weight and/or sum the results for each column, as mentioned by Richard.