def move(n, a, b, c):
if n==1:
print a,'-->',c
else:
move(n-1,a,c,b)
move(1,a,b,c)
move(n-1,b,a,c)
move(4, 'A', 'B', 'C')
思考的过程比较痛苦,之前一直在具体的步骤中找规律,后来转变思维,发现可以将A,B,C位置互换,问题迎刃而解
if n==1:
print a,'-->',c
else:
move(n-1,a,c,b)
move(1,a,b,c)
move(n-1,b,a,c)
move(4, 'A', 'B', 'C')
思考的过程比较痛苦,之前一直在具体的步骤中找规律,后来转变思维,发现可以将A,B,C位置互换,问题迎刃而解
2015-07-02
age = 8
if age >= 6:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'
当 age = 8 时,结果正确,但 age = 20 时,为什么没有打印出 adult?
如果要修复,应该如何修复?
回答:因为age=8已经满足了第一个teenager的条件,直接就显示的teenager,不会再往下走了。所以不会出现adult
if age >= 6:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'
当 age = 8 时,结果正确,但 age = 20 时,为什么没有打印出 adult?
如果要修复,应该如何修复?
回答:因为age=8已经满足了第一个teenager的条件,直接就显示的teenager,不会再往下走了。所以不会出现adult
2015-07-02
print [int(m+n+m) for m in '123456789' for n in '0123456789']
2015-07-01
def move(n, a, b, c):
if n > 1 :
print a,'-->',b
else:
print a,'-->',c
print b,'-->',c
n = n-1;
move(n,a,b,c);
move(4, 'A', 'B', 'C')
if n > 1 :
print a,'-->',b
else:
print a,'-->',c
print b,'-->',c
n = n-1;
move(n,a,b,c);
move(4, 'A', 'B', 'C')
2015-07-01
def square_of_sum(L):
s=0
for x in L:
s=s+x*x
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
s=0
for x in L:
s=s+x*x
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-06-30
import math
def quadratic_equation(a, b, c):
y = math.sqrt(b * b - 4 * a * c)
x1 = (-b + y) / (2 * a)
x2 = (-b - y) / (2 * a)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
y = math.sqrt(b * b - 4 * a * c)
x1 = (-b + y) / (2 * a)
x2 = (-b - y) / (2 * a)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2015-06-30
sum = 0
x = 0
while True:
x = x + 1
if x%2==0:
continue
if x > 100:
break
sum=sum+x
print sum
x = 0
while True:
x = x + 1
if x%2==0:
continue
if x > 100:
break
sum=sum+x
print sum
2015-06-30
def square_of_sum(L):
return sum([x**2 for x in L])
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
return sum([x**2 for x in L])
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-06-30