Download 1. Simple Calculator. Let`s build a simple calculator with Python! The

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
1. Simple Calculator. Let’s build a simple calculator with Python! The program prompts
the user to input the first operand, the operator (one of +, -, * and /), followed by the second
operand. If the input is correct — the operator the user typed in is one of +, -, * or /, then the
program outputs the result and ask for another set of input, otherwise it displays “invalid input”
and terminates.
2. Nim. Two players, Alice and Bob, want to play the following variant of Nim. They start
with a pile of n stones and take turns taking away stones from the pile. The limitation is that they
must either take 1 stone or 2 stones. Alice starts. The first player that cannot move loses.
Write a program so that they can play without having to collect a large number of stones first.
The program lets the user input how many stones to start with. Then it asks players how many
stones they want to take. If the number is not 1 or 2, the program would ask again. When there is
no stone left, the program displays who won/lost and terminates.
Remark. There is a slightly more complicated version where they start with 3 piles, and each
time choose one pile to remove stones from. There is, however, no limit on the number of stones
they can remove. Feel free to implement this variant if you find this version more interesting.
3. Month starting on Sunday. We know that January 1, 2012 is a Sunday. Write a program to
find the next 10 months that starts on a Sunday.
4. Collatz. Start with an integer n that the user inputs. If n is even, divide it by 2; if n is odd,
replace it with 3n + 1. Repeat until you get 1. Print all the intermediate steps.
Example: n = 17.
Output: 17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
Hint. One tricky thing is when we divide an integer we get a float. Be careful when displaying.
5. Sum of longest ascending subsequence. The user inputs a sequence of positive numbers (the
input terminates whenever user inputs 0 or something negative). Output the sum of the longest
ascending subsequence.
Example: 1 2 3 4 5 6 4 10 20 -1. (The numbers are given one at a time, on separate lines)
Output: 21.
The subsequence 4 10 20 has larger sum but is shorter.
Hint. Keep track of the length of the longest sequence seen so far as well as its sum. Also keep
track of the length and sum of the current sequence. When we get a new number, we need to decide
if it can extend the current ascending sequence. What else do we need to keep in order to decide
this?
6. Factoring. Input n. Output its factorization.
Example: n = 252.
Output:
2ˆ2
3ˆ2
7
Hint. Start by trying to divide n by 2. And then 3, and 4, and 5, . . .
7. Integer Rotating. The user inputs n. Consider the seven-segment font of n. What number
do we get when we rotate the whole thing 180◦ ?
Rephrase of the task. If there is 3, 4 or 7 in the number n, display an error; otherwise reverse
the order of the digits, and change 6 to 9 and 9 to 6.
Example: n = 168.
Output: 891.
1