Lecture 4: Decomposition and abstraction through functions; introduction to recursion
来源:互联网 发布:阿里云cdn加速如何使用 编辑:程序博客网 时间:2024/05/17 00:38
# example code, Lecture 4, Fall 2008def sqrt(x): ans = 0 if x >= 0: while ans*ans < x: ans = ans + 1 if ans*ans != x: print (x, 'if not a prefect square') return None else: return ans else: print(x, 'is a negative number') return Nonedef f(x): x = x + 1 return xdef solve(numLegs, numHeads): for numChicks in range(0, numHeads + 1): numPigs = numHeads - numChicks totLegs = 4*numPigs + 2*numChicks if totLegs == numLegs: return [numPigs, numChicks] return [None, None]def barnYard(): heads = int(input('Enter number of heads:')) legs = int(input('Enter umber of legs:')) pigs, chickens = solve(legs, heads) if pigs == None: print ('There is no solution') else: print ('Number of pigs', pigs) print ('Number of chickens', chickens)def solve1(numLegs, numHeads): 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(input('Enter number of heads:')) legs = int(input('Enter umber of legs:')) pigs, chickens, spiders = solve1(legs, heads) if pigs == None: print ('There is no solution') else: print ('Number of pigs', pigs) print ('Number of chickens', chickens) print ('Number of spiders', 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 ('Number of pigs' + str(numPigs) + '.') print ('Number of chickens' + str(numChicks) + '.') print ('Number of spiders' + str(numSpiders)) solutionFound = True if not solutionFound: print ('There is no solution')def isPalindrome(s): if len(s) <= 1: return True else: return s[0] == s[-1] and isPalindrome(s[1:-1])def isPalindrome1(s,indent): print (indent, 'ispalindrome called with', s) if len(s) <= 1: print (indent, 'About to return True from base case') return True else: ans = s[0] == s[-1] and isPalindrome1(s[1:-1], indent + indent) print (indent, 'About to return', ans) return ansdef fib(x): if x == 0 or x == 1: return 1 else: return fib(x-1) + fib(x-2)
阅读全文
0 0
- Lecture 4: Decomposition and abstraction through functions; introduction to recursion
- Learning with python: Chapter3~4 Functions,Conditionals and Recursion
- An Introduction to Recursion笔记
- INTRODUCTION TO JavaScript Functions
- 5.1 - Introduction to Functions
- Introduction to Statistics Through Resampling Methods and Microsoft Office Excel
- 【CS231n winter2016 Lecture 4 (Backpropagation ,Introduction to neural networks)】
- 6.00 Introduction to Computer Science and Programming Lec 9: Lecture 9: Memory and Search Methods
- An Introduction to Recursion, Part 1
- An Introduction to Recursion, Part 2
- An Introduction to Recursion, Part 2
- Lecture 1: Introduction, complexity of algorithms, asymptotic growth of functions.
- Lecture 7_1: Lists and mutability, dictionaries, pseudocode, introduction to efficiency
- Lecture 7_2: Lists and mutability, dictionaries, pseudocode, introduction to efficiency
- Introduction to Linear Algebra Lecture 1
- An Introduction to Dynamic Management Views and Functions in SQL Server 2005
- Lecture 10: Recursion
- Swift from Scratch: An Introduction to Functions
- 表关系多对多,1对多,1对1 映射到数据库时出现的一些异常(重要)
- HDFS 常用shell命令
- 那些设计出来就不希望别人看懂的C代码——IOCCC国际模糊C代码大赛
- Java数据结构和算法(二)——数组
- 92. Reverse Linked List II
- Lecture 4: Decomposition and abstraction through functions; introduction to recursion
- Redis 字符串(String)
- 重新开始写字的一点感想
- Lecture 6: Bisection methods, Newton/Raphson, introduction to lists
- C++ const 的使用
- Hadoop分布式集群搭建
- 数组、链表、堆栈和队列
- Lecture 7_1: Lists and mutability, dictionaries, pseudocode, introduction to efficiency
- linux系统编程-epoll