{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programming with Python\n", "#### Stefan Güttel, [guettel.com](http://guettel.com)\n", "#### (based on course notes written by Vedran Šego, [vsego.org](http://vsego.org/))\n", "\n", "## Exercises: Loops and Conditionals\n", "\n", "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.\n", "\n", "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 analize the problems at hand and find some data for testing. A few random inputs are always a good choice, as well as those from the boundary cases (highly dependent on the problem itself).\n", "\n", "Some 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. Ask for help if you fail to solve them on your own!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notes for the problems below\n", "\n", "\"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.\n", "\n", "Be careful with the digits. If a number can be negative, you need to lose the sign. Otherwise, you will get wrong results. For example, -17 % 10 == 3 while 17 % 3 == 7." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 1.** Write a program that loads one integer and prints the sum of its odd digits. \n", "The output has to be in the form\n", "\n", "> The sum of the odd digits in number $n$ is $d$.\n", "\n", "**Test data:**\n", "\n", "* Input: $-131723$\n", "* Output: The sum of the odd digits in number -131723 is 15. \n", " ($1+3+1+7+3 = 15$)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 2.** Write a program that loads integers until a zero is loaded and then prints the maximum among them and which one was it (not taking the zero into the account). If there is more than one maximum, use the first one among them as a solution.\n", "\n", "**Test data:**\n", "\n", "* Input: $-19, -17, -23, -17, 0$\n", "* Output: The maximum number is #2 and its value is -17.\n", "\n", "Be careful about the case when only a zero is loaded.\n", "\n", "\n", "\n", "**Problem 2a.** Do the same, but in case that there is more than one maximum, use the **last** one among them.\n", "\n", "**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.\n", "\n", "**Test data:**\n", "\n", "* Input: $-19, -17, -23, -17, 0$\n", "* Output: The maximum number is #4 and its value is -17.\n", "\n", "Be careful about the case when only a zero is loaded." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**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$).\n", "\n", "**Test data:**\n", "\n", "* Input: $3, -1719, 1777, 111$ ($3$ means that we're loading $3$ numbers)\n", "* Output: 17 \n", " ($9+7+1 = 17$)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**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).\n", "\n", "**Hints**:\n", "\n", "* 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 has this maximum 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. \n", " 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.\n", " \n", "* Alternatively, you can first find the maximum digit md and then go again through the number and count all digits equal to md.\n", "\n", "**Test data:**\n", "\n", "* Input: $3, -1719, 1777, 111$\n", "* Output: 33 \n", " ($1 \\cdot 9 + 3 \\cdot 7 + 3 \\cdot 1 = 33$)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**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.\n", "\n", "**Explanation:**\n", "\n", "Read the problem text carefully, because it may be a bit confusing.\n", "\n", "What we want is to load some numbers, then:\n", "\n", "1. For **each** of those numbers compute the sum of its odd digits.\n", "\n", "2. Find maximum **among these sums** and print the corresponding **number**. \n", " Be careful **not** to print that sum nor the absolute value of a number, instead of a number itself.\n", "\n", "Rewriting the problem in steps like these above can help you focus on parts of a problem in order to solve it properly.\n", "\n", "**Hints:**\n", "\n", "* 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", "python\n", " n = input(\"The first number: \")\n", " while n != 0:\n", " # do what needs to be done #\n", " n = input(\"The next number: \")\n", "\n", " If you are familiar with break (which the lectures will cover later), you might want to use it for a cleaner solution.\n", "\n", "* You will need a variable to store the maximum and the sum. Be careful where you initialize these!\n", "\n", "* 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).\n", "\n", "**Test data:**\n", "\n", "* Input: $13$, $-17$, $888$, $616$, $0$\n", "* Output: $-17$ \n", " (the required sums are: $4$, $8$, $0$, $1$, so $17$ has the biggest sum of odd digits)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }