1
2
3
4
4
5
2021
number // 10 number % 10
5
def sum_digits(n):
“””Return the sum of the digits of positive integer n. >>> sum_digits(6)
6
>>> sum_digits(2021)
5
“””
6
def sum_digits(n):
“””Return the sum of the digits of positive integer n. >>> sum_digits(6)
6
>>> sum_digits(2021)
5
“””
if n < 10:
return n else:
all_but_last = n // 10
last = n % 10
return sum_digits(all_but_last) + last
6
def sum_digits(n): if n < 10:
return n else:
all_but_last = n // 10
last = n % 10
return sum_digits(all_but_last) + last
7
def sum_digits(n):
if n < 10: # BASE CASE
return n else:
all_but_last = n // 10
last = n % 10
return sum_digits(all_but_last) + last
7
def sum_digits(n):
if n < 10: # BASE CASE
return n
else: # RECURSIVE CASE
all_but_last = n // 10
last = n % 10
return sum_digits(all_but_last) + last
7
8
def fact(n): """
>>> fact(0)
1
>>> fact(4)
24
“””
9
def fact(n): “””
>>> fact(0)
1
>>> fact(4)
24
“””
if n == 0: return 1
else:
return n * fact(n-1)
9
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact(3)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact(3)
fact(2)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact(3)
fact(2)
fact(1)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact(3)
fact(2)
fact(1)
fact(0)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact(3)
fact(2)
fact(1)
1 fact(0)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact(3)
fact(2)
1 fact(1)
1 fact(0)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact(3)
2 fact(2)
1 fact(1)
1 fact(0)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
6 fact(3)
2 fact(2)
1 fact(1)
1 fact(0)
10
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact fact
n
n
Global frame
fact ¡ú func fact[parent=Global]
f1:
Return
value f2:
Return value
f3:
Return value
f4:
11
def fact(n): if n == 0:
return 1 else:
return n * fact(n-1) fact(3)
fact fact
n
n
Global frame
fact ¡ú func fact[parent=Global]
f1: fact[parent=Global] n3
Return 6
value
f2: fact[parent=Global] n2
Return 2
value
f3: fact[parent=Global] n1
Return 1
value
f4: fact[parent=Global] n0
Return 1
value
11
12
13
13
def fact(n):
“””Returns the factorial of N.””” if n == 0:
return 1 else:
return n * fact(n-1) fact
fact
fact(n-1) ¡û
fact(n)
14
15
15
¡â fact()
15
16
17
17
17
17
17
17
17
17
def sum_digits(n): if n < 10:
return n else:
last = n % 10
all_but_last = n // 10
return last + sum_digits(all_but_last)
def luhn_sum(n):
"""Returns the Luhn sum for the positive number N. >>> luhn_sum(2)
2
>>> luhn_sum(32)
8
>>> luhn_sum(5105105105105100)
20
“””
18
def luhn_sum(n): if n < 10:
return n else:
last = n % 10
all_but_last = n // 10
return last + luhn_sum_double(all_but_last)
def luhn_sum_double(n): last = n % 10
all_but_last = n // 10 luhn_digit = sum_digits(last * 2) if n < 10:
return luhn_digit else:
return luhn_digit + luhn_sum(all_but_last)
19
20
def fact(n): if n == 0: return 1
else:
return n * fact(n-1)
def fact(n): total = 1
k = 1
while k <= n:
total *= k
k += 1 return total
fact n
fact n total k
21
def sum_digits(n): if n < 10:
return n else:
all_but_last = n // 10
last = n % 10
return sum_digits( all_but_last ) + last
22
def sum_digits(n): if n < 10:
return n else:
all_but_last = n // 10
last = n % 10
return sum_digits( all_but_last ) + last
def sum_digits(n): digit_sum = 0
while n >= 10: last = n % 10
n = n // 10
digit_sum += last return digit_sum
22
def sum_digits(n): digit_sum = 0
while n >= 10: last = n % 10
n = n // 10
digit_sum += last return digit_sum
23
def sum_digits(n): digit_sum = 0
while n >= 10: last = n % 10
n = n // 10
digit_sum += last return digit_sum
def sum_digits(n, digit_sum): if n == 0:
return digit_sum else:
last = n % 10
all_but_last = n // 10
return sum_digits( all_but_last, digit_sum + last )
23