作业链接
Q1: Num eights
Important:
1.Use recursion
2.No assignment statements
def num_eights(pos):
"""Returns the number of times 8 appears as a digit of pos.
>>> num_eights(3)
0
>>> num_eights(8)
1
>>> num_eights(88888888)
8
>>> num_eights(2638)
1
>>> num_eights(86380)
2
>>> num_eights(12345)
0
>>> from construct_check import check
>>> # ban all assignment statements
>>> check(HW_SOURCE_FILE, 'num_eights',
... ['Assign', 'AnnAssign', 'AugAssign', 'NamedExpr'])
True
"""
if pos < 10:
if pos == 8:
return 1
else:
return 0
return num_eights(pos // 10) + num_eights(pos % 10)
Q2: Ping-pong
Important:
1.Use recursion
2.No assignment statements
def pingpong(n):
"""Return the nth element of the ping-pong sequence.
>>> pingpong(8)
8
>>> pingpong(10)
6
>>> pingpong(15)
1
>>> pingpong(21)
-1
>>> pingpong(22)
-2
>>> pingpong(30)
-2
>>> pingpong(68)
0
>>> pingpong(69)
-1
>>> pingpong(80)
0
>>> pingpong(81)
1
>>> pingpong(82)
0
>>> pingpong(100)
-6
>>> from construct_check import check
>>> # ban assignment statements
>>> check(HW_SOURCE_FILE, 'pingpong',
... ['Assign', 'AnnAssign', 'AugAssign', 'NamedExpr'])
True
"""
def pingpong_times(n):
return n // 8 + sum_num_eights(n)
def sum_num_eights(n):
if n < 8:
return 0
if num_eights(n) != 0:
if n % 8 == 0:
return 0 + sum_num_eights(n-1)
return 1 + sum_num_eights(n-1)
else:
return 0 + sum_num_eights(n-1)
if n <= 8:
return n
if pingpong_times(n) % 2 == 1 :
if n % 8 == 0 or num_eights(n) != 0:
return pingpong(n-1) + 1# minus
return pingpong(n-1) - 1
if pingpong_times(n) % 2 == 0:
if n % 8 == 0 or num_eights(n) != 0:
return pingpong(n-1) - 1# add
return pingpong(n-1) + 1
Q3: Missing Digits
Important:
1.Use recursion
2.No loops
def missing_digits(n):
"""Given a number a that is in sorted, non-decreasing order,
return the number of missing digits in n. A missing digit is
a number between the first and last digit of a that is not in n.
>>> missing_digits(1248) # 3, 5, 6, 7
4
>>> missing_digits(19) # 2, 3, 4, 5, 6, 7, 8
7
>>> missing_digits(1122) # No missing numbers
0
>>> missing_digits(123456) # No missing numbers
0
>>> missing_digits(3558) # 4, 6, 7
3
>>> missing_digits(35578) # 4, 6
2
>>> missing_digits(12456) # 3
1
>>> missing_digits(16789) # 2, 3, 4, 5
4
>>> missing_digits(4) # No missing numbers between 4 and 4
0
>>> from construct_check import check
>>> # ban while or for loops
>>> check(HW_SOURCE_FILE, 'missing_digits', ['While', 'For'])
True
"""
def missing_num(x,y):
if x == y or x == y + 1:
return 0
else:
return x - y - 1
if n < 10:
return 0
if n < 100:
return missing_num(n % 10, n // 10 % 10)
return missing_digits(n // 10) + missing_num(n % 10, n // 10 % 10)
Q4: Count coins
Important:
1.Use recursion
2.No loops
def ascending_coin(coin):
"""Returns the next ascending coin in order.
>>> ascending_coin(1)
5
>>> ascending_coin(5)
10
>>> ascending_coin(10)
25
>>> ascending_coin(2) # Other values return None
"""
if coin == 1:
return 5
elif coin == 5:
return 10
elif coin == 10:
return 25
def count_coins(change):
"""Return the number of ways to make change using coins of value of 1, 5, 10, 25.
>>> count_coins(15)
6
>>> count_coins(10)
4
>>> count_coins(20)
9
>>> count_coins(100) # How many ways to make change for a dollar?
242
>>> count_coins(200)
1463
>>> from construct_check import check
>>> # ban iteration
>>> check(HW_SOURCE_FILE, 'count_coins', ['While', 'For'])
True
"""
def count_coins1(change, i):
"""Return the number of ways to make change using coins of i."""
if change == 0:#finish the partition
return 1
elif change <= 0 or i == None:#partition error or no more coins
return 0
return count_coins1(change-i , i) + count_coins1(change, ascending_coin(i))
return count_coins1(change, 1)