Week 3: Exercises

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

1. Square

Read a number N, then print an N x N square of asterisks as in the example below.

Enter N: 5
*****
*   *
*   *
*   *
*****

2. Triangle

Read a number N, then print an N x N triangle of asterisks as in the example below.

Enter N: 6
     *
    **
   ***
  ****
 *****
******

3. AEIOU

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

4. ASCII or Unicode

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

5. Spaces In Between

Read a string and write it out with a space between each adjacent pair of characters.

Enter string: waffle
w a f f l e

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

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

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

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

10. All Upper

Write a program that reads a string of ASCII characters and makes all lowercase letters uppercase without using the library function upper.

Sample input:

100 Spires and Towers

Sample output:

100 SPIRES AND TOWERS

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

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

13. Pi

  1. Imagine a circle of radius 1 inscribed in a square whose sides have length 2. Suppose that I throw a dart and it lands in a random position in the square (uniformly distributed with respect to area). What is the probability that the dart will land in the circle?

  2. Write a problem that simulates 10,000 such dart throws, and uses the result to compute an estimated value of pi.

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

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

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

17. 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?

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