15-110 Fall 2010 Homework 2
Due Sunday, September 05, at 4pm (no late submissions accepted!)

Note:  to do this homework, you should download Homework2.zip, then unzip it, edit the files, rezip, and submit that zipped file to Autolab!

Read these instructions first!

·         Include your name, AndrewID, and section clearly on the top of each file in your assignment.

·         Place all your solutions in a single file ZIP file (extension.zip)   Once again, start with Homework2.zip.  No other file formats will be accepted. You need to start by downloading a ZIP file that contains a template (a file ending in .py) for each problem in this homework.

·         Show your work.  Correct answers without supporting calculations will not receive full credit.

·         How to submit:

o   Start at the course web site (http://www.cs.cmu.edu/~110)

o   Select the Autolab link

o   Select the Hw2 link

o   Select the Submit link

o   Click on the “Choose File” button

o   Find your Hw2 file that you are submitting

o   Click on the “Upload” button

o   If it worked, you will see this text:  Submission Accepted

o   Your submission is not completed until you see that text!!!

·         Q & A
   Q:  Can I submit multiple times before the deadline?
   A:  Yes, but only the last submission is graded.
   Q:  What if I’m having problems?
   A:  Email your team’s CA’s.  They will help you!
   Q:  Are submission that are just a few seconds late ok?
   A:  No.  The deadline is exact, and no late submissions are accepted.
   Q:  How do I get my grade and the graders’ comments?
   A:  Go back to Hw1 in Autolab and elect “view scores”.

·         Once again:  Late submissions will be rejected.

 


Log

We want to gauge how much time you are spending in each homework and how you are using your resources. So, please, answer the following questions (add it to your answers to problems 6 & 7):

 

Log Q1:  How much time did you spend on this homework assignment?   Please report actual time on task, not counting Facebook excursions, a quick World of Warcraft expedition, or any other non-assignment time.

Log Q2: What resources did you use to complete your assignment? Please check all that apply.
a)  Instructor's office hours
b)  Course Assistants' office hours
c)  Email communication with the Instructor
d)  Email communication with the Course Assistants
e)  Discussions with classmates or friends
f)   Notes posted on the course website
g)  Textbook
h)  Other resources found on the Internet
i)   Other (please specify) ___________________

 


Note:  When solving the following problems you are not allowed:

 

               

Problem 1 (15 pts)

Using the file weights.py, assign the values 9, 3, and 1 to each of three variables.  Using only
different combinations of those variables and the addition and subtraction operators, print all the positive weights that can be measured with 9, 3, and 1 pound measures (1-13).  Each weight should be printed with a separate print statement.

 

Problem 2 (15 pts)

Write a function, enoughEggCartons, that takes two integers -- the number of eggs and the number of egg cartons, both assumed to be non-negative -- and returns true if there are enough egg cartons to hold that many eggs and false otherwise.  A carton of eggs contains 12 eggs.  Write your solution using the file eggs.py.

 

Problem 3 (15 pts)

Write a program to give proper change using the fewer number of coins (quarters, dimes, nickels, and pennies).  Ask the user to enter change amount in cents to start with (assumed to be positive). Do the conversion from cents to the other currency denominations. Display the original amount of cents and their conversion in quarters, dimes, nickels, and cents. Then, display the total number of coins needed to provide the exact change. For example, if the user enters 341 (cents) your program should print 341 cents is equal to 13 quarters 1 dimes 1 nickels and 1 cents.  16 coins are needed.  Write your solution using the file change.py.

 

Problem 4 (15 pts)

Write a function shortIntegerToBinary that takes as an integer number between 0 and 255 (included), and returns a string containing an 8-bit representation of that number.  For example, shortIntegerToBinary(47) should return “00101111”.  Write your solution using the file binary.py.

 

Problem 5 (15 pts)

Write a function nearestBusStop that takes a street number (which you may assume is positive), and returns the street number of the nearest bus stop, where buses stop on streets that are multiples of 8 (8th, 16th, 24th, etc). So we see:


* For 11th street, the nearest bus stop is 8th street.
* For 13th street, the nearest bus stop is 16th street.
* For 12th street, however, both 8th street and 16th street are equally far, but we prefer the

   lower bus stop, which is 8th street.


You can assume there is a bus stop on 0th street.  Write your solution using the file bus.py.

 

 

 


Problem 6 (15 pts, 3 pts each question)

For all the questions below, assume that x and y are always integers.

 

6a. For any k>0, if (x % k) equals (k % x), then we know x is _______________.
 

6b.  True or False:   (x % y % x) always equals (x % y)?
 

6c. If (x/y) equals (y/x), then what must be true about x and y?
 

6d.  Assuming that variables x, y, and z all hold non-negative integer values, under what conditions in general will the following line of code crash?
                                mystery = x / y / z - x / (y / z)
 

6e.  For positive integers  a, b, and c, if (a / b / c) equals (a / (b / c)), what must be true about c?

 

 

 

Problem 7 (10 pts)

In the written portion of your submission, indicate what each of the following will print.  Do not run these programs.  Figure this out by hand.  Remember to show your work.

 

7a. (3 pts)

 

x = 10

y = 3

print(4 + x % y)

print(99 / x)

print(x / 99)

print(99 % x)

print(x % 99)

print(7 % 7 / 6 % 6)

 

 


7b. (4 pts)

 

x = 17 % 6

y = x - 8

x += 1

y -= 1

z = max(y/x, abs(x/y))

print x, "," , y , ",", z

 

 

7c. (3 pts)

 

x = 10

y = 3

print(99/x + 99.0/x)

print x/99

print(4+99%x)

x += 1

y *= 2

print(x%12+y)

 

 

 

The following problems are for extra credit and they are optional. You may skip them without affecting your grade in this homework.

 

Bonus Problem A (3 extra pts)

Write a function letterGrade that takes an integer score for a grade and returns a character representing the letter grade for that score, where scores of 90 or higher receive an A, 80-89 receive a B, 70-79 receive a C, 60-69 receive a D, and below 60 receive an E.  Write your solution using the file grade.py.

 

 

Bonus Problem B (3 extra pts)

Write a function isTriangle that takes three integers representing the length of three line segments and returns true if it possible to form a triangle with those three line segments  and false otherwise.  The rule is that if any of the three lengths is greater than the sum of the other segments then you cannot form a triangle.  Write your solution using the file triangle.py.