Class 11 – Friday, September 17
Odds, ends, and beginnings
Lest we go forward — We cannot come back again — But beware of baths
Look both ways
Agenda
- Extend
print()
function functionality
- Loop chrestomathics
- Foreshadow nested loops
Downloads
- Program summing.py
- Accumulate a sum of values
- Program secret_decoder.py
- Chrestomathics — display a secret message using a code phrase and indices
- Program thats_printastic.py
- Introduce
print()
optional closing parameterend="..."
- Program c_looper.py
- Foreshadow nested looping
- Program going_rowing.py
- Introduce datasets
- Demonstrate looping through a dataset
To do list
- Reveal one of your super power(s)
- Share a selfie
- Review class artifacts
- Complete current homework
Numeric list paradigm — program summing.py
- Demonstrates accumulating a sum of integer values
reply = input( 'Enter a list of numbers: ' )
slist = reply.split()
nlist = []
for s in slist :
nbr = int( s )
nlist.append( nbr )
# by accumulation get the sum of the numbers
total = 0
for nbr in nlist :
total = total + nbr
# print summation
print( "sum(", nlist, "):", total )
-
Some program runs
Enter a list of numbers: 3 1 4 1 5 9
sum( [3, 1, 4, 1, 5, 9] ): 23
Enter a list of numbers: 58 2 37 16 99 1 9 23
sum( [58, 2, 37, 16, 99, 1, 9, 23] ): 245
Chrestomathics — program secret_decoder.py
-
Scenario
- Separately get an input code phrase and list of indices. Determine the hidden message by using the indices to peek into the code phrase. For example, suppose the code phrase equals
'abcdefghijklmnopqrstuvwxyz-'
and the indices are
[ 4, 8, 26, 4, 8, 26, 14 ]
- Then the hidden message is
'ei-ei-o'
Because
s[ 4 ]
equals'e'
;s[ 8 ]
equals'i'
;s[ 14 ]
; equals'o'
; ands[ 26 ]
equals'-'
.
-
Algorithm
- Get the code phrase that contains hidden message.
- Clean up reply to get code phrase with no leading or trailing blank space.
- Print the code phrase.
- Get the list of indices (as string) for peeking into code phrase.
- Convert reply into a list of numeric strings.
- Build one-by-one the list of indices out of the numeric strings .
- Print the list of indices.
- Build secret message (string) by peeking into code phrase using the indices.
- Print secret message.
-
Some program runs
Enter code phrase: computer
Code phrase: computer
Enter indices: 3 6 5
Indices: [3, 6, 5]
Hidden message: pet
Enter code phrase: gosh look what the cat is up to now
Code phrase: gosh look what the cat is up to now
Enter indices: 24 5 17 17 27 23 32 0
Indices: [24, 5, 17, 17, 27, 23, 32, 0]
Hidden message: sleeping
Enter code phrase: ‐abcdefghijklmnopqrstuvwxyz‐
Code phrase: ‐abcdefghijklmnopqrstuvwxyz-
Enter indices: 12 15 22 5 27 9 19 0 1 12 12 27 14 5 5 4
Indices: [12, 15, 22, 5, 27, 9, 19, 0, 1, 12, 12, 27, 14, 5, 5, 4]
Hidden message: love‐is‐all‐need
Program thats_printastic.py
- Introduce
print()
optional closing parameterend="..."
- The optional
end="..."
parameter for aprint()
function allows changing from its default action of starting the next print on a new line.
-
Program run
Wa-Hoo-Wa
Rah-Rah-Rah
Wa-Hoo-Wa!Rah-Rah-Rah
Wa-Hoo-Wa! Rah-Rah-Rah
s-a-i-p-p-u-a-k-i-v-i-k-a-u-p-p-i-a-s-.
s a i p p u a k i v i k a u p p i a s .
saippuakivikauppias.
Program c_looper.py
- Foreshadow nested looping
- For a user-supplied
n
, prints a line of the form
row : 0 1 2 ... n-1
-
Some program runs
Enter number of columns: 5
row : 0 1 2 3 4
Enter number of columns: 12
row : 0 1 2 3 4 5 6 7 8 9 10 11
Program dataset_intro.py
- Demonstrate looping through a dataset in different ways
-
Program run
table: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L', 'M']]
the table has 4 rows
row ['A', 'B', 'C'] has 3 columns
row ['D', 'E', 'F'] has 3 columns
row ['G', 'H', 'I'] has 3 columns
row ['J', 'K', 'L', 'M'] has 4 columns
row 0 : ['A', 'B', 'C']
row 1 : ['D', 'E', 'F']
row 2 : ['G', 'H', 'I']
row 3 : ['J', 'K', 'L', 'M']