Given a list of numbers, return L1-normalized data (each data point divided by the sum of the absolute value of all the data points: $ \frac{x_i}{\sum_j |x_j|} $) and the normalization factor.
def l1_norm(nums):
sum = 0
for num in nums:
sum += abs(num) # note: NOT allowed in HW!
return sum
def l1_normalize(nums):
norm = l1_norm(nums)
result = []
for num in nums:
result.append(num / norm)
return result, norm
print(l1_normalize([60, 20, 20]))
print(l1_normalize([1, 2, 3, 4, 5]))
Given a list and a function, return the subset of the input list for which the function returns true (i.e. filter
).
def is_vowel(c):
return c in ('a', 'e', 'i', 'o', 'u')
def is_even(num):
return num % 2 == 0
def keep_if(pred, l):
result = []
for element in l:
if pred(element):
result.append(element)
return result
print(keep_if(is_even, [1, 2, 3, 4, 5]))
print(keep_if(is_vowel, 'hello there!'))
Given a function, return a function that does the opposite!
def invert_pred(p):
def f(x):
return not p(x)
return f
print(keep_if(invert_pred(is_even), [1, 2, 3, 4, 5]))
print(keep_if(invert_pred(is_vowel), 'hello there!'))
Given a list of strings, return a list of those strings that have the most characters (i.e. max
).
def longest(words):
if len(words) == 0:
return []
first_word = words[0]
big_len = len(first_word)
result = [first_word]
for i in range(1, len(words)):
word = words[i]
word_len = len(word)
if word_len == big_len:
result.append(word)
elif word_len > big_len:
result = [word]
big_len = word_len
return result
print(longest(['a', 'b', '']))
print(longest(['hello', 'howdy', 'hi', 'yo']))
Given a numeric grade and a rubric (sorted list, from smallest to greatest grade), output resulting grade.
PF = (
(0, 'Fail'),
(60, 'Pass')
)
ABCDF = (
(0, 'F'),
(60, 'D'),
(70, 'C'),
(80, 'B'),
(90, 'A'),
)
def number_to_grade(rubric, numeric):
grade = rubric[0][1]
for i in range(1, len(rubric)):
if numeric >= rubric[i][0]:
grade = rubric[i][1]
return grade
print(number_to_grade(PF, 50))
print(number_to_grade(ABCDF, 50))
print(number_to_grade(PF, 60))
print(number_to_grade(ABCDF, 60))
print(number_to_grade(PF, 75))
print(number_to_grade(ABCDF, 75))
print(number_to_grade(PF, 93))
print(number_to_grade(ABCDF, 93))