def triplets(arr):
zeros_in_sequence = 0
triplets_seen = 0
for elt in arr:
if not elt:
zeros_in_sequence += 1
if zeros_in_sequence == 3:
triplets_seen += 1
zeros_in_sequence = 0 # reset, we have seen 3 0's
else:
zeros_in_sequence = 0 # reset, we broke a 0 streak
return triplets_seenMore Iteration Practice
Triplets Example
Today we discussed while loops and a more challenging triplets example. We will discuss Algorithms, Measurements, Reliability, and Validity next lecture. You will do the reading first.
Challenge question: triplets
What does the below code do?
Here are some example outputs:
triplets(make_array(0, 1, 0, 0, 0, 1))1
triplets(make_array(0, 1, 0, 0, 1, 0, 0, 0, 0, 1))1
triplets(make_array(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0))3
Strategy from class:
- Run the code first. See what the example inputs/outputs are. Give a short 1-2 sentence summary of what the function does at a very high level. What you said in class: “It counts the zeros in order” “It counts triplets of zeros”
- Then, look at the code to understand what each name is. What you said in class:
triplets_seen: The number of triplets. We can see this by thereturnstatement.zeros_in_sequence: seems to be updating and “resetting” to zero.elt: Maybe short for element? Iterates over the array,arr.arr: The argument totriplets. Based on the example input/output, this is an array of numbers.
- Finally, trace the code line by line to clear up remaining questions. Some questions you had in class:
- What is
not eltdoing? When does it evaluate toTrueorFalse? - What is
zeros_in_sequencedoing? How does it work? - What is the
+=shorthand?- (Answer: “increment and re-assign” the left-hand-side, using the right-hand-side.)
- What is
For not elt, recall our discussion of truthy values:
- Every value can be cast to a Boolean value.
- The only values that evaluate to
Falseare0,None, and an empty sequence (empty array, zero-length string, empty list, etc.). - All other values evaluate to
True.
- The only values that evaluate to
- Therefore for numeric values of
elt, the only instance in whichnot eltwill evaluate toTrueis wheneltis0:not eltisnot 0isnot FalseisTrue. - All non-zero numeric values of
eltwill evaluate toFalse, e.g., wheneltis1:not eltisnot 1isnot TrueisFalse.
Explanation: Each Line Explained
Each line explained:
def triplets(arr):
zeros_in_sequence = 0
triplets_seen = 0
for elt in arr:
if not elt:
zeros_in_sequence += 1
if zeros_in_sequence == 3:
triplets_seen += 1
zeros_in_sequence = 0
else:
zeros_in_sequence = 0
return triplets_seen
triplets(make_array(0, 1, 0, 0, 1, 0, 0, 0, 0, 1))- (Line 1) Define the function
triplets. It takes an arrayarr. We can see this from Line 14, which is a function call totriplets. - (Lines 2-3) Set
zeros_in_sequenceandtriplets_seento0. - (Line 4) Iterate through each element (
elt) in the argument arrayarr. - (Line 5) The Boolean expression
not eltwill only evaluate toTrue(i.e., we will only evaluate thisifcase) if the currenteltis0.- (Line 6) Increment
zeros_in_sequenceby 1. - (Line 7) Check if we have seen three
zeros_in_sequence.- (Lines 8-9): If so, we have seen a triple, so increment our return value,
triplets_seen. Also resetzeros_in_sequence.
- (Lines 8-9): If so, we have seen a triple, so increment our return value,
- (Line 6) Increment
- (Lines 10-11) If we saw a non-zero
elt, then we have broken our zero-sequence. Resetzeros_in_sequence. - (Line 12): Return the number of triples seen.
Explanation: Array Example Explained
def triplets(arr):
zeros_in_sequence = 0
triplets_seen = 0
for elt in arr:
if not elt:
zeros_in_sequence += 1
if zeros_in_sequence == 3:
triplets_seen += 1
zeros_in_sequence = 0
else:
zeros_in_sequence = 0
return triplets_seen
triplets(make_array(0, 1, 0, 0, 1, 0, 0, 0, 0, 1))elements | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 |
indices 0 1 2 3 4 5 6 7 8 9
The values of zeros_in_sequence and triplets_seen after the corresponding iteration has finished evaluating the loop body, before the loop moves to the next element or exits:
| index | elt |
triplets_seen |
zeros_in_sequence |
|---|---|---|---|
| 0 | 0 |
0 |
1 |
| 1 | 1 |
0 |
0 |
| 2 | 0 |
0 |
1 |
| 3 | 0 |
0 |
2 |
| 4 | 1 |
0 |
0 |
| 5 | 0 |
0 |
1 |
| 6 | 0 |
0 |
2 |
| 7 | 0 |
1 |
0 (why?) |
| 8 | 0 |
1 |
1 |
| 9 | 1 |
1 |
0 |
While loops
See the previous lecture notes set.
Algorithms and Measurements
For this lecture’s notes, please see:
- Lecture slides.
- Excerpt of “Chapter 5: Evaluating Research.” Elizabeth Heger Boyle, Deborah Carr, Benjamin Cornwell, Shelley Correll, Robert Crosnoe, Jeremy Freese, and Waters, Mary C. 2017. The Art and Science of Social Research. New York: W. W. Norton & Company. PDF link (UC Berkeley only)
- Discussion handout.
We will cover this lecture in more detail next time, on Monday 11/2.