In this question, you'll write a method that takes a list of numbers [0-9] and returns a corresponding list with the "ordinal" versions. That is, if you see a 1 in the list, you'll create a string "1st". If you see a 3, you'll create a string "3rd", and so on.
For example, if you receive [2, 1, 4, 3, 4]
as input, you should create a list of strings that looks like this: ["2nd", "1st", "4th", "3rd", "4th"]
.
In [ ]:
def return_ordinals(numbers):
out_list = []
### BEGIN SOLUTION
### END SOLUTION
return out_list
In [ ]:
inlist = [5, 6, 1, 9, 5, 5, 3, 3, 9, 4]
outlist = ["5th", "6th", "1st", "9th", "5th", "5th", "3rd", "3rd", "9th", "4th"]
for y_true, y_pred in zip(outlist, return_ordinals(inlist)):
assert y_true == y_pred.lower()
In [ ]:
inlist = [7, 5, 6, 6, 3, 5, 1, 0, 5, 2]
outlist = ["7th", "5th", "6th", "6th", "3rd", "5th", "1st", "0th", "5th", "2nd"]
for y_true, y_pred in zip(outlist, return_ordinals(inlist)):
assert y_true == y_pred.lower()
In this question, you'll write code that computes the median of a sorted list of numbers. You can assume the list you receive is already sorted in ascending order (least to greatest).
If your input list is [1, 1, 2, 4, 7, 7, 8]
, then your output should be 4
. Recall the rule about median: if you get a list with an even number of elements, you should return the average of the two middle ones.
Store your answer in the variable med_num
.
In [ ]:
def median(numbers):
med_num = 0
### BEGIN SOLUTION
### END SOLUTION
return med_num
In [ ]:
inlist = [ 35.20575598, 45.05634995, 45.42573818, 55.07275661,
66.42501038, 66.48337884, 73.59004688, 81.09609177,
87.67779046, 93.90508029]
outmed = 66
assert outmed == int(median(inlist))
In [ ]:
inlist = [ 12899.59248764, 19792.31177415, 31156.00415682, 31764.93625914,
41443.07238461, 50669.10268086, 55408.34012113, 61352.47232585,
72682.91992934, 86883.37175784]
outmed = 46056
assert outmed == int(median(inlist))
In this question, you'll write code to find the mode of a list of numbers. Recall that the mode is the number that occurs most frequently. Ties can be broken arbitrarily (meaning you can pick whichever number among those tied for the most frequent).
If your input list is [5, 1, 3, 1, 2, 5, 1]
, you should return 1
.
Hint: try using a dictionary to maintain the number of times you've seen particular numbers.
Store your answer in the variable mode_num
.
In [ ]:
def mode(numbers):
mode_num = 0
### BEGIN SOLUTION
### END SOLUTION
return mode_num
In [ ]:
l1 = [5, 1, 3, 1, 2, 5, 1]
a1 = 1
assert mode(l1) == a1
In [ ]:
l2 = [1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 4, 4, 4, 4]
a2 = 4
assert mode(l2) == a2