Programming 1, 2020-21
Week 6: Exercises

1. Same Values

Write a function same_values(l, m) that returns True if the lists l and m contain the same values, i.e. every element of l is in m and every element of m is in l. You may assume that all list elements are immutable.

2. Most Common Word

Write a program that reads text until the end of input is reached, and prints out the most common word in the input text, along with its number of occurrences.

3. Most Common Characters

Write a program that reads a string and print out the five most common characters in the string along with their occurrence counts, e.g.

e 23

t 20

o 18

n 15

i 14

Write the characters in decreasing order of frequency. The string may contain any Unicode characters.

4. Point Class

Write a class Point representing a point in 2 dimensions. The class should support these operations:

5. Line Class

Write a class Line representing a line segment in 2 dimensions. The class should support these operations:

6. Rectangle Class

Write a class Rectangle representing a rectangle in 2 dimensions. The class should support these operations:

7. Circle Class

Write a class Circle representing a circle in 2 dimensions. The class should support these operations:

8. Recursive Orange

Write a recursive function orange(n) that prints the word 'orange' n times.

9. No Zeroes

Write a function no_zeroes(n) that takes an integer n and returns an integer formed by removing all zeroes from the end of n's decimal representation. For example:

As a special case, no_zeroes(0) = 0.

You may not use any loops in your solution, so you will need to write the function recursively.

10. Recursive Power

Write a recursive function isPowOfTwo(n) that returns True if n is a power of 2.

11. Recursive Sum

Write a function sum(a, i, j) that computes the sum of the values in the range a[i:j]. You may not use any loops or call the built-in sum() function, so you will need to use recursion.

12. Recursive Max

Write a function max(a, i, j) that computes the maximum value in the range a[i:j]. You may not use any loops or call the built-in max() function, so you will need to use recursion.

13. Tower of Hanoi

The Tower of Hanoi is a well-known puzzle that looks like this:

The puzzle has 3 pegs and a number of disks of various sizes. The player may move disks from peg to peg, but a larger disk may never rest atop a smaller one. Traditionally all disks begin on the leftmost peg, and the goal is to move them to the rightmost.

Write a program that reads an integer N and prints a solution to the Tower of Hanoi with N disks. Assume that pegs are numbered from 1 to 3, and all disks begin on peg 1 and must move to disk 3. For example:

How many disks? 2
move disk 1 from peg 1 to peg 2
move disk 2 from peg 1 to peg 3
move disk 1 from peg 2 to peg 3

14. Euler #1, revisited

Here is Project Euler's problem 1:

Find the sum of all the multiples of 3 or 5 below 1000.

Solve this problem in one line of Python using a list comprehension.

15. Sum of Squares and Square of Sum

Solve Project Euler's problem 6 in one line of Python using list comprehensions.