CMSC 201 – Computer Science I for Majors Page 1
Homework AI – Makeup
Assignment: Homework AI – Makeup
Value: 80 points
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
Instructions
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.
CMSC 201 – Computer Science I for Majors Page 2
Details
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.
hwAI_part1.py
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
CMSC 201 – Computer Science I for Majors Page 3
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 .
CMSC 201 – Computer Science I for Majors Page 4
hwAI_part2.py
(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:
7 8 9
4 5 6
1 2 3
. 0
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:
1. He starts his finger at 9 and pushes the key.
2. He moves his finger to the left 1 inch to 8 and pushes the key.
3. He moves his finger downwards 1inch to 5 and pushes the key.
4. 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.
CMSC 201 – Computer Science I for Majors Page 5
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.
Here is some sample output, with the user input in blue.
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 $
HINTS:
1. You may want to use a dictionary to map the locations of the keys.
2. Don’t forget the decimal (.) is included in the distance!
3. In order to calculate the square root, you will need to import the math
library and use math.sqrt() for the diagonals.
CMSC 201 – Computer Science I for Majors Page 6
hwAI_part3.py
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 3×3 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
be 5 boards when you are finished).
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
CMSC 201 – Computer Science I for Majors Page 7
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 .
CMSC 201 – Computer Science I for Majors Page 8
Submitting
Once all three parts of your Homework are complete, it is time to turn them in
with the submit command.
Don’t forget to complete the header block comment for each file! Make sure
that you updated the header block’s file name and description for each file.
You must be logged into your GL account, and you must be in the same
directory as the Homework AI files. To double check this, you can type ls.
linux1[3]% ls
hwAI_part1.py hwAI_part2.py hwAI_part3.py
linux1[4]% █
To submit your files, we use the submit command, where the class is
cs201, and the assignment is HWAI. Type in (all on one line)
submit cs201 HWAI hwAI_part1.py hwAI_part2.py
hwAI_part3.py
and press enter.
linux1[4]% submit cs201 HWAI hwAI_part1.py hwAI_part2.py
hw5AI_part3.py
Submitting hwAI_part1.py…OK
Submitting hwAI_part2.py…OK
Submitting hwAI_part3.py…OK
linux1[5]% █
If you don’t get a confirmation like the one above, check that you have not
made any typos or errors in the command.
You can double-check that all three homework files were submitted by
using the submitls command. Type in submitls cs201 HWAI and hit
enter.
And you’re done!