Programming with Python

Stefan Güttel, guettel.com

Exercises: Loops and Conditionals

Here are some problems to be solved as an exercise. While you are welcome to use anything that comes in standard Python distributions, you are also strongly encouraged to use only what we have learned so far. Overcomplicating can often lead to errors or too slow programs.

In this problem sheet we provide some test data for you to test your programs with. In future, these will not be provided. You should always analyze the problems at hand and find some data for testing. A few random values are always a good choice, as well as those from the boundary cases (highly dependent on the problem itself).

Creative thinking and/or Googling might be required to solve some of these problems. However, none of them require knowlege of any material beyond what we have already covered. Please ask for help if you fail to solve problems on your own!

Notes for the problems below

"Odd/even digits" are determined by their value, not their position. For example, the odd digits of number $8417$ are $1$ and $7$, and the desired sum is $1+7 = 8$. The same goes for the even digits.

Be careful with the digits. If a number can be negative, you need to get rid of the sign. Otherwise, you will get wrong results. For example, -17 % 10 == 3 while 17 % 3 == 7.

Problems

Problem 1. Write a program that loads one integer and prints the sum of its odd digits.
The output has to be in the form

The sum of the odd digits in number $n$ is $d$.

Test data:

  • Input: $-131723$
  • Output: The sum of the odd digits in number -131723 is 15.
    ($1+3+1+7+3 = 15$)

Problem 2. Write a program that loads integers until a zero is loaded and then prints the maximum among them and which one it was (not taking the zero into the account). If there is more than one maximum, use the first one among them as a solution.

Test data:

  • Input: $-19, -17, -23, -17, 0$
  • Output: The maximum number is #2 and its value is -17.

Be careful about the case when only a zero is loaded.

Problem 2a. Do the same, but in case that there is more than one maximum, use the last one among them.

Hint: Observe the "<" (or ">") sign that you have used in Problem 2. All you need to do in this problem is change it a bit.

Test data:

  • Input: $-19, -17, -23, -17, 0$
  • Output: The maximum number is #4 and its value is -17.

Be careful about the boundary case when only a zero is loaded.

Problem 3. Write a program that loads a natural number (a non-negative integer) n and then loads n integers. The program has to print the sum of the maximum digits of all the loaded numbers. If a number has multiple maximum digits, they are still taken into the sum only once ($1777$ only adds $7$ to the sum, not $7+7+7$).

Test data:

  • Input: $3, -1719, 1777, 111$ ($3$ means that we're loading $3$ numbers)
  • Output: 17
    ($9+7+1 = 17$)

Problem 4. Solve the previous problem by taking multiple maximum digits into account as many times as they appear ($1777$ adds $21 = 7+7+7$ to the sum).

Hints:

  • Distinguish the cases d > md, d == md and d < md (where d is the currently observed digit and md is currently the biggest one). In one case, increase a counter which will be used to keep track of how many times this maximum has appeared in the number; in another case, reset that counter and save the new maximum (in the third case, nothing is done with these two). Once you're done with a number, you should know the value of its largest digit, as well as the number of times it appeared in that number.
    Be careful with where in the code you are resetting both the maximum digit (this hint also applies to Problem 3) and the counter of that digit's occurrences in the number.

  • Alternatively, you can first find the maximum digit md and then go again through the number and count all digits equal to md.

Test data:

  • Input: $3, -1719, 1777, 111$
  • Output: 33
    ($1 \cdot 9 + 3 \cdot 7 + 3 \cdot 1 = 33$)

Problem 5. Write a program that inputs integers until it gets a zero and prints the one with the maximum sum of its odd digits. If there is more than one such number, print the first one.

Explanation:

Read the problem text carefully, because it may be a bit confusing at first sight.

What we want is to load some numbers, then:

  1. For each of those numbers compute the sum of its odd digits.

  2. Find the maximum among these sums and print the corresponding number.
    Be careful not to print that sum nor the absolute value of a number, instead of a number itself.

Rewriting the problem in steps like above can help you focus on parts of a problem in order to solve it properly.

Hints:

  • Remember that the algorithm from the lectures removes digits from a number, so using that number as a terminating condition of the loop has to be done carefully. One way to do so is with the auxiliary variable (tmp in the code in the lectures), but - given the nature of the problem - this might be more convenient

    n = input("The first number: ")
      while n != 0:
          # do what needs to be done #
          n = input("The next number: ")
    

    If you are familiar with break (which the lectures will cover later), you might want to use it for a cleaner solution.

  • You will need a variable to store the maximum and the sum. Be careful where you initialize these!

  • The desired output is a number, not its sum of odd digits! When finding a new maximum, you will need to remember both the number (for the output) and the sum of its odd digits (for future comparisons).

Test data:

  • Input: $13$, $-17$, $888$, $616$, $0$
  • Output: $-17$
    (the required sums are: $4$, $8$, $0$, $1$, so $17$ has the biggest sum of odd digits)