辅导案例-CS5001

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
CS5001
Spring 2020
HW4 (loops and lists)
Assigned: January 30, 2020
Deadline: February 5, 2020 at 12:00 noon

Please read this handout on lists and this one on nested lists before you start. To submit your solution,
compress all files together into one .zip file. Login to handins.ccs.neu.edu with your Khoury
account, click on the appropriate assignment, and upload that single zip file. You may submit multiple
times right up until the deadline; we will grade only the most recent submission.

Your solution will be evaluated according to our CS5001 grading rubric . The written component accounts
for 20% of your overall HW4 score; the programming component for 80%. We’ll give written feedback
when appropriate as well; please come to office hours with any questions about grading (email to set up a
time if no current office hours work for you).

You are permitted two “late day” tokens in the semester; each one allows you submit a homework up to
24 hours late. You may apply both to this homework, or split them between two homeworks. The Hand-in
Server automatically applies one late-day token for every 24 hours (or part of 24 hours) you submit past
the deadline.

This is an introductory course, and we want to make sure that everyone has a solid understanding of the
fundamentals, so we’ll often rule some Python tools in or out. For this homework, you may not use any
list functions other than len and append (<==== that one too!) . To iterate over a list, you must use
range.

Written Component (20% of your homework score)
● Filename: written.txt

Please open a plaintext file (you can do this in IDLE or any plaintext editor you like, such as TextEdit or
NotePad) and type out your answers to the questions below. You can type your answers into Python to
confirm, but answer the questions first!

Written #1
Each of the code snippets below would generate some kind of error. Identify whether each one is a...
● NameError: A variable or function has been used before it has a value
● TypeError : A function/operation is applied to a value of the wrong type
● IndexError : A list or string subscript is out of range

1A
1
2
lst = [4, 5, 6]
lst[0] + lst
1

1B
1
2
lst = [18, 0, -5]
lst[3] + 3

1C
1
2
lst = [1, 2, 3]
lst[1] + "4"

Written #2
What do each of the following code snippets print to the terminal?

2A
1
2
3
nested = [[3, 4, 5], [8, 9, 10]]
for i in range(len(nested)):
print(nested[i])

2B
1
2
3
4
nested = [[3, 4, 5], [8, 9, 10]]
for i in range(len(nested)):
for j in range(len(nested[i])):
print(nested[i][j])



Written #3
After the Python code below is run, what is the value of pos ?
1
2
3
4
5
6
7
lst = [5, -1, 7, 0, -4, 4, -5, 0]
i = 0
pos = 0
while i < len(lst) and lst[i] != 0:
if lst[i] > 0:
pos += 1
i += 1

Written #4
For each of the Python snippets below, what will be printed to the terminal? Be specific about linebreaks
and indentation where applicable.

4A
2
1
2
3
lst = [3, 2, 1]
for i in range(len(lst)):
print(lst[i])

4B
1
2
3
lst = [3, 2, 1]
for i in range(len(lst)):
print(len(lst))


4C
1
2
3
lst = [3, 2, 1]
for i in range(len(lst) - 1, -1, -1):
print(lst[i])



Programming Component (80% of this HW)
You may not use any list functions other than len and append . To iterate over a list, you must use range.
Remember to refer to the CS5001 Style Guide when considering program structure and readability.

Programming #1 (15% of this HW)
● Filenames: football_functions.py, football_stats.py
● Starter code for reference (we’ll expect all the tests defined here to pass when we grade your
homework): test_football_functions.py

While I was teaching in London last semester, I decided to really embrace the culture over there by sitting
in the pub and watching soccer. I’m new to the sport, so I’m still figuring out who the players are, what
the rules are, and especially the lingo.

Of course, first you have to pick a favorite, so Tottenham Hotspur is my soccer team football club. (Actor
Kenneth Branagh is also a fan and narrated a video for the team , it’s super cool!) For this homework,
you’ll help me, personally, learn more about my new best thing ever by crunching the numbers from a
bunch of previous games matches. A club plays 38 Premier League matches per season. Below, you’ll
find a list of results from the 2017-2018 and 2018-2019 seasons, in that order, along with a list recording
the number of points goals Tottenham scored in each match.

When I run the main function in your football_stats.py , I should see a human-readable Q + A
type printout answering the following questions:
1. How many games did Tottenham win, lose, and draw over the two seasons?
3
2. How many games did we win despite scoring only one goal?
3. What did our “streaks” look like over the season?
4. A team starts a season with zero points*. They earn 3 points for a win, 1 for a draw, and 0 for a
loss. How many points did Tottenham have after the 20th game of the second season in the list?

*I mean, I think so.

