کدهای کلاسی جلسه پایانی
def exp1(a,b):
ans = 1
while (b>0):
ans *= a
b -=1
return ans
def exp2(a,b):
if b==1:
return a
else: return a*exp2(a,b-1)
def exp3(a,b):
if b==1:
return a
if b%2 ==0:
return exp3(a*a,b/2)
else: return a*exp3(a,b-1)
def g (n,m):
x = 0
for i in range(n):
for j in range(m):
x+=1
return x
def Towers(size,fromStack,toStack,sparseStack):
if size == 1:
print 'Move disk from',fromStack,'to',toStack
else:
Towers(size-1,fromStack,sparseStack,toStack)
Towers(1,fromStack,toStack,sparseStack)
Towers(size-1,sparseStack,toStack,fromStack)
def search(s,e):
answer = None
i = 0
numCompares =0
while i numCompares +=1 if e==s[i]: answer = True elif e < s[i]: answer = False i +=1 print answer, numCompares def bsearch(s,e,first,last,calls): print first, last, calls if (last -first)< 2 : return s[first]==e or s[last]==e mid = first + (last-first)/2 if s[mid] == e: return True if s[mid] > e: return bsearch(s,e,first,mid-1,calls+1) else: bsearch(s,e,mid+1,last,calls+1) def search1(s,e): print bsearch(s,e,0,len(s)-1,1) print 'Search Completed!' def testSearch(): s = range(0,1000000) raw_input('basic 1000000') print search(s,1000000) raw_input('binary 1000000') print search1(s,1000000) def selSort(s): for i in range(len(s)): minIdx = i for j in range(i+1,len(s)): if s[j] minIdx = j tmp = s[i] s[i]= s[minIdx] s[minIdx] = tmp return s def BubbleSort(L): for j in range(len(L)): for i in range(len(L)-1): if L[i]>L[i+1]: temp = L[i] L[i]=L[i+1] L[i+1] = temp print L def BubbleSort2(L): while swaped: swaped = False for i in range(len(L)-1): if L[i]>L[i+1]: temp = L[i] L[i]=L[i+1] L[i+1] = temp swaped = True print L def merge(left,right): result = [] i,j = 0,0 while i if left[i] result.append(left[i]) i = i+1 else: result.append(right[j]) j = j + 1 while i result.append(left[i]) i = i+1 while j result.append(right[j]) j = j+1 return result def mergeSort(L): ## print L if len(L)<2: return L[:] else: middle = len(L)/2 left = mergeSort(L[:middle]) right = mergeSort(L[middle:]) together = merge(left,right) ## print 'together',together return together def Max_sub1(L): max_Sum = 0 for i in range(len(L)): for j in range(i,len(L)): this_Sum = 0 for k in range(i,j+1): this_Sum = this_Sum + L[k] if this_Sum > max_Sum: max_Sum = this_Sum return max_Sum def Max_sub2(L): max_Sum = 0 for i in range(len(L)): this_Sum = 0 for j in range(i,len(L)): this_Sum = this_Sum + L[j] if this_Sum > max_Sum: max_Sum = this_Sum return max_Sum def Max_sub3(L,first,last): max_sub,max_perf,max_suf = 0,0,0 if first==last: if L[first]>0: return L[first],L[first],L[first] else: return 0,0,0 mid = int((first+last)/2) max_sub1,max_perf1,max_suf1= Max_sub3(L,first,mid); max_sub2,max_perf2,max_suf2= Max_sub3(L,mid+1,last); max_sub = max(max_sub1,max_sub2,max_suf1+max_perf2) max_perf = 0 this_perf = 0 for i in range(first,last+1): this_perf = this_perf+L[i] if this_perf > max_perf: max_perf = this_perf max_suf = 0 this_suf = 0 for i in range(first,last+1): this_suf = this_suf+L[first+last-i] if this_suf > max_suf: max_suf = this_suf return max_sub,max_perf,max_suf def Max_sub4(L): max_sub,max_suf = 0,0 if len(L)==1: if L[0]>0: return L[0],L[0] else: return 0,0 max_sub1,max_suf1 = Max_sub4(L[0:-1]) max_suf = 0 if max_suf1+L[-1]>max_suf: max_suf = max_suf1+L[-1] max_sub = max(max_sub1,max_suf) return max_sub,max_suf def Max_sub5(L): max_sub, max_suf = 0,0 for i in range(len(L)): max_suf = max(0,max_suf+L[i]); max_sub = max(max_suf,max_sub); return max_sub