CMSC 201 homework AI makeup
This is a makeup homework for students who may have had an issue with academic integrity this semester. Everything this semester is fair game regarding content including classes/objects, file I/O, control structures, and anything else.
Remember to enable Python 3 before you run your programs:
/usr/bin/scl enable python33 bash
Each one of these exercises should be in a separate python file. For this assignment, you may assume that all the input you get will be of the correct type (e.g., if you ask the user for a whole number, they will give you an integer).
For this assignment, you'll need to follow the class coding standards, a set of rules designed to make your code clear and readable. The class coding standards are on Blackboard under “Course Documents” in a file titled
“CMSC 201 - Python Coding Standards.”
You will lose major points if you do not following the 201 coding standards.
A very important piece of following the coding standards is writing a complete file header comment block. Make sure that each file has a comment block at the top (see the coding standards document for an example).
NOTE: You must use main() in each of your files.
Homework AI is broken up into three parts. Make sure to complete all 3 parts.
NOTE: Your filenames for this homework must match the given ones exactly.
And remember, filenames are case sensitive.
Write a program to import a text file with data related to a hotel record. For this assignment you are going to import the file and iterate through the file outputting the important information.
Additionally, you need to use the following functions: getInput() – opens the file and returns the file object cleanInput(yourList) – returns the list as a two-dimensional list output(yourList) – prints the output as below main() – acts as the driver for the program
Here is some partial sample output (there are more than 100 lines of input), with the user input in blue.
bash-4.1$ python hwAI_part1.py What is the name of the file?: AI1.txt Tom Lars stayed for 3 nights and paid $120.98 per night for a total of $362.94 Eric Tall stayed for 2 nights and paid $149.54 per night for a total of $299.08 Tim Can stayed for 2 nights and paid $113.54 per night for a total of $227.08 Kathy Bo stayed for 1 nights and paid $92.29 per night for a total of $92.29 Sam Ewes stayed for 2 nights and paid $200.18 per night for a total of $400.36 Mo Bite stayed for 3 nights and paid $197.92 per night for a total of $593.76 Van Sanch stayed for 1 nights and paid $89.26 per night for a total of $89.26
The sample input file that was used to create the sample output above, AI1.txt, is much too long to include in this document. However, you can directly download the file using the “cp” command.
The command below will copy the file “AI1.txt” from Professor Gibson’s public directory to your current directory. The period at the end (“.”) means that the file will have the same name after you copy it, so AI1.txt will be the copied file’s name. Make sure to run the command from the folder you want the file to be copied into!
cp afs/umbc.edu/users/k/k/k38/pub/cs201/AI1.txt .
(WARNING: This part of the homework is the most challenging, so budget plenty of time and brain power. And read the instructions carefully!)
Mike is an accountant. During tax season, he is required to enter a wide variety of dollar amounts to file taxes. He often has to enter the dollar amounts manually. As part of a research project, we are trying to calculate how far his fingers have to move to enter each dollar amount. He enters dollar amounts using a keypad which has the following layout:
On his keypad, there is a measurable distance between each of the keys. The distance between each adjacent key (horizontal or vertical) is exactly one inch. For example, the distance between the center of 7 and 4 would be one inch. The distance between 8 and 0 would be three inches.
Therefore, we can calculate the distance between each of the diagonals using the Pythagorean Theorem. The distance would be square root of a2 + b2. As we know that a and b are going to be 1 in this case then we know that the distance between each diagonal is the square root of 1+1.
For example, here is how Mike would type out the number 9851:
- He starts his finger at 9 and pushes the key.
- He moves his finger to the left 1 inch to 8 and pushes the key.
- He moves his finger downwards 1inch to 5 and pushes the key.
- He moves his finger diagonally downwards and left sqrt(2 inches) to 1 and pushes the key.
Therefore the total distance that Mike moved his finger to type in 9851 is 1 + 1 + sqrt 2 which is about 3.41 inches.
For this part of the homework, your goal is to write a program that calculates the distance Mike must move his finger to type in arbitrary dollar amounts.
bash-4.1$ python hwAI_part2.py Enter a dollar amount: 1053.34 Distance: 11.54 inches bash-4.1$ python hwAI_part2.py Enter a dollar amount: 80121.41 Distance: 10.41 inches $
- You may want to use a dictionary to map the locations of the keys.
- Don’t forget the decimal (.) is included in the distance!
- In order to calculate the square root, you will need to import the math library and use sqrt() for the diagonals.
For this assignment, you are going to be given a small data file. Here are the contents of the data file AI2.txt:
8 1 6 3 5 7 4 9 2 3 5 7 8 1 6 4 9 2 8 1 6 7 5 3 4 9 2 2 7 6 9 5 1 4 3 8 4 9 2 3 5 7 8 1 6
Each of the 5 lines in the data file represents a 3x3 magic square. A magic square is a grid using the numbers 1 through 9 (exactly one of each) where each row, column, and diagonal adds up to 15.
For example, line 1 above (8 1 6 3 5 7 4 9 2) looks like this:
8 1 6 3 5 7 4 9 2
Each of the three rows (in green above) need add up to 15.
Each of the three columns (in red above) need to add up to 15.
Each of the two diagonals (in blue above) need to add up to 15.
Here is some partial sample output, with the user input in blue (There should
bash-4.1$ python hwAI_part3.py Board 1 816 357 492 Board 1 is a magic square Board 2 357 816 492 Board 2 is not a magic square
The sample input file that was used to create the sample output above, AI1.txt, is difficult to copy and paste from a Windows-based environment. However, you can directly download the file using the “cp” command.
The command below will copy the file “AI2.txt” from Professor Gibson’s public directory to your current directory. The period at the end (“.”) means that the file will have the same name after you copy it, so AI2.txt will be the copied file’s name. Make sure to run the command from the folder you want the file to be copied into!
cp afs/umbc.edu/users/k/k/k38/pub/cs201/AI2.txt .