-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLec04.py
More file actions
133 lines (116 loc) · 3.89 KB
/
Copy pathLec04.py
File metadata and controls
133 lines (116 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# MIT600《计算机科学及编程导论》(2008年秋季)样码
# 第四讲:函数抽象及递归介绍
# 翻译制作:ocourse.org
# 课程讨论版:http://ocourse.org/bbs/forum.php?mod=forumdisplay&fid=29
# by yoeo24
##x = 16
##ans = 0
##if x >= 0:
## while ans*ans < x:
## ans = ans +1
## #print 'ans = ',ans
## if ans*ans != x:
## print x,' 不是完全平方数'
## else: print ans
##else: print x,' 是负数'
def sqrt(x):
"""Returns the square root of x, if x is a perfect square.
Prints an error message and returns None otherwise"""
#hello如果x是完全平方数,返回x的平方根;否则打印错误信息,并无返回值
ans = 0
if x>= 0:
while ans*ans < x: ans = ans + 1
if ans*ans != x:
print x, ' 不是完全平方数'
return None
else: return ans
else:
print x, ' 是负数'
return None
def f(x):
x = x + 1
return x
##x = 3
##z = f(x)
##print x
##print z
##ans = 15
##sqrt(25)
##ans
def solve(numLegs, numHeads):
#solve解,num表示number(数目),tot表示total(总数),leg脚、head头、pig猪、chick鸡
for numChicks in range(0, numHeads + 1):
numPigs = numHeads - numChicks
totLegs = 4*numPigs + 2*numChicks
if totLegs == numLegs:
return (numPigs, numChicks)
return None
def barnYard():
#barnyard农场
heads = int(raw_input('输入头的个数:'))
legs = int(raw_input('输入脚的个数:'))
pigs, chickens = solve(legs, heads)
if pigs == None:
print '无解'
else:
print '猪的数目是:', pigs
print '鸡的数目是:', chickens
def solve1(numLegs, numHeads):
#spider蜘蛛
for numSpiders in range(0, numHeads + 1):
for numChicks in range(0, numHeads - numSpiders + 1):
numPigs = numHeads - numChicks - numSpiders
totLegs =4*numPigs + 2*numChicks + 8*numSpiders
if totLegs == numLegs:
return [numPigs, numChicks, numSpiders]
return [None, None, None]
def barnYard1():
heads = int(raw_input('输入头的个数:'))
legs = int(raw_input('输入脚的个数:'))
pigs, chickens, spiders = solve1(legs, heads)
if pigs == None:
print '无解'
else:
print '猪的数目是:', pigs
print '鸡的数目是:', chickens
print '蜘蛛的数目是:', spiders
def solve2(numLegs, numHeads):
solutionFound = False
for numSpiders in range(0, numHeads + 1):
for numChicks in range(0, numHeads - numSpiders + 1):
numPigs = numHeads - numChicks - numSpiders
totLegs =4*numPigs + 2*numChicks + 8*numSpiders
if totLegs == numLegs:
print '猪的数目是:' + str(numPigs) + ',',
print '鸡的数目是:' + str(numChicks) + ',',
print '蜘蛛的数目是:', numSpiders
solutionFound = True
if not solutionFound: print '无解'
###test function barnYard2 for solve2 added by ocourse.org
###头数目10,腿数目30可以得到两个解
def barnYard2():
heads = int(raw_input('输入头的个数:'))
legs = int(raw_input('输入脚的个数:'))
solve2(legs, heads)
def isPalindrome(s):
"""Return True if s is a palindrome and False otherwise"""
#如果是回文(palindrome),返回True;否则返回False
if len(s) <=1: return True
else: return s[0] == s[-1] and isPalindrome(s[1:-1])
def isPalindrome1(s, indent):
"""Return True if s is a palindrome and False otherwise"""
#indent 缩进
print indent*' ', 'isPalindrome1调用', s
if len(s) <= 1:
print indent*' ', '准备从基础情况返回True'
return True
else:
ans = s[0] == s[-1] and isPalindrome1(s[1:-1], indent + indent)
print indent*' ', '准备返回 ', ans
return ans
def fib(x):
"""Return Fibonacci of x, where x is a non-negative int"""
#返回x时的斐波那契数,其中x是非负整数
if x == 0 or x == 1: return 1
else: return fib(x-1) + fib(x-2)
#此法效率很低,算fib(36)就挂了,为什么?是否有什么高效一些的算法呢?