Docsity

Homework 4 - Python , The Party Planner - Spring 2008 | CS 1301, Assignments of Computer Science

Toggle sidebar
bg1
Homework 4 CS 1301 – Spring 2008 Page 1/4
CS 1301 – Spring 2008
Homework 4 – Python, the Party Planner
Due: Friday, February 15, 2008 at 11:55 PM EST
Files to submit: hw4.py
Functions to include in hw4.py:
1. multiply()
2. summation()
3. digitSum()
4. average()
5. piñata()
For Help:
T-Square Forum
TA Helpdesk – schedule is posted on class website
Email/IM TAs
Notes:
Think through the logic of each of the problems before you start coding anything
Don’t forget to include the required comments and collaboration statement (as outlined
on the course syllabus)
Do not wait until the last minute to do this assignment in case you run into problems
If you find an error in the homework assignment, let a TA or Monica know immediately
Part 1 – Multiply
Recall the recursive function for multiply:
def mult(a, b):
if b == 0:
return 0
if b == 1:
return a
else:
return a + mult(a, b-1)
Now write a function called multiply() that calculates the product of two non-negative integers
using a while loop. Your function should NOT print anything – it should return the answer with
an actual return statement.
WARNING! Do NOT use the * operator or recursion for this or you will receive no credit
for this function. You must use a while loop! Also be sure your function handles 0.
Sample output:
>>> multiply(4, 2)
pf3
pf4

Partial preview of the text

Download Homework 4 - Python , The Party Planner - Spring 2008 | CS 1301 and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Spring 2008

Homework 4 – Python, the Party Planner

Due: Friday, February 15, 2008 at 11:55 PM EST

Files to submit: hw4.py Functions to include in hw4.py:

  1. multiply()
  2. summation()
  3. digitSum()
  4. average()
  5. piñata() For Help:  T-Square Forum  TA Helpdesk – schedule is posted on class website  Email/IM TAs Notes:  Think through the logic of each of the problems before you start coding anything  Don’t forget to include the required comments and collaboration statement (as outlined on the course syllabus)  Do not wait until the last minute to do this assignment in case you run into problems  If you find an error in the homework assignment, let a TA or Monica know immediately

Part 1 – Multiply

Recall the recursive function for multiply: def mult(a, b): if b == 0: return 0 if b == 1: return a else: return a + mult(a, b-1) Now write a function called multiply() that calculates the product of two non-negative integers using a while loop. Your function should NOT print anything – it should return the answer with an actual return statement. WARNING! Do NOT use the * operator or recursion for this or you will receive no credit for this function. You must use a while loop! Also be sure your function handles 0. Sample output:

multiply(4, 2)

8

multiply(0, 85) 0 multiply(14, 23) 322

Part 2 – Summation

Write a function that uses a while loop to sum the numbers from 1 through n. For example, summation(4) = 4 + 3 + 2 + 1 or 1 + 2 + 3 + 4 = 10 Your function must return the result. (No printing.) If you do not use a while loop for the calculation you will get no credit. Sample output:

summation(4) 10 summation(31) 496

Part 3 – Digit Sum

Write a function that returns the sum of the digits of the integer parameter using a while loop. For example, digitSum(143) = 3 + 4 + 1 = 8 HINT: The modulus operator (%) is your friend! Recall that a number mod 10 gives you the last digit and the number divided by 10 (integer division) gives you the other digits (without that last one). Sample Output:

digitSum(4) 4 digitSum(14) 5 digitSum(987654) 39 If you do not use a while loop you will get no credit.

Part 4 – Average

Remember how to compute the average of a set of numbers: average = sum of numbers / count of numbers What is a sentinel value? In computer programming, a sentinel value (also referred to as a flag value or signal value) is a

Enter the next number to average (type q to quit): 53. Enter the next number to average (type q to quit): 474. Enter the next number to average (type q to quit): - Enter the next number to average (type q to quit): 747 Enter the next number to average (type q to quit): 983. Enter the next number to average (type q to quit): 100. Enter the next number to average (type q to quit): q Average: 376.

Part 5 – Piñata Time!!

You are planning a party where the main attraction is a fabulous piñata. You need to fill the piñata with enough candy so that all of your guests reach near satiation (3 pieces of candy per person or slightly more). Prompt the user for the number of guests attending the party. Using a while loop, while the total amount of candy is less than three times the people attending, keep asking the user to put more candy in the piñata. Pretend that the size or amount of the amount of candy is proportional to the length of the name of the candy inputted. (See the example runs below to make sense of this  ) Let the user know how much more candy they need to put in the piñata before it is full. Once the piñata is full, let the user know and print out all the different types of candy in the piñata. Sample output:

pinata() How many people are at the party? 5 You still need 15 pieces of candy to fill the pinata What kind of candy would you like to put in the pinata? Snickers You still need 7 pieces of candy to fill the pinata What kind of candy would you like to put in the pinata? Gum You still need 4 pieces of candy to fill the pinata What kind of candy would you like to put in the pinata? Powerbars The pinata is full! You filled the pinata with: Snickers Gum Powerbars

Final thoughts

Be sure to name your files and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written/Adapted by Beth York, Spring 2008