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
Wednesday, October 28, 2009
Monday, October 5, 2009
Math!
1 - Convert the following binary numbers to both hexadecimal (base-16) and decimal (base-10)
a) 1011 = 8+0+2+1 = 11 (dec) = b (hex)
b) 10101 = 16+0+4+0+1 = 21 (dec) = 2a (hex)
c) 10010110 = 124+0+0+16+0+4+2+0 = 146 (dec) = 96 (hex)
d) 1111111 = 64+32+16+8+4+2+1 = 145 (dec) = 7f (hex)
2 - Convert the following decimal numbers to both binary (base-2) and hexadecimal (base-16)
a) 8 = 8+0+0+0 = 1000 (bin) = 8 (hex)
b) 63 = 32+16+8+4+2+1 = 111111 (bin) = 3F (hex)
c) 113 = 64+32+16+0+0+0+1 = 1110001 (bin) = 71 (hex)
d) 97 = 64+32+0+0+0+0+1 = 1100001 (bin) = 61 (hex)
3 - Suppose that we want to download a 200 MB file. How long will it take on each of the following connections?
a) A modem that can download at 56 kilobits/second = 7h 56m
b) A DSL connection that can download at 5 Megabits/second =
c) A high-speed connection that can download 10 Megabits/second = 2m 40s
4 - If we have an Internet connection that can upload 3 Megabits/second, and an MP3 is 60 Megabytes, how many MP3s can we upload in an hour? In a month (given a reliable connection)? 2m 40s per upload = 22.5 per hour = 16200 per month (30 days)
a) 1011 = 8+0+2+1 = 11 (dec) = b (hex)
b) 10101 = 16+0+4+0+1 = 21 (dec) = 2a (hex)
c) 10010110 = 124+0+0+16+0+4+2+0 = 146 (dec) = 96 (hex)
d) 1111111 = 64+32+16+8+4+2+1 = 145 (dec) = 7f (hex)
2 - Convert the following decimal numbers to both binary (base-2) and hexadecimal (base-16)
a) 8 = 8+0+0+0 = 1000 (bin) = 8 (hex)
b) 63 = 32+16+8+4+2+1 = 111111 (bin) = 3F (hex)
c) 113 = 64+32+16+0+0+0+1 = 1110001 (bin) = 71 (hex)
d) 97 = 64+32+0+0+0+0+1 = 1100001 (bin) = 61 (hex)
3 - Suppose that we want to download a 200 MB file. How long will it take on each of the following connections?
a) A modem that can download at 56 kilobits/second = 7h 56m
b) A DSL connection that can download at 5 Megabits/second =
c) A high-speed connection that can download 10 Megabits/second = 2m 40s
4 - If we have an Internet connection that can upload 3 Megabits/second, and an MP3 is 60 Megabytes, how many MP3s can we upload in an hour? In a month (given a reliable connection)? 2m 40s per upload = 22.5 per hour = 16200 per month (30 days)
Subscribe to:
Comments (Atom)