38
#2 (More) Environments and Recursion Drawing Hands by M. C. Escher TA: Jerry Chen ([email protected])

#2 (More) Environments and Recursion2 if x>10 and x

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: #2 (More) Environments and Recursion2 if x>10 and x

#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

Just for Fun

Page 3: #2 (More) Environments and Recursion2 if x>10 and x
Page 4: #2 (More) Environments and Recursion2 if x>10 and x

Clear winner, "jif"!

shh

Page 5: #2 (More) Environments and Recursion2 if x>10 and x

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

Code Style

Page 7: #2 (More) Environments and Recursion2 if x>10 and x

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

Composition

• Syntactical quibbles

• Content choice and structure

Two main parts

Page 9: #2 (More) Environments and Recursion2 if x>10 and x

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

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

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

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

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

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

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

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

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

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

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

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

Environment Diagrams

Page 22: #2 (More) Environments and Recursion2 if x>10 and x

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

Recursion

Page 24: #2 (More) Environments and Recursion2 if x>10 and x
Page 25: #2 (More) Environments and Recursion2 if x>10 and x
Page 26: #2 (More) Environments and Recursion2 if x>10 and x

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

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

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

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

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

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

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

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

Tree Recursion

Call multiple functionsUseful for representing choices

Page 35: #2 (More) Environments and Recursion2 if x>10 and x

Fib(n) = Fib(n - 1) + Fib(n - 2)

Page 36: #2 (More) Environments and Recursion2 if x>10 and x

Fib(2) = Fib(1) + Fib(0)

Page 37: #2 (More) Environments and Recursion2 if x>10 and x

Fib(2)

Fib(1) Fib(0)

Page 38: #2 (More) Environments and Recursion2 if x>10 and x

Fib(4)

Fib(3)

Fib(1) Fib(0)

Fib(2) Fib(1) Fib(0)

Fib(2)

Fib(1)