## CS 313 - Homework 10 - Problem collection

#### Due: Monday, 5/15, at midnight

This is the last programming assignment. You are encouraged to work on it in groups of two, but you can also work by yourself. Below is a list of 12+1 programming problems, of which you have to solve (at least) two, according to the following rules:
• You may use any language discussed in this course, except the procedural languages (Pascal, C, and Python*). That leaves Smalltalk, Ruby, Scheme, ML, and Prolog. (*Note: If you really like Python, check out problem 12.)

• The language you use for each problem has to belong to a different paradigm (OO, functional, logic). For example, if you solve one problem using Scheme, you will have to use Smalltalk, Ruby, or Prolog for the other problem (not Scheme or ML). If you solve three problems, you need to use all three paradigms.

• I have rated the difficulty of the problems listed below on a scale from 1 (not so hard) to 3 (hard). For full credit, the sum of the difficulties for all problems you solve has to be at least 4 (e.g., 1+3, or 2+2, or even 1+1+2). Note that the rating of difficulty is my own (subjective) estimate - your own rating may be different :)

• In case you can't solve any of the harder problems, you may opt to solve easier (or fewer) problems for a grade penalty. This will likely result in a higher grade than submitting a program that doesn't work. If you achieve a sum of difficulties of 3, the penalty is 15%; for a sum of 2 it is 30%.

• You are not allowed to consult books or the web for solutions for these problems!
Some problems will require language features not discussed in class. For example, to write a Tic-Tac-Toe player, you will need to ask the user for input. You will have to consult the on-line documentation to figure out how to do such things.

#### The problems

1. Write a program that takes a description of a directed graph as input, and reports whether the graph has cycles. The description of the graph should be a list of lists, where there is one sublist for each node in the graph. The first element in each sublist should be the name (or number) of the node, and the remaining elements should be the destinations of the edges leaving this node. For example, the list
```      [[a, b], [b, c], [c, a]]
```
describes a cyclic graph with three nodes a, b, c, and three edges (a, b), (b, c), and (c, d). The list
```      [[1, 2, 4, 3], [2, 3, 4], [3], [4, 3]]
```
describes an acyclic graph with four nodes 1, 2, 3, 4, and six edges (1, 2), (1, 4), (1, 3), (2, 3), (2, 4), and (4, 3).
Difficulty: 1

2. Write a program that, given interval [lo, hi], prints the numbers as English words in alphabetical order. For instance, for the interval [8, 15] the output should be
```eight
eleven
fifteen
fourteen
nine
ten
thirteen
twelve```
You can use this web page to check the spelling of numbers (use the American English version, but omit the commas). You can also do it in a different language if you wish. You may assume that 0 ≤ lo < hi ≤ 9999.
Difficulty: 1

3. Write a program to test whether two polygons intersect. The polygons are not necessarily convex. Each of the two polygons is represented with a list of its points in clockwise order. For example, given the three polygons
```      A = [[0 0] [2 1] [4 8] [4 0]]
B = [[6 5] [6 2] [0 2] [0 5]]
C = [[1 4] [2 4] [1 3]]
```
the only two that don't intersect are A and C (you might want to draw a picture of the three polygons). Hint: to test whether a point is contained in a polygon, count the number of times a ray starting at the point intersects an edge of the polygon. If this number is odd, the point is inside. Careful, this problem is trickier than it looks...
Difficulty: 1.5

4. Write a program to solve the Missionaries and Cannibals problem. The Story:
Three missionaries and three cannibals are traveling together through the jungle. Why are the cannibals traveling with the missionaries? Perhaps in the expectation of an easy meal. Their problem, there will be no easy meals unless they outnumber the missionaries.
They arrive at a river. There is a boat. The boat can carry at most two people. Both the missionaries and cannibals can operate the boat. Can the missionaries devise a series of boat trips that will transport the entire party across the river and where, on either bank, the missionaries will not be outnumbered and become an easy meal?
Your program should compute and output a sequence of boat trips that solves the problem.
Difficulty: 2

5. Write a program to play Tic-Tac-Toe against the user, using a text-based user interface
Difficulty: 2

6. Write a program to solve the 8-Queens problem, to place 8 queens on a chess board such that no queen attacks another.
Difficulty: 2

7. Write a program that solves the Peg Solitaire game (often found in Friendly's restaurant), either the triangular version with 15 spots (14 pegs and 1 hole), or the cross-shaped ("English") version with 33 spots (32 pegs and 1 hole) -- boards #4 and #6 in this image. The goal of the game is to remove all pegs except one, by jumping over each peg with another peg (which removes the peg that was jumped over). Note: you don't have to program a graphical user interface (or any user interface, for that matter). All your program should do is compute and then print out a solution for the puzzle.
Difficulty: 2

8. Write a program to solve Sudoku puzzles, either 6x6 or 9x9. Come up with a way to encode puzzles, e.g., a list of 36 or 81 numbers, with blanks represented by 0. You may search the web for downloadable puzzles, but not for puzzle solvers of course.
Difficulty: 3

9. Write a version of the Eliza program to simulate a conversation with a psychiatrist. (Type "ESC-x doctor" in xemacs for an example of such a program.)
Difficulty: 3

10. Write a problem to solve the 8-Puzzle (also known as sliding-blocks puzzle). Given an initial configuration, your program should output the moves necessary to solve the puzzle. For example, the puzzle
```          1   3
4 2 6
7 5 8
```
can be solved by moving 2 up, 5 up, and 8 left:
```	  1   3        1 2 3        1 2 3        1 2 3
4 2 6   ->   4   6   ->   4 5 6   ->   4 5 6
7 5 8        7 5 8        7   8        7 8
```
Your program should take the initial configuration as input (e.g., "1 0 3 4 2 6 7 5 8", where 0 represents the hole), and output the moves to solve the puzzle (e.g., "2, 5, 8").
Difficulty: 3

11. Write a program to solve the Pentomino puzzle. There are 12 puzzle pieces, similar to the Tetris pieces, but built from 5 unit squares each (not 4). Your program should find solutions for the 6x10, 5x12, 4x15, and 3x20 rectangles. See also this web page.
Difficulty: 3