1. 练习:递归语法
2. 练习:兔子倍增
题目:
# Rabbits Multiplying
# A (slightly) more realistic model of rabbit multiplication than the Fibonacci
# model, would assume that rabbits eventually die. For this question, some
# rabbits die from month 6 onwards.
#
# Thus, we can model the number of rabbits as:
#
# rabbits(1) = 1 # There is one pair of immature rabbits in Month 1
# rabbits(2) = 1 # There is one pair of mature rabbits in Month 2
#
# For months 3-5:
# Same as Fibonacci model, no rabbits dying yet
# rabbits(n) = rabbits(n - 1) + rabbits(n - 2)
#
#
# For months > 5:
# All the rabbits that are over 5 months old die along with a few others
# so that the number that die is equal to the number alive 5 months ago.
# Before dying, the bunnies reproduce.
# rabbits(n) = rabbits(n - 1) + rabbits(n - 2) - rabbits(n - 5)
#
# This produces the rabbit sequence: 1, 1, 2, 3, 5, 7, 11, 16, 24, 35, 52, ...
#
# Define a procedure rabbits that takes as input a number n, and returns a
# number that is the value of the nth number in the rabbit sequence.
# For example, rabbits(10) -> 35. (It is okay if your procedure takes too
# long to run on inputs above 30.)
def rabbits(n):
#print rabbits(10)
#>>> 35
#s = ""
#for i in range(1,12):
# s = s + str(rabbits(i)) + " "
#print s
#>>> 1 1 2 3 5 7 11 16 24 35 52
我的答案:
def rabbits(n):
if n == 1: return 1
if n == 2: return 1
if 3 <= n <= 5: return rabbits(n - 1) + rabbits(n - 2)
if n > 5: return rabbits(n - 1) + rabbits(n - 2) - rabbits(n - 5)
Peter 的答案:
def rabbits(n):
if n < 1:
return 0
else:
if n == 1 or n == 2:
return 1
else:
return rabbits(n-1) + rabbits(n-2) - rabbits(n-5)
3. 练习:传播优达课程
题目:
# Spreading Udaciousness
# One of our modest goals is to teach everyone in the world to program and
# understand computer science. To estimate how long this will take we have
# developed a (very flawed!) model:
# Everyone answering this question will convince a number, spread, (input to
# the model) of their friends to take the course next offering. This will
# continue, so that all of the newly recruited students, as well as the original
# students, will convince spread of their
# friends to take the following offering of the course.
# recruited friends are unique, so there is no duplication among the newly
# recruited students. Define a procedure, hexes_to_udaciousness(n, spread,
# target), that takes three inputs: the starting number of Udacians, the spread
# rate (how many new friends each Udacian convinces to join each hexamester),
# and the target number, and outputs the number of hexamesters needed to reach
# (or exceed) the target.
# For credit, your procedure must not use: while, for, or import math.
def hexes_to_udaciousness(n, spread, target):
# 0 more needed, since n already exceeds target
#print hexes_to_udaciousness(100000, 2, 36230)
#>>> 0
# after 1 hexamester, there will be 50000 + (50000 * 2) Udacians
#print hexes_to_udaciousness(50000, 2, 150000)
#>>> 1
# need to match or exceed the target
#print hexes_to_udaciousness(50000, 2, 150001)
#>>> 2
# only 12 hexamesters (2 years) to world domination!
#print hexes_to_udaciousness(20000, 2, 7 * 10 ** 9)
#>>> 12
# more friends means faster world domination!
#print hexes_to_udaciousness(15000, 3, 7 * 10 ** 9)
#>>> 10
答案:
4. 练习:深度计数
题目:
# Deep Count
# The built-in len operator outputs the number of top-level elements in a List,
# but not the total number of elements. For this question, your goal is to count
# the total number of elements in a list, including all of the inner lists.
# Define a procedure, deep_count, that takes as input a list, and outputs the
# total number of elements in the list, including all elements in lists that it
# contains.
# For this procedure, you will need a way to test if a value is a list. We have
# provided a procedure, is_list(p) that does this:
def is_list(p):
return isinstance(p, list)
# It is not necessary to understand how is_list works. It returns True if the
# input is a List, and returns False otherwise.
def deep_count(p):
#print deep_count([1, 2, 3])
#>>> 3
# The empty list still counts as an element of the outer list
#print deep_count([1, [], 3])
#>>> 3
#print deep_count([1, [1, 2, [3, 4]]])
#>>> 7
#print deep_count([[[[[[[[1, 2, 3]]]]]]]])
#>>> 10
(我没有更进一步的思路了,这是我的不完整的代码:
def deep_count(p):
count = 0
for e in p:
# 如果 e 是列表
if isinstance(e, list):
count += 1
# 这里应该再加 1 个 for 循环 for f in e ,继续判断 f 元素是否为列表类型,但是,这样的话,还要继续判断内层循环的元素是不是列表,就没完没了了,所以我写不下去了。
else:
count += 1
)
Peter 的答案:
(正好解决了我的问题,利用了递归。)
def deep_count(p):
sum = 0
for e in p:
sum = sum + 1
if is_list(e):
sum = sum + deep_count(e)
return sum