Week 3: Exercises

This is a set of optional programming tasks that you should be able to implement in Python.

1. AEIOU

Write a program that reads a string on a single line and prints "vowels" if the string contains any vowels, otherwise "no vowels".

2. ASCII or Unicode

Read a string and print either 'ascii' if the string contains only ASCII characters, 'unicode' otherwise.

3. Capitalizing Words

Write a program that reads a string and capitalizes all words in the string. For this exercise, words are sequences of characters separated by one or more spaces. (Do not use the built-in method title(), which does the same thing.)

Sample input:

  one    fine     day

Output:

  One    Fine     Day

4. Lottery Ticket

Write a program that chooses 5 random numbers from the range 1..25, and prints them all. No two of the numbers may be the same. The program must choose any possible set of 5 numbers with equal probability.

5. Letter Histogram

Write a program that reads a series of input lines and determines how many times each letter A-Z appears in the input. You should ignore case, considering 'a' and 'A' to be the same letter. Ignore any characters that are not letters from the Latin alphabet. The input text is guaranteed to contain at least one letter.

The program should print the most frequent letter with a count of its occurrences. It should also print a histogram showing each letter's frequency as a fraction of all input letters, rounded up to the nearest percent. For example, if 3.7% of letters are N, the program should print 'n: ****'.

Sample input:

The quick fox jumped over the lazy dog.
Then the dog got up and jumped over the fox.

Sample output:

Most frequent letter: e (9)

a: *
b: 
c: *
d: **
e: ***
f: *
g: *
h: **

…

6. Simulated Rumor

Write a program that reads an integer N, representing a number of people. Simulate a rumor that spreads among these people as follows. A random person starts the rumor. They tell it to another person selected at random, who tells it to someone else, and so on, until someone hears the rumor who has already heard it before. At that point the rumor stops circulating. The program should produce output like this:

Enter N: 100
Person 41 heard the rumor.
Person 83 heard the rumor.
Person 22 heard the rumor.
…
Person 83 heard the rumor again.
14 people heard the rumor in all.

7. Casino

Suppose that I walk into a casino in Prague and I have 100 Kc. I repeatedly play a game in which I will either win 9 Kc or lose 10 Kc, with an equal chance of either outcome. My goal is to double my money: if it increases to 200 Kc, I will leave happy. On the other hand, if I run out of money I will also have to leave.

  1. Write a program that simulates my visit to the casino. At each step, it should print how much money I currently have. At the end, it should report the outcome and the number of games that I played.

  2. Expand the program so that it reads an integer N and simulates N visits to the casino. The program should print out the fraction of visits that were successful (i.e. in which I doubled my money), as well as the average number of games that were played.

8. Random Walk

A robot begins at square (0, 0) on an infinite two-dimensional grid. At each step, it randomly walks up, down, left, or right. Will it ever return to its starting square?

Write a program that simulates this situation. The program should read an integer N and perform N simulations. In each simulation, if the robot returns to (0, 0) we will say that it has won. If it ever reaches a square (x, y) where |x| + |y| >= 50, it has wandered too far, and has lost. The program should print out the fraction of simulations in which the robot won, as well as the average length of each simulation.