1 / 4

The Accumulator Pattern Motivating Example:

The Accumulator Pattern Motivating Example: Suppose that you want to add up (“accumulate”) 1 2 plus 2 2 plus 3 2 plus … plus 1000 2 You could use: total = (1 ** 2) + (2 ** 2) + (3 ** 2) + (4 ** 2) + [ etc to 1000] But that would be painful (and impractical)

neva
Télécharger la présentation

The Accumulator Pattern Motivating Example:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Accumulator Pattern • Motivating Example: • Suppose that you want to add up (“accumulate”) 12 plus 22 plus 32 plus … plus 10002 • You could use: • total = (1 ** 2) + (2 ** 2) + (3 ** 2) + (4 ** 2) + [etc to 1000] • But that would be painful (and impractical) • So instead use a loop, like this: • Let’s act that out (next slide): X ** Y meansX raised to the Yth power totalstarts at zero Loop 1000times: totalbecomes what it was + next item to add to total

  2. The Accumulator Pattern, acted out • Using a loop to compute 12 plus 22 plus 32 plus … plus 10002 totalstarts at zero Loop 1000times: totalbecomes what it was + next item to add to total total starts at 0 total becomes 1 total becomes 5 total becomes 14 total becomes 30 total becomes 55 total becomes 91 ... We add in 12 (which is 1), so ... We add in 22(which is 4), so ... We add in 32(which is 9), so ... We add in 42(which is 16), so ... We add in 52(which is 25), so ... We add in 62(which is 36), so ... and so forth

  3. The Accumulator Pattern, in Python • This summing versionof the Accumulator Pattern,applied to this problem of summing squares,is written in Python like this: totalstarts at zero Loop 1000times: totalbecomes what it was + next item to add to total Use a rangeexpression in a for loop Use a variable, which we chose to call total, and initialize that variable to 0 before the loop total = 0 for k in range(1000): total = total + (k + 1) ** 2 Inside the loop, put: total = total + ... Lousy mathematics, but great computer science! Read = as “becomes”. After the loop ends, the variable total has as its value the accumulated sum!

  4. The Summing version of the Accumulator Pattern Use a rangeexpression in a for loop The Accumulator Pattern for summing Use a variable, which we chose to call total, and initialize that variable to 0 before the loop Inside the loop, put: total = total + ... Lousy mathematics, but great computer science! Read = as “becomes”. total = 0 for k inrange(BLAH): total = total + STUFF After the loop ends, the variable total has as its value the accumulated sum!

More Related