#2 (More) Environments and Recursion2 if x>10 and x
Transcript of #2 (More) Environments and Recursion2 if x>10 and x
![Page 1: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/1.jpg)
#2 (More) Environments and Recursion
Drawing Hands by M. C. Escher
TA: Jerry Chen ([email protected])
![Page 2: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/2.jpg)
Just for Fun
![Page 3: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/3.jpg)
![Page 4: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/4.jpg)
Clear winner, "jif"!
shh
![Page 5: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/5.jpg)
The real tally
0
3
6
9
12
15
Jif Hard G Written Out ???
![Page 6: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/6.jpg)
Code Style
![Page 7: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/7.jpg)
Why care about code style?
• The Python interpreter doesn't really care
• You want your boss to understand your code
• You want your coworkers to understand your code
• You want future you to understand your code
![Page 8: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/8.jpg)
Composition
• Syntactical quibbles
• Content choice and structure
Two main parts
![Page 9: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/9.jpg)
Composition
• Grammar and spelling
• Content choice and structure
Two main parts (for an English essay)
![Page 10: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/10.jpg)
Composition
• https://cs61a.org/articles/composition.html
• Syntax is easy to check: http://flake8.pycqa.org/en/latest/
• Content requires more human effort
![Page 11: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/11.jpg)
Composition
• The "best" code is self-explanatory
• Remove repetition and don't repeat yourself
• Reduce length without compromising readability
A few big ideas
![Page 12: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/12.jpg)
Writing "Self-Explanatory" Code
1 # If x is in range and x is even then return True 2 if x>10 and x<100 and x%2 == 0: 3 return True 4 else: 5 return False
![Page 13: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/13.jpg)
Writing "Self-Explanatory" Code
1 # If x is in range and x is even then return True 2 if x>10 and x<100 and x%2 == 0: 3 return True 4 else: 5 return False
1 in_range = lambda x: x>10 and x<100 2 is_even = lambda x: x%2 == 0 3 4 if in_range(x) and is_even(x): 5 return True 6 return False
Is the earlier comment necessary?
![Page 14: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/14.jpg)
Repetition
1 while x < max_val: 2 if x % 2 == 0: 3 handle_a(x) 4 x += 1 5 else: 6 handle_b(x) 7 x += 1
![Page 15: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/15.jpg)
Repetition
1 while x < max_val: 2 if x % 2 == 0: 3 handle_a(x) 4 x += 1 5 else: 6 handle_b(x) 7 x += 1
1 while x < max_val: 2 if x % 2 == 0: 3 handle = handle_a 4 else: 5 handle = handle_b 6 handle(x) 7 x += 1
![Page 16: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/16.jpg)
Repetition
1 while x < max_val: 2 if x % 2 == 0: 3 handle_a(x) 4 x += 1 5 else: 6 handle_b(x) 7 x += 1
Bonus: reduce nesting and length of loop code
1 def choose_handle(x): 2 ... 3 4 while x < max_val: 5 handle = choose_handle(x) 6 handle(x) 7 x += 1
Even if the overall code is longer, the while clause is shorter and easier to read
![Page 17: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/17.jpg)
Length and readability
1 def double_eights(n): 2 prev_eight = False 3 while n > 0: 4 last_digit = n % 10 5 if last_digit == 8 and prev_eight: 6 return True 7 elif last_digit == 8: 8 prev_eight = True 9 else: 10 prev_eight = False 11 n = n // 10 12 return False
Sometimes you bark up the wrong tree
![Page 18: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/18.jpg)
Length and readability
1 def double_eights(n): 2 while n > 10: 3 if n % 100 == 88: 4 return True 5 n = n // 10 6 return False 7 8 9 10 11 12
Sometimes you bark up the wrong tree
![Page 19: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/19.jpg)
Bonus*
1 def double_eights(n): 2 return '88' in str(n) 3 4 5 6 7 8 9 10 11 12
Sometimes, that tree is shorter than you think‡
*(You haven't learned this in class yet)‡(Yeah, it's a weird analogy)
![Page 20: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/20.jpg)
Composition
• There rarely is a "best" way
• The "best" way is even more rarely obvious
• All good code has its genesis in bad code
In Conclusion
![Page 21: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/21.jpg)
Environment Diagrams
![Page 22: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/22.jpg)
Environment Diagram Rules
• Names can also be bound to functions!
• Function call: create and number new frame (f1, f2, etc.) — always start in global frame
• Assignment: write variable name and expression value
• Def statements: record function name and bind function object. Remember parent frame!
• Frames return values upon completion (Global is special)
![Page 23: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/23.jpg)
Recursion
![Page 24: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/24.jpg)
![Page 25: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/25.jpg)
![Page 26: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/26.jpg)
Components of Recursion
1. Solve base case
2. Recursive call on a subproblem
3. Use the result to solve the original problem
3 Easy Steps
![Page 27: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/27.jpg)
1 def factorial(n): 2 if n == 0: 3 return 1 4 return n * factorial(n - 1)
![Page 28: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/28.jpg)
1 def factorial(n): 2 if n == 0: 3 return 1 4 return n * factorial(n - 1)
![Page 29: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/29.jpg)
1 def factorial(n): 2 if n == 0: 3 return 1 4 return n * factorial(n - 1)
![Page 30: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/30.jpg)
1 def factorial(n): 2 if n == 0: 3 return 1 4 return n * factorial(n - 1)
![Page 31: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/31.jpg)
1 def factorial(n): 2 if n == 0: 3 return 1 4 return n * factorial(n - 1)
Base case
Recursive call
Using the result
![Page 32: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/32.jpg)
1 def hailstone(n): 2 print(n) 3 if n == 1: 4 return 5 elif n % 2 == 0: 6 hailstone(n - 1) 7 else: 8 hailstone(n - 1)
![Page 33: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/33.jpg)
What's wrong?
1 def hailstone(n): 2 print(n) 3 if n == 1: 4 return 5 elif n % 2 == 0: 6 hailstone(n - 1) 7 else: 8 hailstone(n - 1)
![Page 34: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/34.jpg)
Tree Recursion
Call multiple functionsUseful for representing choices
![Page 35: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/35.jpg)
Fib(n) = Fib(n - 1) + Fib(n - 2)
![Page 36: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/36.jpg)
Fib(2) = Fib(1) + Fib(0)
![Page 37: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/37.jpg)
Fib(2)
Fib(1) Fib(0)
![Page 38: #2 (More) Environments and Recursion2 if x>10 and x](https://reader033.fdocuments.in/reader033/viewer/2022060519/604cec43f9bfc003f34f2b8d/html5/thumbnails/38.jpg)
Fib(4)
Fib(3)
Fib(1) Fib(0)
Fib(2) Fib(1) Fib(0)
Fib(2)
Fib(1)