Programming Assignment 8
-
(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 andpy -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
- Name the file and program
-
(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 andpy -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
- Name the file and program
-
(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 andpy -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]
- Name the file and program
-
(25 points) Problem 11.29 of the book.
Identical lists. Two lists
m1
andm2
are identical if they have the same contents. Write a functiondef equals(m1, m2)
that returnsTrue
ifm1
andm2
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 andpy -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 andpy -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
- Name the file and program
-
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 functionplay
(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
andch11_pr09.py
. - Example execution
(
python3 ch11_pr09.py
on MAC andpy -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
- Name the files and program
General Instructions, Turning in assignments, and Grading
General Instructions
- Name each file and program as listed in the instructions.
- The top of each program should have a comment block where the first line
is your name and following lines are a description of your program.
# Name: Jory Denny
# This program debates Batman vs Superman!
# References:
# - Ref 1. - Any program solution that defines its own function or class must have a
main
function and properly specified entry point into the program, e.g.:
def main():
# Do something
if __name__ == '__main__':
main() - Use proper coding style (described more in Grading below)
- While I support extra research, use of online materials, and collaboration with other students, please ensure the work is your own and you understand all portions of the work you turn in.
- Follow turn-in instructions precisely.
- Failure to complete any of these steps will result in a significant loss of points.
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.
- Soft copy (Online submission)
- Accept the invitation to the classroom assignment, which creates your repository on GitHub.
- Clone this repository, which includes some starting files.
- Add assignment files (stage, commit, push).
- Modify the Coverpage.txt file to provide name, sources, signature of following the honor code, etc. Be sure to stage, commit, and push these changes as well. If you do not fill out the coverpage, I will not grade the assignment.
- After the due date, I will clone your repository and add a grade and comments as necessary. After which you can pull these changes for your records.
Additionally, robotics problems will need to be demonstrated to me. This must be demonstrated to me by 5pm on the due date.
Points
- Each assignment is graded out of 100 points (not including bonus).
- Criteria and point distribution
- If the code is not named precisely or does not execute correctly, -50% on a problem.
- Following instructions and algorithm used to solve the problem, 25% on a problem. Following instructions is extremely important in computer science, train yourself to think like a computer. There are many ways to solve a problem, some may be better or worse than others.
- Stylistic elements of written code,
25% of a problem. Style includes (but is not
limited to):
- Every class, method, and function must be documented with docstrings describing its purpose, formal arguments, and return value.
- Control flow should be documented with intent and purpose of code
- Naming should be descriptive and consistent
- Indenting properly
- Consistent spacing
- Avoiding code duplication
- While I support extra research, use of online materials, and collaboration with other students, please ensure the work is your own and you understand all portions of the work you turn in. If I suspect excessive use of online materials, I will invite you to walk me through your solution. If you are not able to explain the work sufficiently, I will not give credit for that problem.
- If there are any discrepancies in grades please see the instructor during his office hours or by appointment (do not discuss with the lab assistants or graders).