# Exercise problems

## Block I

1. Write a program that identifies every number between two limits that is divisible by 7.
[ Solution ]
2. Write a program to convert Celsium ⇔ Fahrenheit. Try different method for passing data to the script:
• Prompting input on the shell.
• Accepting arguments passed during script calling.
• Reading data from a file.
[ Solution ]
3. Calculate and print the Fibonacci series: F0 = 1, F1 = 1, Fn = Fn−1 + Fn−2
[ Solution ]
4. Write a proram which determines whether a given year is a leap year or not.
• A year is a leap year, if it divisible by 4 unless
• it is divisible by 100. In this case it is not a leap year, unless
• it is divisible by 400.
[ Solution ]
5. Define yourself a list containing several elements (different value!). Find an elegant way of deleting a specific element from the list. Tip: index().
[ Solution ]

## Block II

1. List Comprehensions
• Many programming language know the concept of stacks. Python has its own equivalent (Queue), but write your own version of a stack. It should emit exeptions, if it’s empty or full.
[ Solution ]
• In yesterday’s exercise 4 we determined whether a year was a leap year. Try to accomplish the same task using list comprehensions.
[ Solution ]
• When working with large data, e.g. larger than 10000 elements, one should consider a less memory consuming alternative: Generator Expressions. Try to solve the last exercise using generator expressions.
[ Solution ]
2. Hamster – Write a script containing a hamster and a board object. The hamster shall move in a pattern and avoid obstacles on the board. Finally the board should have means to display the hamster’s motion.
[ Solution ] [ Video ]
3. Polynome – Define a class for polynomials. One shall be able to define, derive, integrate and do math with polynomials. Do not use tools from the module numpy.
[ Solution ]
4. Rational numbers – Design a class for rational numbers allowing the four arithmetics. This class shall also be able to return the reduced fraction or a float. Remember:
• A fraction can be fully reduced by dividing numerator and dominator by the greatest common divisor.
• Rational numbers are presentated as a fraction p/q, where p ∈ Z and q ∈ N\0.
[ Solution ]

## Block III

1. Threading– Write a threaded script for retrieving (or generating), processing (e.g. apply a function) and saving values. But do not forget:
• Some operations, e.g. I/O, are not "thread-safe". Make sure nothing can go wrong.
• It can be annoying, if one tries to stop a threaded program, but it continues to run. Make sure the user can abort the program at any point.
[ Solution]
2. Calculator – Write a calculator GUI using Tk. The user shall be able to choose a function name (RadioButtons?) and enter a value, which shall be passed to the function, in a separate window (TopLevel?). The shall be closed, if the value is OK, and the result shall be displayed in the initial window.
[ Solution ]
3. Write a simple Tic-Tac-Toe game using Tk. But do not forget to save important data, e.g. which field has been hit by which player!
[ Solution ]
4. Write a script for managing a phone book. Write a GUI for managing entries and export/import to/from a file or database.
[ Solution ]
5. Racetrack – Write a threaded script for simulating a horse race. Horses shall be independently moved forward and the progress shall be displayed in a GUI, where also the winner shall be announced.
[ Solution ]

## Block IV

• Arrays – Getting used to
1. Create a 3 × 3-array with arbitrary values not equal 0 and resize it to a 9 × 6 array.
2. Assign a m × n-slice (m, n != 0, 1, m != n) of the array to a variable.
3. Set all elements of the slice equal 3/2 pi.
4. Define an array with length 100 for each property:
• Distance between elements is logarithmic.
• The array is empty.
5. Resize your 9 × 6 array to a 4 × 13 array and to a 1-dim. array.
6. We have seen select and piecewise. What is the difference between them and where?
[ Solution ]
• Matrices
1. Convert one of your 2-dim. arrays into a matrix.
2. Create another matrix (same shape) using MatLab syntax.
3. Perform addition and multiplication with those matrices. Convert matrices to arrays and repeat.
4. Solve the linear matrix equation:
[ Solution ]
• Polynomials
1. Write a script for determining the roots and extrema – both x- and y- values – of a polynomial. Use e.g. this polynomial:
f(x) = 1/3*x**5 − 2*x**2 − 6pi*x − 10
[ Solution ]
2. Write a script for reading (x, y)-pairs from a file and fitting with a polynomial p. Determine the followin integral:

Highlight the integration limits using a patch.
[ Solution ]
[ Solution ]
• Integral and Differential
1. Write your own functions for integrating and deriving 1-dimensional arrays. Compare your results with the results of the SciPy tools.
2. Expand your previous script such it can derive 2-dimensional arrays: