This preview shows page 1-2-3-4-5-6-7-49-50-51-52-53-54-55-98-99-100-101-102-103-104 out of 104 pages.
61A Lecture 7Monday, September 12Sunday, September 11, 2011Pig Contest Rules•The score for an entry is the sum of win rates against every other entry.•All strategies must be deterministic functions of the current score! Non-deterministic strategies will be disqualified.•Winner: 3 points extra credit on Project 1•Second place: 2 points•Third place: 1 point•The real prize: honor and glory•To enter: submit a file pig.py that contains a function called final_strategy as assignment p1contest by Monday, 9/262Sunday, September 11, 2011Function Decorators(demo)3Sunday, September 11, 2011Function Decorators(demo)3@trace1def triple(x): return 3 * xSunday, September 11, 2011Function Decorators(demo)3@trace1def triple(x): return 3 * xFunction decoratorSunday, September 11, 2011Function Decorators(demo)3@trace1def triple(x): return 3 * xFunction decoratorDecorated functionSunday, September 11, 2011Function Decorators(demo)3@trace1def triple(x): return 3 * xis identical toFunction decoratorDecorated functionSunday, September 11, 2011Function Decorators(demo)3@trace1def triple(x): return 3 * xis identical todef triple(x): return 3 * xtriple = trace1(triple)Function decoratorDecorated functionSunday, September 11, 2011Function Decorators(demo)3@trace1def triple(x): return 3 * xis identical todef triple(x): return 3 * xtriple = trace1(triple)Function decoratorDecorated functionWhy not just use this?Sunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Sunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Practical guidanceSunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsPractical guidanceSunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsTesting functions stay smallPractical guidanceSunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsRevisions should require few code changesTesting functions stay smallPractical guidanceSunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsRevisions should require few code changesIsolates problemsTesting functions stay smallPractical guidanceSunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsRevisions should require few code changesWriting fewer lines of code saves you timeIsolates problemsTesting functions stay smallPractical guidanceSunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsRevisions should require few code changesWriting fewer lines of code saves you timeIsolates problemsTesting functions stay smallCopy/Paste has a steep pricePractical guidanceSunday, September 11, 2011The Art of the FunctionEach function should have exactly one jobDon't repeat yourself (DRY)Functions should be defined generally4Separation of concernsRevisions should require few code changesWriting fewer lines of code saves you timeIsolates problemsTesting functions stay smallCopy/Paste has a steep priceThese are guidelines,not strict rules!Practical guidanceSunday, September 11, 2011Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5Practical guidanceSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5Practical guidanceSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5Practical guidanceboolean turn_is_overSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5Practical guidanceboolean turn_is_overd diceSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5Practical guidanceboolean turn_is_overd diceplay_helper take_turnSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5>>> from operator import mul>>> def square(let):Practical guidanceboolean turn_is_overd diceplay_helper take_turnSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5>>> from operator import mul>>> def square(let): return mul(let, let)Practical guidanceboolean turn_is_overd diceplay_helper take_turnSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5>>> from operator import mul>>> def square(let): return mul(let, let)Practical guidanceboolean turn_is_overd diceplay_helper take_turnSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5>>> from operator import mul>>> def square(let): return mul(let, let)Practical guidanceboolean turn_is_overd diceplay_helper take_turnSunday, September 11, 2011From: To:Choosing NamesNames typically don’t matter for correctnessbutthey matter tremendously for legibility5>>> from operator import mul>>> def square(let): return mul(let, let)Not stylishPractical guidanceboolean turn_is_overd diceplay_helper take_turnSunday, September 11, 2011Functional Abstractions6Sunday, September 11, 2011Functional Abstractions6def square(x): return mul(x, x)Sunday, September 11, 2011Functional Abstractions6def square(x): return mul(x, x)def sum_squares(x, y): return square(x) + square(y)Sunday, September 11, 2011Functional Abstractions6What does sum_squares need to know about square to use it?def square(x): return mul(x, x)def sum_squares(x, y): return square(x) + square(y)Sunday, September 11,
View Full Document