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? 3+3+3+3=12
b. What does combine(6,7) return? 6+6+6+6+6+6+6=42
c. What does combine(3,0) return? 0
d. What mathematical function does combine compute? it is essentially a (a*b) function, but it will only work for (b) values that are positive (the function (3,-4) will return 0)
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? 5
b. What does splitup(8,2) return? 4
c. What does splitup(35,5) return? 7
d. What mathematical function does splitup compute? it is essentially a divide function, (a/b), for every time b goes into a, the result is increased by 1, this only works for numbers that go fully into one another (for example 22,7 will only return 3 when the answer is 3.1428)
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+b = 12"
c. reallyWeird(8, 4) = "Strange: a-b = 4 reallyWeird: a = 8 b = 4 Strange: a+b = 12"
d. downrightOdd(3) = "downrightOdd: a = 3 Strange: a-b = 3 reallyWeird: a = 6 b = 3 Strange: a+b = 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? 4
EXTRA CREDIT: What mathematical function does odd compute? it divides (a) by 2 and adds 1 to the result continuously until (a) is less than or equal to 1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment