Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// PAGE 134 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// PAGE 135 ./////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// PAGE 136 6.1 (8 pts) In Section 5.2, p. 134-136 the author discusses the Accumulator Pattern, which is

image text in transcribed

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PAGE 134

image text in transcribed

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PAGE 135

image text in transcribed

image text in transcribed

.///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PAGE 136

image text in transcribed

6.1 (8 pts) In Section 5.2, p. 134-136 the author discusses the "Accumulator Pattern", which is a very important topic in this course; one of the most important for you to master. So please read those two pages several times and try to understand every detail. The figure at the top of p. 135 shows the various stages of execution for the code on p. 134. The code mySum = mySum + num takes the old value of my Sum, adds num to it, and stores the result back in mySum. That code is done inside a for loop, for num in myList, after setting mySum initially to zero. The final value for mySum is 20. Page 2 of 6 CS 8 (Winter 2020) Re-write this code to iterate through the numbers in numList and add them to mySum using a while loop. numList = [3, 2, 7, -1, 9] print (mySum) # Should produce 20 Loop Pattern: Accumulator Loop A common pattern in loops is to accumulate "something in every iteration of the loop. Given a list of numbers numList, for example, we might want to sum the numbers up. To do this using a for loop, we first need to introduce a variable mySum that will hold the sum. This variable is initialized to 0; then a for loop can be used to iterate through the numbers in numList and add them to mySum. For example: >>> numList = [3, 2, 7, -1, 9] >>> mySum = 0 # initializing the accumulator >>> for num in numList: mySum = mySum + num # adding to the accumulator # the sum of numbers in numList >>> mySum 20 numList mySum = 0 Figure 5.3 Accumulator pattern. The for loop iterates over the numbers in list numList. In every iteration, the current number is added to the accumulator mySum using the assignment mySum = mySum + num. num = mySum = mySum + num = 3 num= mySum = mySum + num = 5 num = mySum = mySum + num = 12 num= mySum = mySum + num = 11 num= mySum = mySum + num = 20 The execution of the previous for loop example is illustrated in Figure 5.3. The variable mySum serves as the accumulator. In this case, it is an integer accumulator initialized to 0 because we are summing integers and 0 is the identity for addition (i.e., 0 doesn't affect addition). Every value of num is added to the accumulator with the assignment mySum = mySum + num In the expression to the right of the assignment operator =, the value of num and the current value of the accumulator mySum are added together. The assignment then puts the result of this addition back into the accumulator my Sum. We say that mySum is incremented by the value of num. This operation is so common that there is a shortcut for it: mySum += num Let's recompute the sum using this shortcut: >>> mySum = 0 >>> for num in numList: mySum += num We refer to the pattern of this for loop as the accumulator loop pattern. Accumulating Different Types We illustrate the accumulator pattern with several more examples. Recall that in Chapter 2 we introduced the built-in function sum) that can be used to add up the values in a list: >>> sum(numList) 20 So, writing a for loop to sum up the numbers in a list was not really necessary. Usually, however, a built-in function is not available. What if, for example, we wanted to multiply all the numbers in the list? An approach similar to the one we used for the sum might work: >>> myProd = 0 # initializing the product >>> for num in numList: # num gets values from numList myProd = myProd * num # myProd is multiplied by num >>> myProd # what went wrong? What went wrong? We initialized the accumulator product myProd to 0; the problem is that O times anything is 0. When we multiply myProd by every value in numList, we will always get 0 back. The value 0 was a good choice for initializing a sum because 0 is the identity for the addition operator. The identity value for the product operator is 1: >>> myProd = 1 >>> for num in numList: myProd = myProd * num >>> myProd -378

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

Explain why employees join unions.

Answered: 1 week ago