Week 6: Exercises

1. Rectangle Class

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

2. Circle Class

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

3. Vector Class

Write a class Vector that represents a vector in n-dimensional space.

4. Date Class

Write a class Date representing a month and day. The class should support these operations:

5. Matrix Sum

Write a function that computes the sum of two matrices A and B, represented as lists of lists. Assert that the matrices have the same dimensions.

6. Matrix Product

Write a function that computes the product of two matrices A and B, represented as lists of lists. Assert that the matrices have dimensions that are compatible for multiplication.

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

8. Cycle

Write a function cycle that takes a list of length N representing a permutation of the integers 0, …, N – 1. The function should return True if the permutation is a single cycle. For example: