In [1]:
for n in range(70, 80):
print(n)
In [2]:
for n in range(1, 11):
print(n**2)
In [3]:
for n in range(19, 9, -1):
print(n)
In [4]:
n = input()
n = int(n)
In [5]:
N = int(input())
for i in range(1, N+1):
print(i)
In [6]:
N = int(input())
for i in range(1, N+1):
print(i*i)
In [7]:
s = 0
n = int(input())
while n != 0:
s += n
n = int(input())
print(s)
In [8]:
cnt = 0
s = 0
sq_sum = 0
n = int(input())
while n != 0:
s += n
cnt += 1
sq_sum += (n**2)
n = int(input())
sq_sum /= cnt
s /= cnt
print(s, (sq_sum - s**2) ** 0.5)
In [9]:
l = [2, 3, -2, -7, 0, 2, 3]
for e in l:
if e % 2 == 0:
print(e)
In [10]:
numbers = []
n = int(input())
while n != 0:
numbers.append(n)
n = int(input())
print(" ".join(str(n) for n in numbers[::-1]))
In [11]:
N = int(input())
numbers = []
for _ in range(N):
n = int(input())
numbers.append(n)
print(" ".join(str(n) for n in numbers[::-1]))
In [12]:
def is_prime(N):
if N < 2:
return False
maxn = int(N**0.5)
for n in range(2, maxn+1):
if N % n == 0:
return False
return True
def get_n_primes(N):
primes = []
n = 2
while len(primes) < N:
if is_prime(n):
primes.append(n)
n += 1
return primes
assert(get_n_primes(3) == [2, 3, 5])
In [13]:
def get_nth_fibonacci(N):
if N < 3:
return 1
return get_nth_fibonacci(N-1) + get_nth_fibonacci(N-2)
def get_nth_fibonacci_solution2(N):
if N < 3:
return 1
prev = 1
prevprev = 1
for _ in range(2, N):
f = prev + prevprev
prevprev = prev
prev = f
return f
assert(get_nth_fibonacci(3) == 2)
In [14]:
def get_n_fibonacci(N):
if N < 2:
return [1]
fib_l = [1, 1]
while len(fib_l) < N:
fib_l.append(fib_l[-1] + fib_l[-2])
return fib_l
assert(get_n_fibonacci(4) == [1, 1, 2, 3])
In [15]:
def get_range_fibonacci(A, B):
assert B >= A
fibs = get_n_fibonacci(B-1)
return fibs[A-1:B-1]
assert(get_range_fibonacci(2, 5) == [1, 2, 3])
In [16]:
def sum_list(l, start=0):
s = start
for e in l:
s += e
return s
assert(sum_list([1, 2, 3]) == 6)
assert(sum_list([1, 2, 3], 5) == 11)
In [17]:
def arithmetic(x, y, operation="+"):
if operation == "+":
return x + y
if operation == "-":
return x - y
if operation == "*":
return x * y
if operation == "/":
return x / y
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 [18]:
def call_func(x, y, func=None):
if func is None:
return x + y
return func(x, y)
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 [19]:
def filter_list(input_list, predicate):
output_list = []
for e in input_list:
if predicate(e):
output_list.append(e)
return output_list
def is_prime(N):
if N < 2:
return False
maxn = int(N**0.5)
for n in range(2, maxn+1):
if N % n == 0:
return False
return True
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 [20]:
def reduce(l, acc_func, accumulator=None):
if len(l) < 1:
return accumulator
if len(l) == 1 and accumulator is None:
return l[0]
if accumulator is None:
accumulator = acc_func(l[0], l[1])
return reduce(l[2:], acc_func, accumulator)
return reduce(l[1:], acc_func, acc_func(accumulator, l[0]))
def add(x, y):
return x + y
l1 = [1, 2, -1, 5]
assert(reduce(l1, add) == 7)
assert(reduce(l1, add, 10) == 17)
In [21]:
def string_len_add(acc, s):
return acc + len(s)
l2 = ["foo", "bar", "hello"]
assert(reduce(l2, string_len_add, 0) == 11)
In [22]:
l = [1, 2, -1, -4, -3]
# odd elements
print("Number of odd elements:", reduce(l, lambda x, y: x + int(y % 2 == 0), 0))
# maximum
print("Maximum:", reduce(l, lambda x, y: max(x, y)))
l = ["apple", "plum", "", "avocado", "pear"]
print("Longest string:", reduce(l, lambda x, y: x if len(x) > len(y) else y))
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])