This is a collection of problems used by me to teach a young student programming in Python. The problems start from an absolute beginner level. The student in question is 12 years old. The problems are not programming language specific, but we are using Python. Upon a closer examination, Python has been a laungage of choice when working with young students.
Write a program that spells a word backwards. The program should be put in a file spell_backwards.py. The following is a sample input and output.
>>> spell_backwards
A word, please:
"skin"
Your word spelled backwards is: "niks"
Do you want to continue? (y/n)
"n"
Good bye!
>>>
Strings cannot be modified. See, for example, effbot.org/pyfaq/how-do-i-modify-a-string-in-place.htm. You need to create a new string, or use one of the methods discussed there.
Write a program palindrome.py checking whether a word is a palindrome. An example of a palindrome is the word "kayak". It spells the same, if it is read backwards. Here is a sample input and output:
>>> palindrome
Your word?
"kayak"
The word is a palindrome. Next word?
"arizona"
The word is not a palindrome. Next word, please!
"quit"
>>>
Roman soldiers used to use a simple code that allowed them to send secret messages over distances.
If the Roman alpabet has 26 letters, ordered as an alphabet ABCDEFGHIJKLMNOPQSTUVWXYZ, then an arbitrary word can be translated by rotating every letter by 13=26/2 places. Thus, A becomes N, B becomes O etc. Finally, letter Z is translated to M.
Write a function like this:
def rot13(word): ...
which returns the translated word, where word is an arbitrary string written with the 26-letter alphabet. For instance, rot13("NICK") is "AVPX". Note that rot13("AVPX") yields "NICK".
If you have a prhase, only the letters are subject to rotation, while the spaces remain fixed.
You will need to work with the remainder. This is how Python computes the remainder from dividing 14 by 4:
>>> 14%4
2
That is, you use "%" instead of "%". The "/" is used to compute te quotient:
>>> 14/4
3
It is always true that n == (n/m)*m+(n%m). For instance:
>>> 14 == (14/4)*4+(14%4)
True
Write a program divisor.py which calculates the smallest divisor of a number. Here is a sample input and output:
>>> divisor
Your number:
15
Smallest divisor is 3.
>>>
Your friend gives you 33 dollars, and you are to give him an equivalent amount, but you have only 1-dollar and 5-dollar bills. What is the minumum amount of bills that you must give your friend? Write a program that will calculate the amount of 1-dollar and 5-dollar bills. For example:
>>> change
How much?
33
Change: 3 1-dollar bills, 6 5-dollar bills.
>>>
Solve the above problem when you yave a two-dollar bill instead of a one-dollar bill. Note that you cannot always change a whole number of dollars. However, 33=4*2+5*5, so you can change 33 dollars, using four two-dollar bills and five five-dollar bills.