新农上路
- 积分
- 92
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-2-15
- 最后登录
- 1970-1-1
|
# Question 9
def factors(n):
"""print out all the number that divide 'n' evenly.
>>> factors(20)
20
10
5
4
2
1
"""
OriNum = n
while n>0:
if OriNum%n == 0:
print(n)
n-=1
# Question 10
def fib(n):
"""Returns the nth Fibonacci number.
>>> fib(0)
0
>>> fib(1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5
>>> fib(6)
8
"""
"*** YOUR CODE HERE ***"
last1, last2=1, 0
while n>0:
last2, last1 = last1, last1+last2
n-=1
print(last2)
# Question 11
def gets_discount(x, y):
""" Returns True if this is a combination of a senior citizen
and a child, False otherwise.
>>> gets_discount(65, 12)
True
>>> gets_discount(9, 70)
True
>>> gets_discount(40, 45)
False
>>> gets_discount(40, 75)
False
>>> gets_discount(65, 13)
False
>>> gets_discount(7, 9)
False
>>> gets_discount(73, 77)
False
>>> gets_discount(70, 31)
False
>>> gets_discount(10, 25)
False
"""
"*** YOUR CODE HERE ***"
return (x >= 65 and y <= 12) or \
(y >= 65 and x <= 12)
# Question 12
def is_factor(x, y):
""" Returns True if x is a factor of y, False otherwise.
>>> is_factor(3, 6)
True
>>> is_factor(4, 10)
False
>>> is_factor(0, 5)
False
>>> is_factor(0, 0)
False
>>> is_factor(5, 0)
True
"""
"*** YOUR CODE HERE ***"
return x!=0 and not y%x
# Question 13
def falling(n, k):
"""Compute the falling factorial of n to depth k.
>>> falling(6, 3) # 6 * 5 * 4
120
>>> falling(4, 0)
1
>>> falling(4, 3) # 4 * 3 * 2
24
>>> falling(4, 1) # 4
4
"""
"*** YOUR CODE HERE ***"
Outcome = 1
while k>0:
Outcome*= n
n-= 1
k-= 1
print(Outcome)
#quiz1
def harmonic(x, y):
"""Return the harmonic mean of x and y.
>>> harmonic(2, 6)
3.0
>>> harmonic(1, 1)
1.0
>>> harmonic(2.5, 7.5)
3.75
"""
"*** YOUR CODE HERE ***"
return 2/(1/x+1/y)
#quiz2
from math import pi
def pi_fraction(gap):
"""Print the fraction within gap of pi that has the smallest denominator.
>>> pi_fraction(0.01)
22 / 7 = 3.142857142857143
>>> pi_fraction(1)
3 / 1 = 3.0
>>> pi_fraction(1/8)
13 / 4 = 3.25
>>> pi_fraction(1e-6)
355 / 113 = 3.1415929203539825
"""
numerator, denominator = 3, 1
"*** YOUR CODE HERE ***"
"*** YOUR CODE HERE ***"
while True:
numerator = round(denominator * pi)
if abs(numerator/denominator-pi) < gap:
print(numerator, '/', denominator, '=', \
numerator/denominator)
return
denominator+=1
#quiz3
def nearest_two(x):
"""Return the power of two that is nearest to x.
>>> nearest_two(8) # 2 * 2 * 2 is 8
8.0
>>> nearest_two(11.5) # 11.5 is closer to 8 than 16
8.0
>>> nearest_two(14) # 14 is closer to 16 than 8
16.0
>>> nearest_two(2015)
2048.0
>>> nearest_two(.1)
0.125
>>> nearest_two(0.75) # Tie between 1/2 and 1
1.0
>>> nearest_two(1.5) # Tie between 1 and 2
2.0
"""
power_of_two = 1.0
"*** YOUR CODE HERE ***"
def nearest(x,power_of_two, power_of_two_last):
if abs(power_of_two-x)>abs(x-power_of_two_last):
return power_of_two_last
elif abs(power_of_two-x)==abs(x-power_of_two_last):
return max(power_of_two,power_of_two_last)
else:
return power_of_two
if x>=1:
while power_of_two <= x:
power_of_two*= 2
return nearest(x,power_of_two,power_of_two/2)
else:
while power_of_two >= x:
power_of_two/= 2
return nearest(x,power_of_two,power_of_two*2) |
|