Docsity

Homework 4 for Python ,the Road Warrior - Introduction to Computing | CS 1301, Assignments of Computer Science

Toggle sidebar
bg1
Homework 4 CS 1301 – Fall 2007 Page 1/4
CS 1301 – Fall 2007
Homework 4 – Python, the Road Warrior
UPDATED VERSION: September, 27, 2007
Due: Friday, September 28, 2007 at 6 PM EST
Files to submit: hw4.py
Functions to include in hw4.py:
1. multiply(num1, num2)
2. summation(num)
3. digitSum(num)
4. roadtrip(dollars)
For Help:
T-Square Forum – see course syllabus for more detailed information
TA Helpdesk – schedule is posted on class website
Email 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 know immediately
Part 1 – Multiply
Recall the recursive function for multiply:
def mult(a, b):
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 out anything – it should return the answer.
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)
pf3
pf4

Partial preview of the text

Download Homework 4 for Python ,the Road Warrior - Introduction to Computing | CS 1301 and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Fall 2007

Homework 4 – Python, the Road Warrior

UPDATED VERSION: September, 27, 2007

Due: Friday, September 28, 2007 at 6 PM EST

Files to submit: hw4.py

Functions to include in hw4.py:

1. multiply(num1, num2)

2. summation(num)

3. digitSum(num)

4. roadtrip(dollars)

For Help:

 T-Square Forum – see course syllabus for more detailed information

 TA Helpdesk – schedule is posted on class website

 Email 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 know immediately

Part 1 – Multiply

Recall the recursive function for multiply:

def mult(a, b): 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 out anything – it should return the answer.

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)

multiply(14, 23) 322 Part 2 – Summation

Write a function using a while loop that returns the sum of digits 1 through n. For example,

summation(4) = 1 + 2 + 3 + 4

If you do not use a while loop 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 passed in using a while loop.

For example, digitSum(14) = 1 + 4

HINT: The modulus operator is your friend! Recall that a number mod 10 gives you the last

digit and the number divided by 10 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 – The Road Trip!

You and your friends are going on a cross-country road trip with one catch—as long as you have

money you can continue on the road trip otherwise your trip is OVER. As you drive you run into

three situations – being hungry, tired and lost.

When you are hungry, you stop to pick up some snacks – it costs the digit sum of the number of

dollars you have divided by two (2). Note: Divide the number of dollars you have by 2 then take

the digit sum of this number.

(c) I am lost (q) Quit-get me out of here! Please enter your response (lowercase letter): b You spent $21.00 to get a hotel room and have $74.00 left How is that drive going? (a) I am hungry (b) I am sweepy (c) I am lost (q) Quit-get me out of here! Please enter your response (lowercase letter): c You were abducted and it cost $100.00 to get out of that situation with $- 26.00 left

roadtrip(250) How is that drive going? (a) I am hungry (b) I am sweepy

(c) I am lost (q) Quit-get me out of here! Please enter your response (lowercase letter): a You spent $8.00 on those snacks and have $242.00 left How is that drive going? (a) I am hungry (b) I am sweepy (c) I am lost (q) Quit-get me out of here! Please enter your response (lowercase letter): q 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, Fall 2007