announcements 61a lecture 7
play

Announcements 61A Lecture 7 Hog Contest Rules Hog Contest Winners - PDF document

Announcements 61A Lecture 7 Hog Contest Rules Hog Contest Winners Your name could be here FOREVER! Up to two people submit one entry; Fall 2011 Winners Max of one entry per person Spring 2015 Winners Kaylee Mann Slight rule


  1. Announcements 61A Lecture 7 Hog Contest Rules Hog Contest Winners Your name could be here FOREVER! • Up to two people submit one entry; Fall 2011 Winners Max of one entry per person Spring 2015 Winners Kaylee Mann 
 • Slight rule changes Yan Duan & Ziming Li 
 Sinho Chewi & Alexander Nguyen Tran Brian Prike & Zhenghao Qian 
 • Your score is the number of entries Zhaoxi Li Parker Schuh & Robert Chatham Stella Tao and Yao Ge against which you win more than 50.00001% of the time Fall 2012 Winners Fall 2015 Winners • Strategies are time-limited Chenyang Yuan Micah Carroll & Vasilis Oikonomou 
 Spring 2017 Winners • All strategies must be deterministic, Joseph Hui Matthew Wu 
 pure functions of the players' scores Anthony Yeung and Alexander Dai Cindy Jin and Sunjoon Lee Fall 2013 Winners • All winning entries will receive Anny Patino and Christian Vasquez Paul Bramsen Spring 2016 Winners Asana Choudhury and Jenna Wen extra credit Sam Kumar & Kangsik Lee Michelle Lee and Nicholas Chew • The real prize: honor and glory Kevin Chen Michael McDonald and Tianrui Chen • See website for detailed rules Andrei Kassiantchouk Fall 2014 Winners Benjamin Krieges Alan Tong & Elaine Zhao Fall 2017 Winners Zhenyang Zhang Adam Robert Villaflor & Joany Gao Zhen Qin & Dian Chen Zizheng Tai & Yihe Li cs61a.org/proj/hog_contest 3 4 The Cascade Function (Demo) Order of Recursive Calls • 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. 6 Interactive Diagram Two Definitions of Cascade (Demo) def cascade(n): def cascade(n): print(n) if n < 10: if n >= 10: print(n) cascade(n//10) else: Example: Inverse Cascade print(n) print(n) cascade(n//10) print(n) • If two implementations are equally clear, then shorter is usually better • In this case, the longer implementation is more clear (at least to me) • When learning to write recursive functions, put the base cases first • Both are recursive functions, even though only the first has typical structure 7

  2. Inverse Cascade Write a function that prints an inverse cascade: 1 
 1 
 def inverse_cascade(n): grow(n) 12 
 12 
 print(n) 123 
 123 
 shrink(n) 1234 
 1234 
 Tree Recursion 123 
 123 
 def f_then_g(f, g, n): 12 
 12 
 if n: 1 1 f(n) g(n) grow = lambda n: f_then_g(grow, print, n//10) shrink = lambda n: f_then_g(print, shrink, n//10) 9 Tree Recursion A Tree-Recursive Process Tree-shaped processes arise whenever executing the body of a recursive function makes more The computational process of fib evolves into a tree structure than one recursive call fib(5) n: 0, 1, 2, 3, 4, 5, 6, 7, 8, ... , 35 fib(n): 0, 1, 1, 2, 3, 5, 8, 13, 21, ... , 9,227,465 fib(3) fib(4) fib(1) fib(2) def fib (n): fib(2) fib(3) if n == 0 : fib(0) fib(1) 1 return 0 elif n == 1 : fib(0) fib(1) fib(1) fib(2) return 1 0 1 else : fib(0) fib(1) return fib(n- 2 ) + fib(n- 1 ) 0 1 1 0 1 (Demo) 11 12 http://en.wikipedia.org/wiki/File:Fibonacci.jpg Repetition in Tree-Recursive Computation This process is highly repetitive; fib is called on the same argument multiple times fib(5) fib(3) fib(4) Example: Counting Partitions fib(1) fib(2) fib(2) fib(3) fib(0) fib(1) 1 fib(0) fib(1) fib(1) fib(2) 0 1 fib(0) fib(1) 0 1 1 0 1 (We will speed up this computation dramatically in a few weeks by remembering results) 13 Counting Partitions Counting Partitions The number of partitions of a positive integer n, using parts up to size m, is the number The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order. increasing order. count_partitions(6, 4) count_partitions(6, 4) • Recursive decomposition: finding 2 + 4 = 6 simpler instances of the problem. 1 + 1 + 4 = 6 • Explore two possibilities: 3 + 3 = 6 • Use at least one 4 1 + 2 + 3 = 6 • Don't use any 4 1 + 1 + 1 + 3 = 6 • Solve two simpler problems: 2 + 2 + 2 = 6 • count_partitions(2, 4) 1 + 1 + 2 + 2 = 6 • count_partitions(6, 3) 1 + 1 + 1 + 1 + 2 = 6 • Tree recursion often involves 1 + 1 + 1 + 1 + 1 + 1 = 6 exploring different choices. 15 16

  3. Counting Partitions The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order. def count_partitions(n, m): • Recursive decomposition: finding if n == 0: simpler instances of the problem. return 1 • Explore two possibilities: elif n < 0: return 0 • Use at least one 4 elif m == 0: • Don't use any 4 return 0 • Solve two simpler problems: else: with_m = count_partitions(n-m, m) • count_partitions(2, 4) without_m = count_partitions(n, m-1) • count_partitions(6, 3) return with_m + without_m • Tree recursion often involves exploring different choices. (Demo) Interactive Diagram 17

Recommend


More recommend