Laboratory 01

You are expected to complete all basic exercises. Advanced exercises are prefixed with *. You are free to use any material (lecture, Stackoverflow etc.) except full solutions.

1. range() practice

1.1 Print the numbers between 70 and 79 inclusive.


In [ ]:

1.2 Print the first 10 square numbers starting from 1.


In [ ]:

*1.3 Print the numbers from 19 to 10 (backwards).


In [ ]:

2. Reading user input

User input can be read with the input() function. It always returns a string which can be converted to integer using the int() function.


In [ ]:
n = input()
n = int(n)

2.1 Read a number N and print the numbers from 1 to N.


In [ ]:

2.2 Read a number N and print the first N square numbers starting from 1.


In [ ]:

2.3 Read numbers until 0 and print their sum.


In [ ]:

*2.4 Read numbers until 0 and print their mean and standard deviation without using a list.


In [ ]:

3. Lists

3.1 Print the odd numbers from this list.


In [ ]:
l = [2, 3, -2, -7, 0, 2, 3]

3.2 Read numbers until 0 and collect them in a list. Print the elements backwards (zero not included).

For example:

input: 3 4 2 12 3 0
output: 3 12 3 4 3

In [ ]:

3.3 Read a number N and then read exactly N numbers. Print them backwards.

For example:

input: 4 -1 0 2 12
output: 12 2 0 -1

In [ ]:

4. Functions

The following exercises contain function skeletons. Finish these functions. If you work correctly, the assert statements should not fail.

4.1 Write a function that takes an integer parameter (N) and returns a list of the first N prime numbers starting from 2.


In [ ]:
def get_n_primes(N):
    # TODO

assert(get_n_primes(3) == [2, 3, 5])

4.2 Write a function that takes an integer parameter (N) and returns the Nth Fibonacci number.

The Fibonacci series starts as 1, 1, 2, 3, 5, 8, so the 2nd number is 1.


In [ ]:
def get_nth_fibonacci(N):
    # TODO

assert(get_nth_fibonacci(3) == 2)

4.3 Write a function that takes an integer parameter (N) and returns a list of the first N Fibonacci numbers.

You are encouraged to reuse previous functions.


In [ ]:
def get_n_fibonacci(N):
    # TODO
    
assert(get_n_fibonacci(4) == [1, 1, 2, 3])

*4.4 Write a function that takes two integers, A and B and returns B-A Fibonacci numbers starting from the Ath Fibonacci number to the (B-1)th number. See the example below.


In [ ]:
def get_range_fibonacci(A, B):
    # TODO

assert(get_range_fibonacci(2, 5) == [1, 2, 3])

5. Default and keyword arguments

5.1 Write a function that computes the sum of a list with an optional starting value. If no starting value is provided, it should sum from 0.


In [ ]:
# TODO def sum_list...

assert(sum_list([1, 2, 3]) == 6)
assert(sum_list([1, 2, 3], 5) == 11)

5.2 Write a function that takes two numbers and an arithmetic operator as a string ("+", "-", "*" or "/") and returns the result of the arithmetic operation on the two numbers. The default operation should be addition.


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)

*5.3 Write a function that takes three parameters. The third is a callable (function) that takes two parameters. The function should call its third parameter with the first two as parameters. If the third parameter is not specified the function should add the two arguments.

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")

6. Extra exercises

*6.1 Create a function that take a list and a predicate (function with boolean return value) as parameters and returns a new list of those elements which the predicate return True.

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])

*6.2 Reduce is a function that applies a two argument function against an accumulator and each element in the sequence (from left to right) to reduce it to a single value. If no initial value is provided, the accumulator is initialized with the return value of the function run on the first two elements of the sequence.

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)

*6.3 Use your reduce function for the following operations:

  1. count the number of odd elements in a list of integers,
  2. find the maximum of a list of integers,
  3. find the longest string in list of strings.

Test your solutions.


In [ ]:

*6.4 Implement qsort. Qsort sorts a list in place using the quicksort algorithm.


In [ ]:
def qsort(l):
    # TODO
    
l = [10, -1, 2, 11, 0]

qsort(l)

assert(l == [-1, 0, 2, 10, 11])

*6.5 Implement bubble sort (in place).


In [ ]:
def bubblesort(l):
    # TODO
    
l = [10, -1, 2, 11, 0]

bubblesort(l)

assert(l == [-1, 0, 2, 10, 11])