南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)07-Recursion Examples

Recursion Examples
Recursion Examples

Recursion (review)
Recursion (review)

Recursion (review) Scenario:You are waiting in line for a concert.You can't see the front of the line,but you want to know your place in the line. The person at the front,knows they are at the front! Base case You ask the person in front of you:"what is your Recursive call place in the line?” When the person in front of you figures it out and Use the solution to tells you,add one to that answer. the smaller problem
Recursion (review) Scenario: You are waiting in line for a concert. You can't see the front of the line, but you want to know your place in the line. Base case Recursive call Use the solution to the smaller problem You ask the person in front of you: “what is your place in the line?” When the person in front of you figures it out and tells you, add one to that answer. The person at the front, knows they are at the front!

Iteration vs.Recursion Iteration and recursion are somewhat related ● Converting iteration to recursion is formulaic,but converting recursion to iteration can be more tricky Iterative Recursive def fact_iter(n): deffact(n): total,k=1,1 ifn==0: whilek <n: return 1 total,k total*k,k+1 else: return total return n*fact(n-1)】 m n!=Ik nn(n-1)!otiherwise if n 0 k=1 Names:n,total,k,fact_iter Names:n,fact
Iteration vs. Recursion ● Iteration and recursion are somewhat related ● Converting iteration to recursion is formulaic, but converting recursion to iteration can be more tricky def fact_iter(n): total, k = 1, 1 while k <= n: total, k = total*k, k+1 return total def fact(n): if n == 0: return 1 else: return n * fact(n-1) Iterative Recursive Names: n, total, k, fact_iter Names: n, fact

Sum Digits Let's implement a recursive function to sum all the digits of 'n'. Assume 'n'is positive. def sum_digits(n): """Calculates the sum of the digits 'n'. 1.One or more base >>sum_digits(8) cases 8 sum_digits(18) 2.One or more >>> 9 recursive calls >>> sum_digits(2018) with simpler 11 arguments. I 1 II ”大*火 YOUR CODE HERE 3.Using the +*★” recursive call to solve our larger problem
Sum Digits Let’s implement a recursive function to sum all the digits of `n`. Assume `n` is positive. def sum_digits(n): """Calculates the sum of the digits `n`. >>> sum_digits(8) 8 >>> sum_digits(18) 9 >>> sum_digits(2018) 11 """ "*** YOUR CODE HERE ***" 1. One or more base cases 2. One or more recursive calls with simpler arguments. 3. Using the recursive call to solve our larger problem

