Friday, October 23, 2009

Blog 4: Python Output Homework

This is just half the homework! Numbers 3 and 4 aren't done. It's due the 28th according to the class site. Most of the indenting is lost, so refer to the original code if verifying.

1. Given the following python function definition:

def combine(a, b):
result = 0
while b > 0:
result = result + a
b = b - 1
return result

a. What does combine(3,4) return?
0+a+a+a+a = 3*4 = 12
b. What does combine(6,7) return?
0+6+6+6+6+6+6+6=6*7=42
c. What does combine(3,0) return?
0 of course! It won't enter the 'while' loop is the second input isn't > 0.
d. What mathematical function does combine compute?
a*b! *gasp*


2. Given the following python function definition:

def splitup(a,b):
result = 0
while a >= b:
result = result + 1
a = a - b
return result

a. What does splitup(10,2) return?
0+1+1+1+1+1=5
b. What does splitup(8,2) return?
0+0+0+0+0=4
c. What does splitup(35,5) return?
0+1+1+1+1+1+1=6
d. What mathematical function does splitup compute
a/b *gasp*

3. Given the following python function definitions:

def strange(a):
print "Strange: a = ",a

def weird(a, b):
print "weird: a = ", a, "b = ", b
strange(a+b)

def reallyWeird(a, b):
strange(a - b)
print "reallyWeird: a = ", a, "b = ", b
strange(a+b)

def downrightOdd(a):
print "downrightOdd: a = ", a
reallyWeird(2*a, a)

What is the output of each of the following statements:

a. strange(6)
Strange: a = 6
b. weird(8, 4)
weird: a = 8 b = 4
strange: a = 12
c. reallyWeird(8, 4)
Strange: a = 4
reallyWierd: a = 8 b = 4
Strange: a = 12
d. downrightOdd(3)
downrightOdd: a = 3
Strange: a = 3
reallyWierd: a = 6, b = 3
Strange: a = 9
4 Given the following python function definition:

def odd(a):
result = 0
while a > 1:
a = a / 2
result = result + 1
return result

a. What does odd(2) return?
1
b. What does odd(8) return?
3
EXTRA CREDIT: What mathematical function does odd compute?
2^odd=a

No comments:

Post a Comment