Friday, December 4, 2009

Blog 4: Python Output!!

This will go parallel to my previous post, including the math information and solutions. These Python questions are aimed to help in the area of retaining python information for midterm and final exams! So without further introduction, here we go: ...

1.def combine(a, b):
result = 0
while b > 0:
result = result + a
b = b - 1
return result
a. Combine (3, 4) returns 12
b. Combine (6, 7) returns 42
c. Combine (3, 0) returns 0
d. Combine (a, b) returns a x b

2. def splitup(a,b):
result = 0
while a >= b:
result = result + 1
a = a - b
return result
a. Splitup (10, 2) returns 5
b. Splitup (8, 2) returns 4
c. Splitup (35, 5) returns 7
d. Splitup (a, b) returns a / b

3. 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)

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
reallyWeird: a = 8 b = 4
Strange: a = 12

d. downrightOdd(3)
downrightOdd: a = 3
Strange: a = 3
reallyWeird: a = 6 b = 3
Strange: a = 9

4. def odd(a):
result = 0
while a > 1:
a = a / 2
result = result + 1
return result
a. odd(2) = 1
b. odd(8) = 3

No comments:

Post a Comment