Sum Digits 1 def sum_digits(n): 2 """Calculates the sum of the digits n 3 >>sum_digits(8) 4 8 5 >>sum_digits(18) 6 9 7 >>sum_digits(2018) 8 11 9 111111 10 if n 10: 11 return n 12 else: 13 all_but_last,last n /10,n 10 14 return sum_digits(all_but_last)+last
Sum Digits 1 def sum_digits(n): 2 """Calculates the sum of the digits n 3 >>> sum_digits(8) 4 8 5 >>> sum_digits(18) 6 9 7 >>> sum_digits(2018) 8 11 9 """ 10 if n < 10: 11 return n 12 else: 13 all_but_last, last = n // 10, n % 10 14 return sum_digits(all_but_last) + last

Order of Recursive Calls
Order of Recursive Calls

Demo Cascade Goal:Print out a cascading tree of a positive integer n. >>> cascade(486) ldeas: :486 If n is a single digit,just print it out! ==== 48 Otherwise,let cascade(n /10) 4 take care of the middle ===================== 48 and print(n)around it! 486 >>> cascade(48) 1 def cascade(n): 48 2 if n>cascade(4) 6 icascade(n 77 10)) ==号=== 4 7 iprint(n)_
1 def cascade cascade(n): 2 if n >> cascade(486) 486 48 4 48 486 >>> cascade(48) 48 4 48 >>> cascade(4) 4 Ideas: ● If n is a single digit, just print it out! ● Otherwise, let cascade(n // 10) take care of the middle and print(n) around it Demo

The Cascade Function 1 def cascade(n): Global frame func cascade(n)[p=G] 1fn<10: 2 print(n) cascade 4 else: 5 :print(n): f1:cascade [p=G] 6 cascade(n//10) 47 print(n) n123 8 。中。。t。。。 9 cascade(123) f2:cascade [p=G] 12 Each cascade frame is Return value None from a different call to cascade. Output Base cas∥ f3:cascade【p=G] 123 n 1 Until the Return value Return 12 value None appears,that call has 1 not completed. 12 Any statement can...... appear:before:or after the recursive call
The Cascade Function Each cascade frame is from a different call to cascade. Until the Return value appears, that call has not completed. Any statement can appear before or after the recursive call. Output 123 12 1 12 Base case

Demo Two Implementations of Cascade 1 def cascade(n): 1 def cascade(n): 2 if n 10: 2 print(n) 3 print(n) 3 ifn>=-10: 4 else: 4 cascade(n /10) 5 print(n) 5 print(n) 6 cascade(n /10) 7 print(n) If two implementations are equally clear,then shorter is usually better ● In this case,the longer implementation might be more clear When learning to write recursive functions,put the base case/s first
Two Implementations of Cascade ● If two implementations are equally clear, then shorter is usually better ● In this case, the longer implementation might be more clear ● When learning to write recursive functions, put the base case/s first 1 def cascade(n): 2 if n = 10: 4 cascade(n // 10) 5 print(n) Demo
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)06-Recursion.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)05-Higher-Order Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)04-Environment Diagrams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)03-Control.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)02-Names & Functions.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)01-Introduction(主讲:冯新宇).pptx
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Concurrent Assembly Code with Dynamic Thread Creation and Termination.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Modular Verification of Assembly Code with Stack-Based Control Abstractions.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)An Open Framework for Foundational Proof-Carrying Code.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)Certifying Low-Level Programs with Hardware Interrupts and Preemptive Threads.ppt
- 《计算机科学》相关教学资源(PPT课件讲稿)On the Relationship between Concurrent Separation Logic and Assume-Guarantee Reasoning.ppt
- 《计算机科学》相关教学资源(参考文献)Technical Report TTIC-TR-2008-1(Local Rely-Guarantee Reasoning).pdf
- 《计算机科学》相关教学资源(参考文献)Deny-Guarantee Reasoning.pdf
- 《计算机科学》相关教学资源(参考文献)A Rely-Guarantee-Based Simulation for Verifying Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Modular Verification of Linearizability with Non-Fixed Linearization Points.pdf
- 《计算机科学》相关教学资源(参考文献)Characterizing Progress Properties of Concurrent Objects via Contextual Refinements.pdf
- 《计算机科学》相关教学资源(参考文献)Rely-Guarantee-Based Simulation for Compositional Verification of Concurrent Program Transformations.pdf
- 《计算机科学》相关教学资源(参考文献)Compositional Verification of Termination-Preserving Refinement of Concurrent Programs.pdf
- 《计算机科学》相关教学资源(参考文献)A Program Logic for Concurrent Objects under Fair Scheduling.pdf
- 《计算机科学》相关教学资源(参考文献)A Practical Verification Framework for Preemptive OS Kernels.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)08-Containers.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)09-Data-Abstractions.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)10-Trees.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)11-Mutable-Values.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)12-Mutable Functions & Growth.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators.pdf
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)13-Iterators & Generators.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)14-Object-Oriented Programming.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)15-Inheritance.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)16-Linked Lists & Mutable Trees.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)17-Interfaces.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)18-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)19-More-Scheme.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)20-Interpreters.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)21-Macros.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)22-Streams.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)23-SQL-I.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)24-SQL-II.pptx
- 南京大学:《计算机程序的构造和解释 Structure and Interpretation of Computer Programs》课程教学资源(PPT课件讲稿)25-Conclusion, and Final Exam Review.pptx
- 中国科学技术大学:《信号与图像处理基础 Signal and Image Processing》课程教学资源(PPT课件讲稿)01 绪论 Introduction(主讲:曹洋).pptx