Mobile Universe

Programming for Android and iOS

Exercises

Welcome to the Exercises page. Here I'll put all kinds of exercises for practice. There will mostly be stuff I've already done myself so if there are any difficulties I could reply relatively quickly. The problems are not that hard to solve once a good foundation of algorithms is built. Remember that to be a good programmer it takes more than just solving examples in the books, you have to constantly try and learn new things in order to be able to find a way out of every situation.

The Exercises

  1. Write a program that reads in an integer representing a Celsius value and writes it out as a Fahrenheit value. If the Celsius value is C, then the Fahrenheit value, F, is calculated as follows: F = ( 9.0 / 5.0 ) * C + 32. [ Hint: recall the raw_input() and input() functions for user input. ]

  2. Write a program that reads in two numbers from the user and writes out which number divides exactly into the other, if either do. [ Hint: first write a function divisorOneWay( a, b ) that takes two integer parameters and returns True if the second parameter divides exactly into the first, and False otherwise, second write a related function divisor( a, b ) that returns True if either number divides exactly into the other, and False otherwise. ] Example input and output is:

  • Input 6 12  Output: 6 divides exactly into 12
  • Input 12 6  Output: 6 divides exactly into 12
  • Input 4 7    Output: Neither divides into the other

  3. Write a program that reads in a sequence of positive numbers typed by the user. You should stop reading numbers when the user types -1. a) Write out how many numbers were typed in by the user. b) Calculate the sum of the numbers typed in by the user, and write it out. c) Find the average of the numbers and write it out. d) Find and write out the largest number. e) Do the same with the smallest number. e) Find out how many times the number 39 was entered and write this out.

  4. Here's a small program to illustrate how to use Tkinter and its components in a GUI program - example.py. Here's a screenshot of a program. The lines on the screen look like curved lines, but in fact they are all straight lines. Work out how the lines are related to each other. What is the difference in terms of start/end points between adjacent lines? Write a program to draw this figure.

  5. A palindrome is a word/sentence reading the same from left to right or right to left. "civic" is an example of a single word that is a palindrome.

  1. Write a function wordPalindrome that takes as a parameter a single word in all lower-case letters and returns True if it is a palindrome, False otherwise.
  2. Write a second function sentencePalindrome that tests to see if a whole sentence passed in as a parameter is a palindrome. In this case spaces and punctuation should be ignored, and upper-case letters should be treated as lower-case letters. So "A man, a plan, a canal: Panama." is a valid sentence palindrome. [Hint: write a helper function remove_non_letter( s ) that takes a string as a parameter and returns a new string containing only the letters extracted from s.] 

  6. Write a function join that takes two lists and returns a list in which each element is a list of two elements, one from each of the given lists. For example:

  • join( [ 1, 2, 3 ], [ "a", "b", "c" ] )   returns [ [ 1, "a" ], [ 2, "b" ], [ 3, "c" ] ] 

  7. Define a function frequency which is given a string and returns the number of occurrences of each letter in the string, ignoring differences between upper and lower case and ignoring non-letter characters. For example:

  • frequency( "This is a test." )   should return [ [ 'a', 1 ], [ 'b', 0 ], [ 'c', 0 ], [ 'd', 0 ], ... ]

  8. Define a function wordFrequency which is given a string and returns a dictionary with strings as keys and numbers as values, showing how many times each word occurs in the original string. The function should ignore difference between upper and lower case letters as well as non-letter characters. For example:

  • wordFrequency( "The first test of the function." )  returns { "the":2, "first":1, "test":1, ... } 

  9. Birthday book project. The idea is to store people's birthdays and produce reminders of birthdays that are coming up. A birthday consists of a month and a date, which can be represented by a dictionary:

  • { "month":"Sep", "day":17 }

The birthday book is a dictionary in which the keys are people's names and the values are birthdays, represented as dictionaries as above. All the functions that have to be implemented take a birthday book as a parameter:

  • Write some code to set up a birthday book with several people and their birthdays.
  • Define a function which, given a person's name, prints his or her birthday.
  • Define a function which, given a month, prints a list of all people who have birthdays in that month, sorted in alphabetic order, with the dates.
  • More challenging: write a function which, given a month and a date, prints a list of all people who have birthdays within the next week, with the dates. Don't forget that some of these birthdays moght be in the next month, and if the given date is in December, some of them might be in January.

  10. Write out the 12 times table, e.g.

  • 1 times 12 = 12
  • 2 times 12 = 24
  • ...
  • 12 times 12 = 144 

  11. Write a function raise that takes two parameters, x and y, and returns the value generated by raising x to the power of y. Do not use Python's ** operator - work out how to generate the answer using a loop.

  12. Read in a sequence of integers from the user, ending in -1. These integers represent altitude readings while going on a walk, taken every minute. Your program should calculate how many hill-tops have been encountered on the walk. For example:

  • for:     1   3   2   2   4   6   7   5   -1           2 hilltops should be reported

  13. Using the Tkinter module, write a function grid taking two parameters n and m to draw a grid with n boxes accross by m boxes high. Hence grid(5, 4) would produce a grid of 4 rows with each row having 5 squares.

  14. Write a similar function to grid, only this time a chessboard-pattern should be created, of alternating white and black squares.

  15. Create the effect of a coin spinning on its edge. This will be an animation with a sequence of ovals appearing with changing shapes. Use trigonometry to work out how the oval should be each time you draw it. Remember that the canvas method .create_line(x1, y1, x2, y2) draws an oval inside the bounding rectangle defined by the parameters.

  16. Using Tkinter, write a program that reads in an integer from the user, and then writes it out using Tkinter functions in seven-segment format - like in a digital display, where each digit is made up of some of the seven segments of an '8' shape, drawn using 7 short lines.