Programming with Python

Stefan Güttel, guettel.com

Test 1

Problem 1. Write a program that loads an integer n and a natural number k. The program has to verify that k > 0 and, if it is so, print the k-th digit of n from the right; if k <= 0, a descriptive error must be reported with no further computation nor outputs.

If n has less than k digits, the program should print 0 (a zero).

For example, for $n = 1234567$ and $k = 3$, the program has to print the digit $5$.

Problem 2. One generalization of Fibonacci numbers is defined as follows: $$F_0 = 0, \quad F_1 = 1, \quad F_{k+2} = pF_{k+1} + F_{k}, \quad k \in \mathbb{N}_0, \quad p \in \mathbb{R}.$$ Write a program that loads $p \in \mathbb{R}$ and two natural numbers $a$ and $b$. The program has to print all numbers $F_k$ for $k$ between $a$ and $b$, including the two of them.

Note: The program has to work properly regardless of the order of $a$ and $b$, i.e.,

  • when $a < b$ it should print $F_a, F_{a+1}, \dots, F_b$,
  • when $a = b$ it should print $F_a$,
  • when $a > b$ it should print $F_b, F_{b+1}, \dots, F_a$.

Swapping a and b is allowed.