کدهای کلاسی پنجم
##x = 16
##ans = 0
##if x>=0:
## while ans*ans ## 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' ##def sqrt(x): ## ''' return the squre root of x, if x is a perfect ## square. print the error message and return None otherwise ''' ## ## ans = 0 ## if x>=0: ## while ans*ans ## ans = ans + 1 ## ## if ans * ans !=x: ## print x,'is not a perfect square!' ## return None ## else: return ans ## else: ## print x , 'is a negative number' ## return None ## #### local binding vs global binding ##def f(x): ## x = x+1 ## return x ##def solve(numHeads,numLegs): ## for numChicks in range(0,numHeads + 1): ## numSheeps = numHeads - numChicks ## totLegs = 4*numSheeps + 2* numChicks ## if totLegs == numLegs: ## return[numSheeps,numChicks] ## return [None,None] ## ##def barnYard(): ## heads = int(raw_input('Enter number of heads: ')) ## legs = int(raw_input('Enter number of legs: ')) ## sheeps, chickens = solve(heads,legs) ## if sheeps==None: ## print 'your problem is not solvable!' ## else: ## print 'Number of sheeps:',sheeps ## print 'Number of chicks:',chickens ## ## ## ##def solve1(numHeads,numLegs): ## for numChicks in range(0,numHeads + 1): ## for numSheeps in range(0,numHeads-numChicks+1): ## numSpiders = numHeads - numChicks - numSheeps ## totLegs = 6*numSpiders + 4*numSheeps + 2* numChicks ## if totLegs == numLegs: ## return[numSheeps,numChicks,numSpiders] ## return [None,None,None] ##def barnYard1(): ## '''return nothing and gets nothing''' ## heads = int(raw_input('Enter number of heads: ')) ## legs = int(raw_input('Enter number of legs: ')) ## sheeps, chickens, spiders = solve1(heads,legs) ## if sheeps==None: ## print 'There is no solution!' ## else: ## print 'Number of sheeps:',sheeps ## print 'Number of chicks:',chickens ## print 'Number of spiders:',spiders #### solve2(legs,heads) ## ##def solve2(numLegs,numHeads): ## solutionFound = False ## for numChicks in range(0,numHeads + 1): ## for numSheeps in range(0,numHeads-numChicks+1): ## numSpiders = numHeads - numChicks - numSheeps ## totLegs = 6*numSpiders + 4*numSheeps + 2* numChicks ## if totLegs == numLegs: ## print 'Number of sheeps:',numSheeps ## print 'Number of chicks:',numChicks ## print 'Number of spiders:',numSpiders ## solutionFound = True ## if not solutionFound: print 'There is no solution' ## ## ##def integral(n): ## sum = 0 ## for i in range(1,n+1): ## sum +=i ## return sum ## ##def integral2(n): ## if n==1: ## return(1) ## else: ## return integral2(n-1)+n ## ## def isPalindrome(s): '''return True if s is palindrome and False otherwise''' if len(s)<=1: return True else: return s[0]==s[-1] and isPalindrome(s[1:-1]) ## ## ##def fib(x): ## if x ==0 or x ==1: return(x) ## else: return fib(x-1) + fib(x-2)