Computer Science 1 Homework 3 Lists and If statements

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment

Description

5/5 - (2 votes)

Part 1: How Have the Popular Baby Names Changed?
Create a folder for HW 3. Download the zip file hw3Files.zip from Piazza. Put it in this folder
and unzip it. You should see
read_names.py
names_example.py
top_names_1880_to_2014.txt
hw3_util.py
legos.txt
2
The first three will be used in this part, while part 2 will use hw3_util and legos.txt. Do all of
your work for part 1 in this folder. Start by opening names_example.py in the Wing IDE and then
read the following…
We have data from the Social Security Administration that gives the top 250 female and male baby
names for every year from 1880 up to and including 2014, and also gives you how many babies were
given each name. These data are stored the file top names 1880 to 2014.txt.
We have also provided you with a module called read names.py that gives you easy access to this
data. The program names_example.py provided to you and shown here illustrates the use of this
module and the resulting lists:
‘ ‘ ‘
I n i t i a l example t o dem on s t r a te how t o read and a c c e s s t h e baby names
and c o u n t s .
‘ ‘ ‘
import read names
# Read in a l l t h e names . The r e s u l t i s s t o r e d in t h e module .
read names . r e a d f r o m f i l e ( ” t o p n am e s 1 8 8 0 t o 2 0 1 4 . t x t ” )
# Access t h e fem ale names and c o u n t s f o r 1886
( female names , f em al e c o u n t s ) = read names . t o p i n y e a r ( 1 8 8 6 , ‘ f ‘ )
pr int ( ”The most common f em al e name i n 1 8 8 6 , with a count o f { : d} , i s { : s }”\
. format ( f em al e c o u n t s [ 0 ] , fem ale n ame s [ 0 ] ) )
# Access t h e male names and c o u n t s f o r 1 9 9 7 . Note t h a t t h e 100 t h most
# p o p ul a r name i s in p o s i t i o n 99 o f t h e l i s t .
( male names , m al e c o u n t s ) = read names . t o p i n y e a r ( 1 9 9 7 , ‘M’ )
pr int ( ”The 100 th most common male name i n 1 9 9 7 , with a count o f { : d} , i s { : s }”\
. format ( m al e c o u n t s [ 9 9 ] , male names [ 9 9 ] ) )
Play around with code until you understand what it is doing and how to work with the two lists
that are returned. Only when you are comfortable should you proceed to the actual assignment…
Write a program that does the following:
1. It asks the user for a year in the range from 1880 up to and including 2014. It should check
to see if the year is in the appropriate range and if it is not the program should print an error
message (see example below) and stop.
2. It asks the user for a female name and finds the index of the name in the list.
3. It then finds the index of the name at plus and minus 5 and 10 years from the selected date,
ignoring any dates that are outside of the 1880 – 2014 range.
3
4. At this point, for each valid year in the test set year-10, year-5, year, year+5, year+10,
if the name was not found in that year the program should print a message saying it was not
found. If the name was found in that year it outputs the statistics about the name including
the year tested, the rank, the count, the percentage of the count relative to the count of the
top ranked name, and the percentage of the name relative to the sum of all the name counts
in the top 250. For any index less than 0 or greater than 249, nothing should be printed.
5. It should ask for a male name and do the same.
Example output when running from the Wing IDE is below. Formatting should use 3 spaces for
the rank {:3d}, 5 spaces for the count ({:5d}), and 7 spaces for each percentage ({:7.3f}).
First example
Enter the ye a r t o check => 2014
2014
Enter a f em al e name => Emma
Emma
Data about f em al e names
Emma:
2 0 0 4: 2 21599 8 6. 3 1 0 2. 1 4 5
2 0 0 9: 2 17881 8 0. 2 6 3 1. 9 2 5
2 0 1 4: 1 20799 1 0 0. 0 0 0 2. 3 2 1
Enter a male name => Jonathan
Jonathan
Data about male names
Jonathan :
2 0 0 4: 21 14357 5 1. 5 1 2 1. 0 3 4
2 0 0 9: 29 11359 5 3. 7 2 2 0. 8 7 7
2 0 1 4: 44 8035 4 1. 9 7 1 0. 6 6 5
Second example
Enter the ye a r t o check => 1952
1952
Enter a f em al e name => Mary
Mary
Data about f em al e names
Mary :
1 9 4 2: 1 63238 1 0 0. 0 0 0 5. 5 8 7
1 9 4 7: 2 71684 7 1. 9 1 4 4. 8 0 7
1 9 5 2: 2 65681 9 7. 8 9 1 4. 2 3 8
1 9 5 7: 1 61096 1 0 0. 0 0 0 3. 6 5 7
1 9 6 2: 2 43497 9 4. 3 8 8 2. 7 8 4
Enter a male name => Jackson
4
Jackson
Data about male names
Jackson :
1 9 4 2: Not i n the top 250
1 9 4 7: Not i n the top 250
1 9 5 2: Not i n the top 250
1 9 5 7: Not i n the top 250
1 9 6 2: Not i n the top 250
Third example
Enter the ye a r t o check => 2015
2015
Year must be a t l e a s t 1880 and a t most 2014
Here are several notes to help you.
ˆ If you import a module called sys then you can stop your program from executing with the
statement sys.exit(). This is a nice thing to be able to do when you find an error in your
input.
ˆ You can easily pass lists as arguments to functions. For example
d e f t o t al a n d a v g ( o n e l i s t ) :
n = l e n ( o n e l i s t )
t o t a l = sum ( o n e l i s t )
avg = t o t a l / f l o a t ( n )
p r i n t ( ‘ f i r s t { : d} , t o t a l { : d} , avg { : 5 . 2 f } ‘ . forma t ( o n e l i s t [ 0 ] , t o t al , avg ) )
y = [ 8 , 2 , 4 , 5 ]
t o t al a n d a v g ( y )
ˆ Writing one or two functions can make the code much, much simpler. One function might
be given as parameters a single name, the year, the list of names and the list of counts, and
from these it can print a line of the table. The function would be called once for each year
and would ignore years outside of the valid range. A second function, called from within this
first function, might calculate and print the statistics for a given index in the counts list, or
perhaps return the statistics as a tuple.
ˆ The following example will help you understand how to find if a value is in the list and the
list index.
>>> a = [ 1 6 5 , 3 2 , 8 9 , 16 ]
>>> x = 32
>>> x i n a
True
>>> i = a . inde x ( x )
>>> i
5
1
>>> p r i n t ( a [ i ] , a [ i +1])
32 89
>>> 183 i n a
F al s e
>>> j = a . inde x ( 1 8 3 )
Traceback ( most r e c e n t c a l l l a s t ) :
F i l e ””, l i n e 1 , i n
V alueE r r o r : 183 i s not i n l i s t
Note that it is an error to ask for an index that is not in the list!
When you are finished, submit your program to Submitty as hw3Part1.py. You must use this
filename, or your submission will not work in Submitty. You do not have to submit any of the files
we have provided. Note that you do not need loops for this part. Use the appropriate list functions
such as sum and find to generate the answers. Use of loops will be penalized in the grading.
Part 2: Legos (Everything is awesome!)
In celebration of everyone’s childhood, we have a lego problem in this homework. We will solve a
simple problem in this part.
Suppose you are given a list of lego pieces that you own, but you have a new project. You want to
see if you have enough of a specific piece. But, you can put together different lego pieces to make
up bigger pieces too.
1×1 2×1 2×2 2×4
2×2 2×2
+ =
2×4
Figure 1: All the possible lego pieces for this homework are shown on the top row. The name
explains the dimensions of the lego. The bottom row shows how you can combine two 2×2 pieces
to make up a 2×4 piece.
Write a program to read from a file the list of all lego pieces you currently have. Then, ask the
user for the type of lego piece that she is searching for. If the lego type is not in the list of possible
pieces, print out Illegal lego and exit Otherwise, print out how many of that piece you can
make using the legos in your collection. You will only consider methods in which one type of lego
is repeated. For example, you can make up a 2×4 lego using: two 2×2 legos, or four 2×1 legos, or
eight 1×1 legos. In other words, you do not consider the possibility of using two 2×1 legos and four
1×1 legos.
Let’s assume you have the following lego pieces available to you:
6
1×1, 6
2×1, 2
2×2, 2
2×4, 1
Here are some sample outputs of your program:
What type of lego do you need? ==> 2×4
2×4
I can make 2 2×4 pieces:
1 pieces of 2×4 using 2×4 pieces.
1 pieces of 2×4 using 2×2 pieces.
0 pieces of 2×4 using 2×1 pieces.
0 pieces of 2×4 using 1×1 pieces.
What type of lego do you need? ==> 2×1
2×1
I can make 5 2×1 pieces:
0 pieces of 2×1 using 2×4 pieces.
0 pieces of 2×1 using 2×2 pieces.
2 pieces of 2×1 using 2×1 pieces.
3 pieces of 2×1 using 1×1 pieces.
What type of lego do you need? ==> 4×2
4×2
Illegal lego
To solve this problem, you will first read from a file how many of each type of lego pieces you
currently have using the function provided in hw3_util as follows:
import hw3_util
legos = hw3_util.read_legos(‘legos.txt’)
print(legos)
If you execute this code with the above file, you will get the legos list below.
[‘1×1’, ‘1×1’, ‘1×1’, ‘1×1’, ‘1×1’, ‘1×1’, ‘2×1’, ‘2×1’, ‘2×2’, ‘2×2’, ‘2×4’]
7
A very easy way to solve this problem is to use the count() function of lists. For example, given
the above list, legos.count(‘1×1’) returns 6. You need to write the if statements to check for
each lego, whether a substitute exists.
It should be obvious how you can put smaller pieces together to make up bigger pieces, but we
provide possible substitutions here for completeness. We only use one type of lego for any request,
no mix and match.
Piece Possible replacement
2×1 2 1×1
2×2 2 2×1
4 1×1
2×4 2 2×2
4 2×1
8 1×1
Note that we only gave you one test file. But, you must create other test files to make sure that
the program works for all possible cases. Feel free to share your test files on Piazza and test cases.
Discussing test cases on Piazza are a good way to understand the problem.
We will test your code with different input files than the one we gave you in the submission server.
So, be ready to be tested thoroughly!
When you are finished, submit your program to Submitty as hw3Part2.py. You must use this
filename, or your submission will not work in Submitty. You do not have to submit any of the
files we have provided. Note that you do not need loops for this part. Use the appropriate list
functions. Use of loops will be penalized in the grading.
As an important final note: It is easy to overcomplicate this problem by doing calculations
based on the sizes of the legos. You do none of this!! You should write your code assuming that the
only legos that can possibly exist are ‘1×1’, ‘2×1’, ‘2×2’ and ‘2×4’ and you should hard-code
all of the replacements outlined above.
Part 3: Turtle moves!
Suppose you have a Turtle that is standing in the middle of an image, at coordinates (200,200)
facing right (along the dimensions). Assume top left corner of the board is (0,0) like in the images.
You are going to read input from the user five times. Though you do not have to use a loop for
this part, it will considerably shorten your code to use one. It is highly recommended that you
use a loop here. However, using a loop in place of simple list functions such as sorting will incur
penalties during grading.
Each user input will be a command to the Turtle. You are going to execute each command as it is
entered, print the Turtle’s current location and direction. At the same time, you are going to put
all the given commands in a list, sort this resulting list at the end of the program and print it as
is (no other formatting).
Here are the allowed commands:
8
ˆ move will move Turtle 20 steps in its current direction.
ˆ jump will move Turtle 50 steps in its current direction.
ˆ turn will turn the Turtle 90 degrees counterclockwise: right, up, left, down.
ˆ sleep will keep the turtle in the same spot for two turns.
If user enters an incorrect command, you will do nothing and skip that command. You should
accept commands in a case insensitive way (move, MOVE, mOve should all work).
There is a fence along the boundary of the image. No coordinate can be less than 0 or greater than
400. 0 and 400 are allowed. We realize that not all boundaries can be reached in 5 turns, but check
it anyway. It will make the program less fragile should we make the number of turns variable in a
later homework.
You must implement two functions for this program:
ˆ move(x,y,direction,amount)
will return the next location of the Turtle as an (x,y) tuple if it is currently at (x,y), facing
direction (direction one of right, up, left, down) and moves the given amount.
ˆ turn(direction)
will return the next direction for the Turtle currently facing direction, moving counterclockwise.
Now, write some code that will call these functions for each command entered and update the
location of the Turtle accordingly. Here is an example run of the program:
T u r tl e : ( 2 0 0 , 2 0 0 ) f a c i n g : r i g h t
Command (move , jump , turn , s l e e p ) => t u r r n
t u r r n
T u r tl e : ( 2 0 0 , 2 0 0 ) f a c i n g : r i g h t
Command (move , jump , turn , s l e e p ) => tu rn
tu rn
T u r tl e : ( 2 0 0 , 2 0 0 ) f a c i n g : up
Command (move , jump , turn , s l e e p ) => Move
Move
T u r tl e : ( 2 0 0 , 1 8 0 ) f a c i n g : up
Command (move , jump , turn , s l e e p ) => tu rn
tu rn
T u r tl e : ( 2 0 0 , 1 8 0 ) f a c i n g : l e f t
Command (move , jump , turn , s l e e p ) => jumP
jumP
T u r tl e : ( 1 5 0 , 1 8 0 ) f a c i n g : l e f t
All commands e n t e r e d : [ ‘ tu r rn ‘ , ‘turn ‘ , ‘Move ‘ , ‘turn ‘ , ‘jumP ‘ ]
S o r ted commands : [ ‘ Move ‘ , ‘jumP ‘ , ‘turn ‘ , ‘turn ‘ , ‘ tu r rn ‘ ]
In the above example, turrn is an invalid command, so it has no effect in the Turtle’s state. In case
you are wondering, the list is sorted by string ordering, which is called lexicographic (or dictionary)
ordering.
9
Here is a second example:
T u r tl e : ( 2 0 0 , 2 0 0 ) f a c i n g : r i g h t
Command (move , jump , turn , s l e e p ) => s l e e p
s l e e p
T u r tl e f a l l s a s l e e p .
T u r tl e : ( 2 0 0 , 2 0 0 ) f a c i n g : r i g h t
T u r tl e i s c u r r e n t l y s l e e p i n g . . . no command t h i s tu rn .
T u r tl e : ( 2 0 0 , 2 0 0 ) f a c i n g : r i g h t
Command (move , jump , turn , s l e e p ) => Move
Move
T u r tl e : ( 2 2 0 , 2 0 0 ) f a c i n g : r i g h t
Command (move , jump , turn , s l e e p ) => tu rn
tu rn
T u r tl e : ( 2 2 0 , 2 0 0 ) f a c i n g : up
Command (move , jump , turn , s l e e p ) => s l e e p
s l e e p
T u r tl e f a l l s a s l e e p .
T u r tl e : ( 2 2 0 , 2 0 0 ) f a c i n g : up
All commands e n t e r e d : [ ‘ sl e e p ‘ , ‘Move ‘ , ‘turn ‘ , ‘ sl e e p ‘ ]
S o r ted commands : [ ‘ Move ‘ , ‘ sl e e p ‘ , ‘ sl e e p ‘ , ‘turn ‘ ]
When you are finished, submit your program to Submitty as hw3Part3.py. You must use this
filename, or your submission will not work in Submitty.
10