In [ ]:
In [ ]:
In [ ]:
In [ ]:
n = input()
n = int(n)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
l = [2, 3, -2, -7, 0, 2, 3]
In [ ]:
In [ ]:
In [ ]:
def get_n_primes(N):
# TODO
assert(get_n_primes(3) == [2, 3, 5])
In [ ]:
def get_nth_fibonacci(N):
# TODO
assert(get_nth_fibonacci(3) == 2)
In [ ]:
def get_n_fibonacci(N):
# TODO
assert(get_n_fibonacci(4) == [1, 1, 2, 3])
In [ ]:
def get_range_fibonacci(A, B):
# TODO
assert(get_range_fibonacci(2, 5) == [1, 2, 3])
In [ ]:
# TODO def sum_list...
assert(sum_list([1, 2, 3]) == 6)
assert(sum_list([1, 2, 3], 5) == 11)
In [ ]:
# TODO def arithmetic...
assert(arithmetic(2, 3) == 5)
assert(arithmetic(2, 3, "-") == -1)
assert(arithmetic(2, 3, "*") == 6)
assert(arithmetic(2, 3, "/") == 2/3)
This is very similar to the previous function but the third parameter is a function instead of a string.
In [ ]:
# TODO def call_func(...)
def product(x, y):
return x * y
assert(call_func(3, 4, product) == 12)
assert(call_func("foo", "bar") == "foobar")
A predicate is a function that takes one element and return True
or False
, for example is_even
, is_prime
.
If you implemented The following tests should run.
In [ ]:
def filter_list(input_list, predicate):
# TODO
return output_list
def is_prime(n):
# TODO
pass
def is_odd(n):
return n % 2 == 1
l1 = [1, 2, 3, 4, 19, 35, 11]
assert(filter_list(l1, is_odd) == [1, 3, 19, 35, 11])
assert(filter_list(l1, is_prime) == [2, 3, 19, 11])
reduce([1, 2, 3], product) ---> 6
reduce([1, 2, 3], product, accumulator=10) ---> 60
reduce(["foo", "bar"], string_addition) ---> "foobar"
reduce(["foo", "bar"], string_addition, accumulator="hello") ---> "hellofoobar"
In [ ]:
def reduce(l, acc_func, accumulator=None):
# TODO
def add(x, y):
return x + y
l1 = [1, 2, -1, 5]
assert(reduce(l1, add) == 7)
assert(reduce(l1, add, 10) == 17)
In [ ]:
def string_len_add(acc, s):
return acc + len(s)
l2 = ["foo", "bar", "hello"]
assert(reduce(l2, string_len_add, 0) == 11)
In [ ]:
In [ ]:
def qsort(l):
# TODO
l = [10, -1, 2, 11, 0]
qsort(l)
assert(l == [-1, 0, 2, 10, 11])
In [ ]:
def bubblesort(l):
# TODO
l = [10, -1, 2, 11, 0]
bubblesort(l)
assert(l == [-1, 0, 2, 10, 11])