Class 11 – Friday, September 18
Listing
Lest we go forward — Without knowing about lists — We would be wanting
Look both ways
Agenda
- Explore lists
- Emphasize some basic list processing algorithms
For the fun of it
- Reveal one of your super power(s).
Alert
- Anyone who asks me today about a function
sum()
earns negative ducks.
Downloads
- Program list_creation.py
- Program list_support.py
- Program list_building.py
- Program summing.py
- Program secret_decoder.py
To do list
- Review class artifacts.
- Review epistles
- Complete the big_production.py homework
- Complete the aeschylus.py homework
Lists
- Tell me about them
List creation basics — program list_creation.py
Task
- Show some different list possibilities
s = "we are in it together"
values = [ ]
stuff = [ 'abc', 1112, 2.71, ]
digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]
words = s.split()
print( "values =", values )
print( "stuff =", stuff )
print( "digits =", digits )
print( "words =", words )
Program run
values = []
stuff = ['abc', 1112, 2.71]
digits = [3, 1, 4, 1, 5, 9, 2, 6]
words = ['we', 'are', 'in', 'it', 'together']
List pythonics — program list_support.py
Task
- Show some basic list operations
s = "we are in it together"
values = [ ]
stuff = [ 'abc', 1112, 2.71, ]
digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]
words = s.split()
vlen = len( values )
slen = len( stuff )
dlen = len( digits )
wlen = len( words )
print( "size of", values, "=", vlen )
print( "size of", stuff, "=", slen )
print( "size of", digits, "=", dlen )
print( "size of", words, "=", wlen )
dmax = max( digits )
dmin = min( digits )
wmax = max( words )
wmin = min( words )
print( "max of", digits, "=", dmax )
print( "min of", digits, "=", dmin )
print( "max of", words, "=", wmax )
print( "min of", words, "=", wmin )
Program run
size of [] = 0
size of ['abc', 1112, 2.71] = 3
size of [3, 1, 4, 1, 5, 9, 2, 6] = 8
size of ['we', 'are', 'in', 'it', 'together'] = 5
max of [3, 1, 4, 1, 5, 9, 2, 6] = 9
min of [3, 1, 4, 1, 5, 9, 2, 6] = 1
max of ['we', 'are', 'in', 'it', 'together'] = we
min of ['we', 'are', 'in', 'it', 'together'] = are
List construction — program list_building.py
Task
- Re-demonstrate list function
append()
and built-in functionrange()
values = [ ]
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
values.append( " " )
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
print( "values =", values )
ints = []
for i in range( 0, 10 ) :
ints.append( i )
print( "ints =", ints
Program run
values = ['u', 'v', 'a', '!', ' ', 'u', 'v', 'a', '!']
ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Numeric list paradigm — program summing.py
Task
- Demonstrates accumulation to sum a list of numbers
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 possible 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 whitespace.
- 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 phrasee: 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
Proof dogs are not as smart as they think they are
© 2020 Jim Cohoon | Resources from previous semesters are available. |