Note that, even though we’re asking very specific questions here, we expect your code to work for any
reasonable inputs (if I wanted to ask the same questions about another team, or add more seasons to my
Tottenham lists). You should write one function for each of the questions, as specified below, and we
expect all the tests we’ve provided in test_football_functions.py to pass for these functions:
● count_result. Parameters: List of W/L/D, string for outcome. Returns: Number of games
with that outome, an int.
● count_one_wins. Parameters: List of W/L/D; list of integers with the number of goals per
game. Returns: Number of games we won with only one goal, an int.
● compile_streaks. Parameters: List of W/L/D. Returns: A string of streaks for the whole
season, something like "1W 1L 1D 1W 1D 4W"
● sum_points . Parameters: List of W/L/D; two ints s and n . Returns: the number of points we had
after the n th game of season s .

AMAZING points: These final two points may be awarded if you’ve completed the rest of the assignment
perfectly and blown us away with...
● Writing another function that calculates and reports your own fun stat.

Copy/paste these two lists into your football_stats.p y file (they’re from Wikipedia but I’m sure
they’re right, probably). In “real life” if you’re working with a big-ish dataset like this, you’d actually
read it in from a file, instead of copy/pasting. We’ll get to that soon!

These two lists correspond with each other at each position. For example, in the first game of the
2017-2018 season, the Spurs won and scored 2 goals. In the final game of the 2018-2019 season, they
drew and scored 2 goals.

RESULTS = ['W', 'L', 'D', 'W', 'D', 'W', 'W', 'W', 'W', 'L',
'W', 'L', 'D', 'L', 'D', 'W', 'W', 'L', 'W', 'W',
'W', 'D', 'W', 'D', 'W', 'D', 'W', 'W', 'W', 'W',
'W', 'W', 'L', 'D', 'W', 'L', 'W', 'W', 'W', 'W',
'W', 'L', 'L', 'W', 'W', 'W', 'W', 'L', 'W', 'W',
'W', 'L', 'W', 'W', 'W', 'W', 'W', 'L', 'W', 'L',
'W', 'W', 'W', 'W', 'L', 'L', 'D', 'L', 'L', 'W',
'W', 'L', 'W', 'L', 'L', 'D']

GOALS = [2, 1, 1, 3, 0, 3, 4, 1, 4, 0, 1, 0, 1, 1, 1,
5, 2, 1, 3, 5, 2, 1, 4, 1, 2, 2, 1, 1, 2, 4,
3, 2, 3, 1, 2, 0, 1, 5, 1, 3, 3, 1, 1, 2, 2,
4
1, 1, 0, 3, 1, 3, 2, 3, 2, 1, 6, 5, 1, 3, 0,
2, 2, 1, 3, 1, 0, 1, 1, 1, 2, 4, 0, 1, 0, 0, 2]


Programming #2 (35% of this HW)
● Filename: compress_functions.py

This is another real-life example: image compression. If you’ve ever run out of space on your phone, or
been frustrated with a slow download, you know why images are compressed -- they can be big and
clunky. There are a lot of different image compression algorithms out there, and today we’re going to
implement one.

An image comprises a nested list of pixels. A pixel is a small square of one color. Our compression
algorithm will count the number of consecutive pixels of the same color. For example, to representing six
purple pixels in a row, we’ll change P P P P P P into “ 6 P ”.

Finally, we’re adding another twist to this algorithm, based on a coding workshop I’m hosting on campus
soon (and thank you if you’re a volunteer! :) . We re-created an image in post-it notes; a single
8.5x11-inch piece of paper would hold 12 total post-its (3 rows and 4 columns). This problem will go the
same way -- when we compress the original image, we compress it into pages . Like this:

P P P P G G G G
P G G G Y Y L L
L L L L P P L L
L L P P L L L L
W W W W L L L L
W G G G L L P P
Original image, with 48 pixels. Each pixel would be
represented using a post-it when we do this as a fun coding
exercise for kids.


Page 1

Page 2
5

Page 3

Page 4
Original image split into 4 pages, each with 3 rows / 4 columns of post-its.

5 P 3 G 4 L 4 G 2 Y 2 L 2 P 2 L
2 L 2 P 5 W 3 G 10 L 2 P
The same 4 pages, with the colors compressed.

In the file compress_functions.py , you’ll write the function compress , which accepts a nested
list as input and returns another nested list, the compressed version of the image. When I run your code
and call compress , it should start with the starting list (shown below), and end up with the list shown in
step 2.

We recommend writing two functions, which compress would then call: One to transform the original list
(one row of the list = one row of pixels) into a page-specific list (one row = one page of 12 pixels); and
one to do the compression.

