cs61A lab01
解锁测试不用伯克利邮箱参考
用到了% // pow() max min 递归 for while
def digit(n, k):"""Return the k-th digit from the right of n for positive integers n and k.>>> digit(3579, 2)5>>> digit(3579, 0)9>>> digit(3579, 10)0"""return n//pow(10,k)%10def middle(a, b, c):"""Return the number among a, b, and c that is not the smallest or largest.Assume a, b, and c are all different numbers.>>> middle(3, 5, 4)4>>> middle(30, 5, 4)5>>> middle(3, 5, 40)5>>> middle(3, 5, 40)5>>> middle(30, 5, 40)30"""return max(min(a,b),min(b,c),min(a,c))def falling(n, k):"""Compute the falling factorial of n to depth k.>>> falling(6, 3) # 6 * 5 * 4120>>> falling(4, 3) # 4 * 3 * 224>>> falling(4, 1) # 44>>> falling(4, 0)1""""*** YOUR CODE HERE ***"if k == 0:return 1else:return n*falling(n-1, k-1)def divisible_by_k(n, k):""">>> a = divisible_by_k(10, 2) # 2, 4, 6, 8, and 10 are divisible by 2246810>>> a5>>> b = divisible_by_k(3, 1) # 1, 2, and 3 are divisible by 1123>>> b3>>> c = divisible_by_k(6, 7) # There are no integers up to 6 that are divisible by 7>>> c0""""*** YOUR CODE HERE ***"c = 0for i in range(1, n+1):if i % k ==0:print(i)c += 1i += 1return cdef sum_digits(y):"""Sum all the digits of y.>>> sum_digits(10) # 1 + 0 = 11>>> sum_digits(4224) # 4 + 2 + 2 + 4 = 1212>>> sum_digits(1234567890)45>>> a = sum_digits(123) # make sure that you are using return rather than print>>> a6""""*** YOUR CODE HERE ***"sum = 0while y:sum += y%10y //=10return sumdef double_eights(n):"""Return true if n has two eights in a row.>>> double_eights(8)False>>> double_eights(88)True>>> double_eights(2882)True>>> double_eights(880088)True>>> double_eights(12345)False>>> double_eights(80808080)False""""*** YOUR CODE HERE ***"panduan = Falsewhile n:if n % 10 == 8:if panduan == True:return Trueelse:panduan = Trueelse:panduan = Falsen //= 10return False