Week 3: Exercises

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

1. Double Or Nothing

Read a string and print 'double' if any two adjacent characters are the same, or 'nothing' otherwise.

Enter string: abacuses
nothing
===
Enter string: lionesses
double

2. 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

3. Password Generator

Write a program that generates a random password with 10 lowercase letters. The password should contain alternating consonants and vowels.

Sample outputs:

  kimolonapo
  ritilenora

4. String Comparison

Write a program that reads two string S and T, each on its own line. The program should print the string that is lexicographically greater, i.e. the string that appears later in dictionary order. Do not use the built-in string comparison operator '>'.

Enter S: limerick
Enter T: limeade
limerick

5. Starts With

Write a function startswith(s, t) that takes strings s and t and returns True if s starts with t. Do not use the built-in method of the same name.

6. Contains

Write a function contains that takes two strings S and T, and returns true if S contains T. For example, contains('key lime pie', 'lime') should return true. Do not use the in operator.

7. Consecutive Sum

Write a program that reads two integers N and T, each on its own line, followed by a line containing a series of integers, separated by whitespace. The program should determine whether any consecutive series of N values in the series have the sum T. If so, it should write those values to the output, separated by spaces. If there is no such series, write 'none'.

8. 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: **

…

9. Closest Points

Write a program that reads a series of points, one per line. Each point will consist of N floating-point coordinates separated by spaces, for some N ≥ 2. It is guaranteed that all points will have the same number of coordinates. Determine which two points are closest to each other. Write those points, along with the distance rounded to 1 decimal point. For example, if the input is

0.0 0.0
4.0 3.0
20.0 20.0

then the output will be

Points (0.0, 0.0) and (4.0, 3.0) are closest, with distance 5.0

10. 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.

11. 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.

12. 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.

13. Largest Palindrome Product

Solve Project Euler's problem 4:

A palindromic number reads the same both ways. Find the largest palindrome made from the product of two 3-digit numbers.

14. Smallest Multiple

Solve Project Euler's problem 5:

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

15. Sum Square Difference

Solve Project Euler's problem 6:

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

16. 10001st prime

Solve Project Euler's problem 7:

What is the 10 001st prime number?