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?

Monday, October 5, 2009

Math

Convert the following binary numbers to both hexadecimal (base-16) and decimal (base-10)
1.
a. 1011 Hexadecimal-B Decimal-11
b. 10101 Hexadecimal-15 Decimal-21
c. 10010110 Hexadecimal-96 Decimal-150
d. 1111111 Hexadecimal-7f Decimal-27

Convert the following decimal numbers to both binary (base-2) and hexadecimal (base-16)
2.
a. Decimal-8 Hexadecimal-8 Binary-1000
b. Decimal-63 Hexadecimal-3F Binary-111111
c. Decimal-113 Hexadecimal-71 Binary-1110001
d. Decimal-97 Hexadecimal-61 Binary-1100001

3.
Suppose that we want to download a 200 MB file. How long will it take on each of the following connections?
a.56 kilobits/s
1638400 bits/56 bits per second
=
29257.14 seconds
8 hours, 7 minutes, 31 minutes

b.5 megabits/s
8*200mb=1600megabits/s
1600/5=320 seconds
5min and 20 seconds

c.10 megabits/s
1600megabits per second/10megabits per second=160 seconds
2 minutes and 40 seconds

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)?
.375 megabytes per second*60 megabytes=22.5 seconds for mp3 to be uploaded
3600/22.5=160 songs in an hour
2629743.83/22.5=
116,877.5 songs in a month