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.
    • Name the file and program ch10_pr10.py.
    • Example execution (python3 ch10_pr10.py on MAC and py -3 ch10_pr10.py on Windows) user provides input (red):
      Enter a list of numbers: 2 5 3 7 6
      The numbers reversed are: 6 7 3 5 2
  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.
    • Name the file and program ch13_pr05.py.
    • Example execution (python3 ch13_pr05.py on MAC and py -3 ch13_pr05.py on Windows) user provides input (red), input file can be downloaded/seen here:
      Enter a filename: story.txt
      Enter a string to be replaced: pig
      Enter the new string to replace the old string: dog
      Done
      output file contents can be seen here
  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.
    • Name the file and program ch11_pr07.py.
    • Example execution (python3 ch11_pr07.py on MAC and py -3 ch11_pr07.py on Windows) user provides input (red):
      Considering the following 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]]
      The nearest points are: [4, 1, 1] and [3.5, 2, -1]
  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.
    • Name the file and program ch11_pr29.py.
    • Example execution (python3 ch11_pr29.py on MAC and py -3 ch11_pr29.py on Windows) user provides input (red):
      Enter m1: 51 25 22 6 1 4 24 54 6
      Enter m2: 51 22 25 6 1 4 24 54 6
      The two lists are identical
    • Example execution (python3 ch11_pr29.py on MAC and py -3 ch11_pr29.py on Windows) user provides input (red):
      Enter m1: 51 5 22 6 1 4 24 54 6
      Enter m2: 51 22 25 6 1 4 24 54 6
      The two lists are not 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.
    • Name the files and program tictactoe.py and ch11_pr09.py.
    • Example execution (python3 ch11_pr09.py on MAC and py -3 ch11_pr09.py on Windows) user provides input (red):
      -------------
      |   |   |   |
      -------------
      |   |   |   |
      -------------
      |   |   |   |
      -------------
      Enter a row (0, 1, or 2) for player X: 0
      Enter a column (0, 1, or 2) for player X: 0
      -------------
      | X |   |   |
      -------------
      |   |   |   |
      -------------
      |   |   |   |
      -------------
      Enter a row (0, 1, or 2) for player O: 1
      Enter a column (0, 1, or 2) for player O: 4
      Invalid space, retry
      Enter a row (0, 1, or 2) for player O: 1
      Enter a column (0, 1, or 2) for player O: 1
      ...
      -------------
      | X | O | X |
      -------------
      |   | O | X |
      -------------
      | O |   | X |
      -------------
      X player won


General Instructions, Turning in assignments, and Grading

General Instructions

Turn in Instructions

Each assignment will be turned in through GitHub classroom. Please find the link to create a repository at the top of this page, or through this link. Robot problems must be demonstrated by 5pm and assignments must be submitted to GitHub by 11:59pm of the due date. I do not accept any late assignments.

Additionally, robotics problems will need to be demonstrated to me. This must be demonstrated to me by 5pm on the due date.

Points