Programming Assignment 8


  1. (25 points) Problem 10.10 of the book. Reverse a list. Write a function called def reverse(l) that reverses a list in place (using the same memory as the list itself and swapping elements). Note that this method will not return any value. Write a program (main) that prompts the user to enter a list of numbers, invokes the reversal function, and then displays the result.
  2. (25 points) Problem 13.5 of the book. Replace text. Write a program (main function) that replaces text in a file. Prompt the user for a filename, an old string, and a new string.
  3. (25 points) Problem 11.7 of the book. Points nearest to each other. Given a multi-list representing a series of 3D points (each point is a list of length 3), find the two points that are closest together. To facilitate, write the following two functions:

    # Compute the distance of two points
    def distance(p1, p2)

    # Compute the indices of the two nearests points
    def nearestPoints(points)

    Write a main function to test out your functions. Use a statically generated multilist for your test:

    points = [[-1, 0, 3], [-1, -1, -1], [4, 1, 1], [2, 0.5, 9], [3.5, 2, -1], [3, 1.5, 3], [-1.5, 4, 2], [5.5, 4, -0.5]]

    Note the output will invoke print on the multilist and lists.
  4. (25 points) Problem 11.29 of the book. Identical lists. Two lists m1 and m2 are identical if they have the same contents. Write a function def equals(m1, m2) that returns True if m1 and m2 are identical. Write a test program (main) that prompts the user for two lists of integers and displays whether the two are identical.
  5. Bonus. (10 points) Problem 11.9 of the book. Game: play tic-tac-toe. In a game of tic-tac-toe, two players take turns marking an available cell in a 3x3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create a class TicTacToe with a public function play (and as many private data fields and methods as you need) for playing tic-tac-toe. Your main program should create an instance and play the game. The user will enter a row and column to place their token. Protect against bad user input [(bad row/column less than 0 or greater than 2) and (already taken space)]. Report reasonable messages for errors and allow the user to try again. Report a reasonable end game message.