Tuesday, October 27, 2009

Blog 4: Python Output

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?
Answer:12
b. What does combine(6,7) return?
Answer:42
c. What does combine(3,0) return?
Answer:error? Can't enter equation.
d. What mathematical function does combine compute?
Answer:function is a*b
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?
result=5
b. What does splitup(8,2) return?
result=4
c. What does splitup(35,5) return?
result=7
d. What mathematical function does splitup compute
a/b


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)
strange: a =12
c. reallyWeird(8, 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?

No comments:

Post a Comment