During session 5
Exercises
NB! The practical session exercises are provided in such capacity that more experienced students would also find them interesting. It is not expected of you to get all of them done during one session. However, you are always welcome to complete the unfinished tasks at home!
1. Fractal
The following image contains examples of the fractal of order 0, 1, 2, 3 and 4. Write a recursive function, which takes two arguments: the order of the fractal and the length of a side. The function has to draw the fractal of the corresponding order. Demonstrate the work of the function drawing the fractal of order 4.
2. Cesaro fractal
Upgrade the previous program so that it draws the following fractal:
Hint: you can use a loop and the recursive function defined in previous exercise.
3. Recursive exponentiation
Write a recursive function that takes two numbers as its arguments and returns the first number raised into the power of the second number. It is not allowed to use loops, operator ** or the built-in function math.pow().
Hint: exponentiation corresponds to the repeated multiplication of the base.
4. Recursive number of elements
Write a recursive function number_of_elements that takes a recursive data structure (a nested list) and returns the number of all the elements (count all elements in all lists). It is not allowed to use the built-in function len.
Examples of the function call:
>>> number_of_elements([1, [2, 3], [[[[4, 5], 6]]], 7, 8]) 8 >>> number_of_elements([[1, [2, [3, 4], 5], [6, 7, 8]], [9, 10]]) 10 >>> number_of_elements([1, 2, [3, 4, 5, 6], 7, [8], 9]) 9 >>> number_of_elements([1, 2, 3]) 3
Sample exam exercises
Check out some examples of the test and the programming exercises: