lecture3
past learnt
Data - numbers; strings;
operation - +;*;and; or;
statements - assignment;input;output;conditionals条件语句;loop(while);
next we do:
put them together into common templates
good programming style- annotation; check the types before operation; good variable name;
interative program 迭代程序 -步骤
1. choose variable that 'count'
2.initialize it outside the loop
3. set up the right end test
4.construct the block指令集-chang variable
5.what to do when done
例子:求perfect number的平方根
tools:流程图flow charts
可计算流程复杂度
tools:调试代码-手工模拟代码-看执行的结果-寻找bug
1.确保程序可以终结
2.确保程序输出正确的值
防卫性程序
if ans*ans !==x:#!==不等于
print x, 'is not a perfect square'
Exhaustive enumeratio穷尽列举法 - try all 'reasonable' values until you find solution
FOR loop
for <> in <>:
block of code
#程序自动更新变量
for i in range (1,x):
tuple元祖-ordered sequence of elements
test=(1,2,3,4)
test[0]=test[-1]
test[:3] :(1,2,3)
test[2:] #slicing 切片 or selecting
range(1,10) 数组
#把数据集合到一起
x = 100
divisors=()#初始化-元祖-除数
for i in range(1,x):
if x%i =0:
divisors = divisors +(i,)# 把i元祖 加到divisors末尾
#当元组只有一个数字在里面时,应该在数字后面
#加一个逗号,元组里有多个数字时,最后一个数字后面则不需要加逗号
strings also support
sumDigits=0
for c in str(1952):#take the number into the string
sumDigits += int(c) # + and = 缩写
print sumDigits #17
lecture 4
past learnt
we have assignment;conditionals;I/O;looping constructs(for,while)
a language we said was Turing-complete, enough to wirite any program
but not easily
we dont have 1. decompassion-put code into modules模块
2. Abstraction-black box
functions函数- Block uo into modules and supress details and creat new primitives
comment pattern of compution
x=16
ans=0
if x >=0:
while ans*ans <x :
ans =ans +1
print 'ans=' ,ans
if ans*ans !=x :
print x, ' is not a perfect square'
else: print ans
else : print x ,'is a negative number'
key word of function
def # creating a function : name() 形参
return#stop, 将函数的结果return程序
None#special value 无
def sqrx(x) : # sqrx
"""使用说明
ans =0
if x>=0:
while ans*ans <x: ans=ans+1
if ans*ans != x:
print x,'is not a perfect square'
return None #
else: return ans
else:
print x.'is a 。。。'
return None
#invoke调用 function by passing values for the parametes
#sqrt(16): bind x to 16 locally ; ans is only locally局部 donot affect any global binds
interpreter-global binding x=3
call function-local table sqrt(3)
函数包含分解,抽象的思想,规范和约束的概念
鸡兔同笼问题
20 heads and 56 legs
numberP+numC=20
numP*4+numC*2=56
穷举法-loop
def solve(numlegs,numheads) :
for numchicks in range(0,numheads+1):
numpigs=numheads-numchicks
totlegs=4*numpigs+numchicks
if totlegs==numlegs:
return (numpigs,numchicks)
return (None,None)
def solve1(numlegs,numheads) :
for numspiders in range(0,numheads+1):
for numchicks in range(0,numheads+1):#嵌套
numpigs=numheads-numchicks-numspiders
totlegs=4*numpigs+numchicks+8*numspiders
if totlegs==numlegs:
return (numpigs,numchicks,numspiders)
return (None,None,None)
def barnTaed():
heads =int (raw_input(
legs= int(raw_input(
pigs, chickens= solve(legs,heads)
if pigs=None:
print'
else:
prit'',pigs
print'',chickens
def barnTaed1():
heads =int (raw_input(
legs= int(raw_input(
pigs, chickens,spiders= solve(legs,heads)
if pigs=None:
print'
else:
prit'',pigs
print'',chickens
print'',spiders
>>>barnTaed()#running
def solve2(numlegs,numheads) :
solutionFound=False
for numspiders in range(0,numheads+1):
for numchicks in range(0,numheads+1):#嵌套
numpigs=numheads-numchicks-numspiders
totlegs=4*numpigs+numchicks+8*numspiders
if totlegs==numlegs:
print '',+str(numlegs);
print '',+str(numheads);
print '',+str(numspiders);
solutionFound=True
if not solutionFound: print'there is no solution'
recursion递归-base case-simplest
-inductive step-break problem into a simpler version
eg: 是否回文?
def isPalindrome(s,indent):
'''Returns True if s is a palindrome and False otherwise
if len(s) <= 1:return True
else: return s[0]==s[-1] and isPalindrome(s[1:-1])
eg:斐波那契数列 pair(n)=pair=(n-1)+pair(n-2)
def fib(x):
if x==o or x==1: return 1
else: return fib(x-1)+fib(x-2)