You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _sources/Lists/TheAccumulatorPatternwithLists.rst
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,14 +16,14 @@ The Accumulator Pattern with Lists
16
16
----------------------------------
17
17
18
18
Remember the :ref:`accumulator pattern <accumulator>`? Many algorithms involving lists make use of
19
-
this pattern to process the items in a list and compute a result. In this section, we'll
19
+
this pattern to process the items in a list and compute a result. In this section, we'll
20
20
explore the use of the accumulator pattern with lists.
21
21
22
22
Let's take the problem of adding up all of the items in a list. The following program
23
23
computes the sum of a list of numbers.
24
24
25
25
.. activecode:: ac7_10_0
26
-
26
+
27
27
sum = 0
28
28
for num in [1, 3, 5, 7, 9]:
29
29
sum = sum + num
@@ -42,7 +42,7 @@ Sometimes when we're accumulating, we don't want to add to our accumulator every
42
42
Consider, for example, the following program which counts the number of names with more than 3 letters.
43
43
44
44
.. activecode:: ac7_10_1
45
-
45
+
46
46
long_names = 0
47
47
for name in ["Joe", "Sally", "Amy", "Brad"]:
48
48
if len(name) > 3:
@@ -53,9 +53,9 @@ Here, we **initialize** the accumulator variable to be zero on line 1.
53
53
54
54
We **iterate** through the sequence (line 2).
55
55
56
-
The **update** step happens in two parts. First, we check to see if the name is longer than 3 letters. If
57
-
so, then we increment the accumulator variable ``long_names`` (on line 4) by adding one to
58
-
it.
56
+
The **update** step happens in two parts. First, we check to see if the name is longer than 3 letters. If
57
+
so, then we increment the accumulator variable ``long_names`` (on line 4) by adding one to
58
+
it.
59
59
60
60
At the end, we have accumulated the total number of long names.
61
61
@@ -70,17 +70,17 @@ We can use conditionals to also count if particular items are in a string or lis
70
70
num_vowels += 1
71
71
print(num_vowels)
72
72
73
-
We can also use ``==`` to execute a similar operation. Here, we'll check to see if the character we are iterating over is
74
-
an "o". If it is an "o" then we will update our counter.
73
+
We can also use ``==`` to execute a similar operation. Here, we'll check to see if the character we are iterating over is
74
+
an "o". If it is an "o" then we will update our counter.
75
75
76
76
.. image:: Figures/accum_o.gif
77
-
:alt:a gif that shows code to check that "o" is in the phrase "onomatopoeia".
77
+
:alt:a gif that shows code to check that "o" is in the phrase "onomatopoeia".
78
78
79
79
Accumulating the Max Value
80
80
~~~~~~~~~~~~~~~~~~~~~~~~~~
81
81
82
-
We can also use the accumulation pattern with conditionals to find the maximum or minimum value. Instead of
83
-
continuing to build up the accumulator value like we have when counting or finding a sum, we can reassign the
82
+
We can also use the accumulation pattern with conditionals to find the maximum or minimum value. Instead of
83
+
continuing to build up the accumulator value like we have when counting or finding a sum, we can reassign the
84
84
accumulator variable to a different value.
85
85
86
86
The following example shows how we can get the maximum value from a list of integers.
@@ -96,13 +96,13 @@ The following example shows how we can get the maximum value from a list of inte
96
96
97
97
Here, we initialize best_num to zero, assuming that there are no negative numbers in the list.
98
98
99
-
In the for loop, we check to see if the current value of n is greater than the current value of ``best_num``.
100
-
If it is, then we want to **update** ``best_num`` so that it now is assigned the higher number. Otherwise, we
99
+
In the for loop, we check to see if the current value of n is greater than the current value of ``best_num``.
100
+
If it is, then we want to **update** ``best_num`` so that it now is assigned the higher number. Otherwise, we
101
101
do nothing and continue the for loop.
102
102
103
-
You may notice that the current structure could be a problem. If the numbers were all negative what would
104
-
happen to our code? What if we were looking for the smallest number but we initialized ``best_num`` with
105
-
zero? To get around this issue, we can initialize the accumulator variable using one of the numbers in the
103
+
You may notice that the current structure could be a problem. If the numbers were all negative what would
104
+
happen to our code? What if we were looking for the smallest number but we initialized ``best_num`` with
105
+
zero? To get around this issue, we can initialize the accumulator variable using one of the numbers in the
106
106
list.
107
107
108
108
.. activecode:: ac7_10_4
@@ -114,13 +114,13 @@ list.
114
114
best_num = n
115
115
print(best_num)
116
116
117
-
The only thing we changed was the value of ``best_num`` on line 2 so that the value of ``best_num`` is the
117
+
The only thing we changed was the value of ``best_num`` on line 2 so that the value of ``best_num`` is the
118
118
first element in ``nums``, but the result is still the same!
119
119
120
120
Accumulating a String Result
121
121
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
122
122
123
-
The accumulator pattern can be used to convert a list of items to a string.
123
+
The accumulator pattern can be used to convert a list of items to a string.
124
124
125
125
Consider the following program:
126
126
@@ -138,7 +138,7 @@ the current contents of ``result`` with the comma separator and a score from the
138
138
the ``result`` with the new value. Use CodeLens to step through this example to see it in action.
139
139
140
140
The output of the program has some undesirable formatting problems: there is a trailing comma instead
141
-
of a period, and there are no spaces between the items. The next activity lets you work to
141
+
of a period, and there are no spaces between the items. The next activity lets you work to
142
142
correct those problems.
143
143
144
144
.. tabbed:: tab_string_accum
@@ -163,7 +163,7 @@ correct those problems.
163
163
164
164
print("The scores are " + result)
165
165
166
-
=====
166
+
====
167
167
168
168
from unittest.gui import TestCaseGui
169
169
@@ -254,8 +254,8 @@ correct those problems.
254
254
**Challenge** For each word in ``words``, add 'd' to the end of the word if the word ends in "e" to make it past tense. Otherwise, add 'ed' to make it past tense. Save these past tense words to a list called ``past_tense``.
Copy file name to clipboardExpand all lines: _sources/PythonTurtle/AFewMoreturtleMethodsandObservations.rst
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -102,63 +102,63 @@ color, or to put her pen down and draw a line, or to change her shape, etc.)
102
102
103
103
.. parsonsprob:: 3_10
104
104
105
-
The following program uses the stamp method to create a circle of turtle shapes as shown to the left:
105
+
The following program uses the stamp method to create a circle of turtle shapes as shown to the left:
106
106
107
-
.. image:: Figures/TurtleCircle.png
107
+
.. image:: Figures/TurtleCircle.png
108
108
:width:150
109
-
:align:left
110
-
109
+
:align:left
110
+
111
111
But the lines are mixed up. The program should do all necessary set-up, create the turtle, set the shape to "turtle", and pick up the pen. Then the turtle should repeat the following ten times: go forward 50 pixels, leave a copy of the turtle at the current position, reverse for 50 pixels, and then turn right 36 degrees. After the loop, set the window to close when the user clicks in it.
112
-
113
-
Drag the blocks of statements from the left column to the right column and put them in the right order with the correct indention. Click on <i>Check Me</i> to see if you are right. You will be told if any of the lines are in the wrong order or are incorrectly indented.
112
+
113
+
Drag the blocks of statements from the left column to the right column and put them in the right order with the correct indention. Click on <i>Check Me</i> to see if you are right. You will be told if any of the lines are in the wrong order or are incorrectly indented.
114
114
-----
115
115
import turtle
116
116
wn = turtle.Screen()
117
117
jose = turtle.Turtle()
118
118
jose.shape("turtle")
119
119
jose.penup()
120
-
=====
121
-
for size in range(10):
122
-
=====
123
-
jose.forward(50)
124
120
=====
125
-
jose.stamp()
126
-
=====
127
-
jose.forward(-50)
121
+
for size in range(10):
128
122
=====
129
-
jose.right(36)
123
+
jose.forward(50)
124
+
====
125
+
jose.stamp()
130
126
=====
127
+
jose.forward(-50)
128
+
====
129
+
jose.right(36)
130
+
====
131
131
wn.exitonclick()
132
132
133
133
**Mixed up program**
134
134
135
135
.. parsonsprob:: 3_11
136
136
137
137
The following program uses the stamp method to create a line of turtle shapes as shown to the left:
138
-
139
-
.. image:: Figures/Turtle3Stamp.png
138
+
139
+
.. image:: Figures/Turtle3Stamp.png
140
140
:width:150
141
-
:align:left
142
-
141
+
:align:left
142
+
143
143
But the lines are mixed up. The program should do all necessary set-up, create the turtle, set the shape to "turtle", and pick up the pen. Then the turtle should repeat the following three times: go forward 50 pixels and leave a copy of the turtle at the current position. After the loop, set the window to close when the user clicks in it.
144
-
144
+
145
145
Drag the blocks of statements from the left column to the right column and put them in the right order with the correct indention. Click on <i>Check Me</i> to see if you are right. You will be told if any of the lines are in the wrong order or are incorrectly indented.
wn = turtle.Screen() # Set up the window and its attributes
31
31
wn.bgcolor("lightgreen")
@@ -100,63 +100,63 @@ Here are some *How to think like a computer scientist* observations:
100
100
.. parsonsprob:: 3_6
101
101
102
102
The following program has one turtle, "jamal", draw a capital L in blue and then another, "tina", draw a line to the west in orange as shown to the left:
103
-
104
-
.. image:: Figures/TwoTurtles1.png
103
+
104
+
.. image:: Figures/TwoTurtles1.png
105
105
:width:150
106
-
:align:left
107
-
106
+
:align:left
107
+
108
108
The program should do all set-up, have "jamal" draw the L, and then have "tina" draw the line. Finally, it should set the window to close when the user clicks in it.
109
-
109
+
110
110
Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on *Check Me* to see if you are right. You will be told if any of the lines are in the wrong order.
111
111
-----
112
112
import turtle
113
113
wn = turtle.Screen()
114
-
=====
114
+
=====
115
115
jamal = turtle.Turtle()
116
116
jamal.pensize(10)
117
-
jamal.color("blue")
117
+
jamal.color("blue")
118
118
jamal.right(90)
119
119
jamal.forward(150)
120
-
=====
120
+
=====
121
121
jamal.left(90)
122
122
jamal.forward(75)
123
-
=====
123
+
====
124
124
tina = turtle.Turtle()
125
125
tina.pensize(10)
126
126
tina.color("orange")
127
127
tina.left(180)
128
128
tina.forward(75)
129
-
=====
129
+
====
130
130
wn.exitonclick()
131
131
132
132
.. parsonsprob:: 3_7
133
133
134
134
The following program has one turtle, "jamal", draw a line to the north in blue and then another, "tina", draw a line to the east in orange as shown to the left:
135
-
136
-
.. image:: Figures/TwoTurtlesL.png
135
+
136
+
.. image:: Figures/TwoTurtlesL.png
137
137
:width:150
138
-
:align:left
139
-
138
+
:align:left
139
+
140
140
The program should import the turtle module, get the window to draw on, create the turtle "jamal", have it draw a line to the north, then create the turtle "tina", and have it draw a line to the east. Finally, it should set the window to close when the user clicks in it.
141
-
141
+
142
142
Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on *Check Me* to see if you are right. You will be told if any of the lines are in the wrong order.
0 commit comments