Programming with Python

Stefan Güttel, guettel.com

Lab classes: Functions


Reminder: In most of the upcoming problems you will be asked to write functions with their names being indicated in the problem. It is very important to use exactly the names provided (for example, myfunction()) as you will be able to reuse your functions in future exercises and also because our in-class tests are automatically graded. If you do not use the exact name as required (for example using myFunction()) your function will not be executable and you will lose marks. Also, when asked to write functions, make it a habit to write a function main() where all the testing is done. This main() function should define meaningful input parameters, call the functions, and output the result for testing.

Here is a simple example where we have been asked to write functions myfunction1() and myfunction2() and we are testing both of them using a main() function:</font>

def myfunction1(a, b):
   """
   Takes two input arguments a and b and returns their sum.
   """
   s = a + b
   return s

def myfunction2(a, b):
   """
   Takes two input arguments a and b and returns their product.
   """
   s = a * b
   return s

def main():
   a = 5
   b = 3
   print(myfunction1(a, b)) # hopefully outputs 8
   print(myfunction2(a, b)) # hopefully outputs 15

main()


Problem 1. Write a function main() that loads a natural number n and then n integers. The function has to compute the sum of the digits of each of these numbers, and then the product of these sums. In the end, the function has to print the sum of the digits of the computed product.

More formally, if $\operatorname{sd}(x)$ denotes the sum of the digits of $x$, and $x_1,x_2,\dots,x_n$ denote the loaded numbers, then the desired output is $$\operatorname{sd}\left(\prod_{k=1}^n \operatorname{sd}(x_k)\right).$$ Your main() should make use of a function sd(x) which returns the sum of the digits of its input argument x.

Problem 2. Write a function sdn takes integer arguments n and r. If $r \le 0$, it returns $n$. If $r > 0$, it returns $$\operatorname{sd}^r(n) := (\underbrace{\operatorname{sd} \circ \operatorname{sd} \circ \cdots \circ \operatorname{sd}}_{r})(n) = \underbrace{\operatorname{sd}(\operatorname{sd}(\dots\operatorname{sd}}_{r}(n)\dots)),$$ where $\operatorname{sd}(n)$ denotes the sum of the digits of $n$.

If r is undefined (i.e., if the function is called with only one argument), the function returns sd(n, r) for the largest r for which it makes sense. In this case, explain your stopping criteria in detail.

Write a main() function that will test all the possible inputs.

Problem 3. Write a function get_digit(n, left, right). The function returns

  • the d-th digit of n from the left if the function was called as get_digit(n, left=d). If the required digit doesn't exist, the function must return -1.
  • the d-th digit of n from the right if the function was called as get_digit(n, right=d). If the required digit doesn't exist, the function must return 0.
  • -1 otherwise, i.e., for calls get_digit(n) and get_digit(n, left=d1, right=d2).

Problem 4. Write a function rev(n) that returns the value of the integer argument n with its digits order reversed ($1719 \mapsto 9171$). The sign of the number must remain unchanged ($-1719 \mapsto -9171$).

Write a function main() that loads a natural number n and then n integers. The function has to print which of the numbers (first, second, etc.; you can write $k$-th as number #k) has the largest reversed value.

Hint: We know how to take the last digit $d$ out of a number $x$. However, we can add that digit to the end of a new number $y$ by computing $y = 10y + d$. Doing so for all digits will produce the reversed number.