高级农民
- 积分
- 2019
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-1-22
- 最后登录
- 1970-1-1
|
交一下lab02的作业:
然后是代码:- """Required questions for lab 2"""
- ## Boolean Operators ##
- # Q6
- def both_positive(x, y):
- """
- Returns True if both x and y are positive.
- >>> both_positive(-1, 1)
- False
- >>> both_positive(1, 1)
- True
- """
- "*** YOUR CODE HERE ***"
- return x > 0 and y > 0
- ## while Loops ##
- # Q9
- def factors(n):
- """Prints out all of the numbers that divide `n` evenly.
- >>> factors(20)
- 20
- 10
- 5
- 4
- 2
- 1
- """
- "*** YOUR CODE HERE ***"
- x = n
- while x > 0:
- if n % x == 0:
- print(x)
- x -= 1
- # Q10
- 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 ***"
- cur, next = 0, 1
- while n > 0:
- cur, next = next, cur + next
- n -= 1
- return cur
复制代码- """Extra questions for lab 2."""
- # Q11
- 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 ***"
- if max(x, y) >= 65 and min(x, y) <= 12:
- return True
- return False
- # Q12
- 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
- """
- "*** YOUR CODE HERE ***"
- if x == 0:
- return False
- if y == 0:
- return True
- return (y % x == 0)
- # Q13
- 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 ***"
- i = 0
- res = 1
- while i < k:
- res *= n
- n -= 1
- i += 1
- return res
复制代码 |
|