Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CS5001

Spring 2020
HW3 (conditionals + while loops)
Assigned: January 23, 2020
Deadline: January 29, 2020 at 12:00 noon

Read this week’s handout (http://bit.ly/37gwPjK) before you begin this homework. 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 HW3 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 can use while
loops only (no range loops or for loops this time).

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
For each of the loops below, specify:
● Whether the loop is infinite
● Whether an error would result
● If neither of the above, what would be printed to the terminal? Be specific with spacing,
indentation, and line breaks.

1A
1 level = 0
2 danger = 3
3 while level < danger:
4 print("Beetlejuice!")
5 level += 1

1B
1 level = 0
2 danger = 3
3 while level > danger:
4 print("Beetlejuice!")
5 level += 1
6 print("Ghost with the Most!")

1C
1 level = 0
2 danger = 3
3 while level < danger:
4 print("shake", end = " ")
5 print("Senora!")

Written #2

2A Which of the following two options will correctly prompt the user until they enter a valid
weekend day?

# Option 1

SAT = "Sat"
SUN = "Sun"
day = input("Which day is better for brunch?\n")
while day != SAT and day != SUN:
day = input("Sorry, what? Please enter a valid day.\n")
print("Thank you!")

# Option 2

SAT = "Sat"
SUN = "Sun"
day = input("Which day is better for brunch?\n")
while day != SAT or day != SUN:
day = input("Sorry, what? Please enter a valid day.\n")
print("Thank you!")

2B For the option above that doesn’t work (i.e., it doesn’t succesfully detect a valid weekend day), is
the problem that...
(1) It thinks bad inputs (like “Mon”) are valid.
(2) It thinks good inputs (like “Sat”) are invalid.

Written #3
After the Python code below is run, what is the value of summation?
1 i = 0
2 summation = 0
3 while i < 7:
4 if i % 2 == 1:
5 summation += 1
6 i += 1

Written #4
Flow of control: List the line numbers in the order in which they would be executed
1 i = 0
2 while i < 2:
3 if i == 1:
4 break
5 i = i + 1
6 print("i is", i)

Programming Component (80% of this HW)

Code Structure and Style


A percentage of your score for every homework is for writing good, clear, readable code. For HW3, focus
on writing good loops. The only kind of loop allowed on this HW is the while loop.
Programming #1 (25% of this HW)
● Filename: horizon.py

For this part of the homework, we’ll do a little animation with Python.

When I run your finished program, it should look like this: https://youtu.be/WLeIhW7gZ_s

It’s a moon passing through the sky. When the moon reaches the sun, the sky changes from blue to gray;
then the sky flips back to blue again after the moon has moved on.

There are really no additional requirements. You can make your scene look different, or work at a
different speed, as long as the basic sun/moon/eclipse elements are there.

Helpful hints:
● It’s near-impossible to test graphics-related functions, so you’re not required to implement a test
suite for this one. Guidelines for good functions still apply, though, so make sure they’re concise,
clear, and well-documented.
● A while loop makes an animation work in Python. The moon is the thing moving across the sky,
so you need to update its x coordinate with every iteration of the loop.
● Your loop can just run forever, and the user ends the program by clicking out of the Turtle screen.
● Animation with just a for loop can look fritzy, though. We suggest creating at least two turtles --
one for the sun and one for the moon. Then, you re-draw the moon every time it’s at a new
position. A few functions that might come in handy: clear, update and tracer.

AMAZING points: These final two points may be awarded if you’ve completed the rest of the assignment
perfectly and blown us away with...
● Ignoring what we said about the loop not needing to end. Stop the animation nicely when the
moon gets all the way across the screen.

Programming #2 (25% of this HW)


● File names: nim_functions.py, nim.py, test_nim_functions.py

For this problem, you will write a program in which the computer plays a game of Nim against a hooman.

This version of Nim works as follows.


● The game starts with a pile of beans, a random number between 5 and 30.
● A coin flip determines which player goes first.
● The players alternate taking beans from the pile.
● Each player can take only 1, 2, or 3 beans from the pile.
● The player who takes the last bean loses the game.

Here’s an example of playing the game:


Program Requirements:
● There’s a lot of randomization and user interaction with this program, so you won’t be able to test
everything, and that’s fine.
● We require two functions that can and should be tested. You must write these as described and
test them in your test suite:
○ coin_flip : Given an integer, return the string ‘H’ if it’s even and ‘T’ if it’s odd.
○ is_over : Given an integer representing the number of beans in the game, return a
boolean indicating whether the game is over.
● You do not have to handle the case of the user entering non-numeric answers.
● You do not need to play strategically; the computer can choose a random number of beans for
each turn, as long it’s an acceptable number.
● Your wording doesn’t need to be the same as our example, but as always your program must be
friendly and informative.

AMAZING points: These final two points may be awarded if you’ve completed the rest of the assignment
perfectly and blown us away with...
● Ignore what we said above about playing strategically. Have the computer try to win!

Programming #3 (30% of this HW)


● Filenames (starter code and test code): sentence.py, test_typing_functions.py
● Filenames (your driver and functions): typing.py, typing_functions.py
It may not be the most glamorous skill, but we all need to be good at typing. Did you learn how to type in
the fourth grade with that little QWERTY dude? I did, and we had to have our typing speed evaluated
using the standard measure -- the number of words per minute you can type (correctly).

For this program, you’ll write your own version of the words-per-minute typing test. We’ve gotten a few
things started in the sentence.py file linked above (plus some test code in the other file), and you’ll
polish off the rest. We start off with a bunch of sentences to practice typing with. Some of them come
from actual typing tests we found online, some from the News @ Northeastern emails cluttering my
inbox, and some song lyrics. Just a random bunch of stuff.

We’ve provided the following functions (think of them as black boxes; trust that they do what they say).
● select_sentence. Returns a randomly-chosen sentence, one of the ones described above. (If
you call this function multiple times, it might return the same sentence more than once; that’s
expected and totally fine.)
● count_word. Returns the number of words in a given sentence.
● count_mismatches. Returns the number of word-mismatches between two sentences.
Punctuation and case matter. If one sentence is “Hello World!” and the other is “Hello World?”,
that is a mismatch count of one, because Hello is typed correctly but World! is not .

You’ll use these functions to put together a program that does the following:
● Gives the user sentences to type, until they type DONE and then the test is over.
● Counts the number of seconds from when the user begins to when the test is over.
● Counts and reports:
○ The total number of words the user typed, and how many seconds it took them.
○ The words-per-minute that turns out to be.
○ The number of mistakes they made.
○ The adjusted words-per-minute, accounting for mistakes.

WPM and adjusted WPM...


● You must write the two functions we’re testing in test_typing_functions.py. We’ll
expect all our tests to pass when we run them against your solution.

The number of mistakes is always subtracted from the total words typed, regardless of whether it was a
misspelled word, an extra word, or a missing word.

Requirements:
● The number of words typed and number of mistakes made should should be integers throughout
the program. The words per minute and adjusted words per minute, when reported on the
terminal, should be rounded to the nearest whole number.
● The number of seconds should be rounded to the nearest 100th.
● The test must keep going, selecting sentence after sentence until the user types DONE.
● The user might type DONE at the very beginning and just get zeroes for everything.

Helpful hints:
● Python has a time module that you’ll probably find useful.
● You should prompt the user to just type Enter at the beginning, and start the timer right after that.
● You should stop the timer right after they type DONE (don’t worry about any extra little bits of
time it took them to type DONE).

AMAZING points: These final two points may be awarded if you’ve completed the rest of the assignment
perfectly and blown us away with...
● Submitting a modified sentence.py... don’t change any functions, but add your own
sentences for testing.
● Including in the block comment at the very top your own results from the typing test (mine are in
the example below)

Here’s an example of running the program (as always, your wording can vary!):

You might also like