Here’s an example where the output would be two pages:
[[P, P, P, P],
[P, G, G, G],
[L, L, L, L],
[L, L, P, P],
[W, W, W, W],
[W, G, G, G]]
[[P, P, P, P, P, G, G, G, L, L, L, L],
[L, L, P, P, W, W, W, W, W, G, G, G]]
[["5 P", "3 G", "4 L"],
["2 L","2 P","5 W","3 G"]]
Starting list.

This is given to
your function,
compress .
(1) transform the original list into a
page-specific list (one row = one page
12 pixels).


(2) Final result, a nested
list where each sublist
represents a page. Each page
is compressed.


Here’s one where the output would be four pages, same as the illustrated multi-color example above.
[[P, P, P, P, G, G, G, G],
[P, G, G, G, Y, Y, L, L],
[L, L, L, L, P, P, L, L],
[L, L, P, P, L, L, L, L],
[W, W, W, W, L, L, L, L],
[W, G, G, G, L, L, P, P]]
[[P, P, P, P, P, G, G, G, L,
L, L, L],
[G, G, G, G, Y, Y, L, L, P,
P, L, L],
[L, L, P, P, W, W, W, W, G,
G, G],
[["5 P", "3 G", "4 L"],
["4 G", "2 Y", "2 L",
"2 P", "2 L"],
["2 L","2 P","5 W","3 G"],
["10 L", "2 P"]]
6
[L, L, L, L, L, L, L, L, L,
L, P, P]
Starting list

This is given to your
function, compress
(1) transform the original
list into a page-specific
list (one row = one page of
12 pixels)

(2) Final result, a nested
list where each sublist
represents a page. Each page
is compressed.




Helpful Hints:
● For this program, all you’re doing is defining functions; no main required. Make sure that you
create a function compress as described above so we can run it for grading.
● The compile-streak function you wrote for P1 will probably be reusable with just a few tweaks!
● You can assume that the number of pixels per page (3 rows, 4 columns) is constant.
● You can assume that there are no incomplete pages; the number of sublists is a multiple of three,
and the length of each sublist is a multiple of four. All the sublists are the same length.
● You’re not required to submit test code for this one, but you’ll definitely want to make sure that
everything works; we’ll test your compress function with the two examples above plus a few
more. Make sure the final result matches exactly what we have in the two tables above -- a nested
list of strings.

AMAZING points: These final two points may be awarded if you’ve completed the rest of the assignment
perfectly and blown us away with...
● Each individual function is no more than 30 lines total, including header and comments.

Programming #3 (30% of this HW)
● Filename: render.py

Finally, you’ll take a compressed image and render it on the screen. This part of the homework will
prompt the user.

When I run the main in your render.py file, it should prompt me by asking which image I want to render
-- big or little. (If I enter something invalid, re-prompt until I get it right.) Then, your program starts with
either the little image and big image list, compresses the list, and then renders it on the screen using
Turtle.

You can copy/paste the little and big lists linked above right into your code, or save them in a separate file
and import that file into render.py . The little image has 9 rows and 12 columns; the big image has 30
rows and 32 columns (but we’ll expect your code to be able to work on any image of any size, as long as
everything is in multiples of 3 for rows and 4 for columns).
7

The examples we’ve started off with use constants for colors that Turtle understands:
P = "purple"
B = "black"
L = "light blue"
Y = "yellow"
O = "brown"
R = "red"

Helpful Hints:
● The input lists must be compressed before you render the image. We’re mimicking real life here,
where images are compressed before they are uploaded, downloaded, saved, shared, whatever.
Your rendering functions should start with a compressed list (the lists you produced in P2).
● How will you know if it’s working? I’m pretty sure you’ll recognize the images we’ve created but
feel free to come by office hours if you want to check in on what you’re rendering and how it
should look.
● Because your image is compressed into pages, it’s easiest to draw one page at a time. Consider
making a draw_page function that accepts a list of strings representing the compressed color
codes for a single page.
● In our solution, we made each “pixel” actually a 10x10 square so the images would be a little
bigger and look like a cool retro 8-bit video game. That’s not a requirement, though; you can
make the sizes anything you like as long as it all renders like one single image.
● The bigger one takes a little time to render BUT YOU GUYS IT IS WORTH IT.

AMAZING points: These final two points may be awarded if you’ve completed the rest of the assignment
perfectly and blown us away with...
● Finding a way to create one turtle per page, and having them draw concurrently. I’m told there’s a
way to do this in Python Turtle but I’ve never tried it. ORRRR...
● Come up with your own image, like we’ve done above. Make it one of the menu options, and
compress/render it just like you did with our examples.

8
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468