CS 313 - Homework 10 - Problem collection

Due: Friday 12/10 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: Some problems will require language features not discussed in class. For example, to write a Tic-Tac-Toe player or an Eliza program, you will need to ask the user for input. You will have to consult the on-line documentation or search the web to figure out how to do such things.

To submit your code, create a zip file containing all your programs. Submit the code for each problem in a separate file with and intuitive name and the proper extension (e.g., "graphcycles.rkt" or "problem6.pl"). For problem 12, include all solved problems in a single file. For all problems, include sample output as comments in your files. Also, please list any collaboration and help you received and also list any souces you consulted. More than in previous assignments, in grading your solution, I will give special weight to good documentation and to good sample output that demonstrates thorough testing. Upload your file (one submission per group) via the HW 10 submission page by midnight of the last day of classes, Friday 12/10.

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
    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 an optimal game of Tic-Tac-Toe against the user, using a text-based user interface. (Do not search the web here, just wing it!)
    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. That is, no two queens may share the same row, column, or diagonal.
    Difficulty: 2

  7. Write a program that solves the Peg Solitaire game, 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: 2

  9. Write a version of the Eliza program to simulate a conversation with a psychiatrist. Today such programs are known as chatbots and are quite sophisticated. But Eliza dates back to the 1960s and only did simple pattern matching. Type "ESC-x doctor" in emacs for an example of such a program; or try this online version. Note your program can be quite simple, but it should pick up on words in the user's answers.
    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.
    Variation: Instead of Pentomino, write a program to solve Mike Naylor's A-Puzzle-A-Day. (This is my favorite puzzle of all times and it makes a good Christmas present!) Note that the pieces are 7 of the Pentomino pieces, plus a 2x3 rectangle. Your program should compute the solution for any given date provided as input.
    Difficulty: 3

  12. Python + Ruby special: Solve as many of the problems at Project Euler as you can, using only Python and Ruby, using each language for half of the problems. (If you solve an odd number of problems, i.e., 2N+1, then at least N solutions need to be in Python and N in Ruby.) You'll need to create an account so you can verify whether your solutions are correct. Once you submit a correct answer, you'll be able to see other people's solutions - feel free to check them out, especially the ones in Python or Ruby, but you may not modify your own solution at this point. Keep your programs as short as possible without sacrificing readability. For the purpose of this homework, all your solutions to the Project Euler problems only count as a single HW problem solved, in a separate paradigm, i.e., they don't restrict what languages you may use for the other problem(s), except you may not use Ruby again for a different problem. The total difficulty is computed as follows: problems 1-29 count as difficulty 1/3, and problems 30 and higher count as difficulty 0.5. The total difficulty is capped at 3, so you do need to solve at least one other problem. Please take a screen shot of your project Euler account that shows which problems you have solved. Submit your answers in three files, euler.py, euler.rb, and euler-screenshot.png.

  13. Wildcard - If you have some other really cool program you want to write, that's great! Just discuss it with me first, and we'll also need to agree on how many points it's worth ;).