Institute of Computer Science
  1. Courses
  2. 2020/21 fall
  3. Introduction to Programming II (MTAT.03.256)
ET
Log in

Introduction to Programming II 2020/21 fall

  • Home
  • Grading
  • Links

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:

Example test

Example programming exercises

At the end of the session go to Moodle and submit all programs written during the session.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment