Class 10 – Wednesday, February 24
Listing
Lest we go forward — Without knowing about lists — We would be wanting
Look both ways
Agenda
- Make loops right
- Provide you more background on list basics
Important survey
- Please take this voluntary survey
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.
To do list
- Review class artifacts.
- Review epistles
- Complete homework
Downloads
- Program list_support.py
- Program list_building.py
- Program wowzers.py
- Program summing.py
- Program secret_decoder.py
Lists
- Tell me about them
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
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]
Program wowzers.py
-
Task
- Loop through a string, list of words, and a list of numbers
- Analyze a list using built-in functions
-
Possible program run
Enter your favorite color: teal
Enter some names: willy evander aurelius ann
color = teal
names = ['willy', 'evander', 'aurelius', 'ann' ]
numbers = [12, 7, -11, -12, 59]
T
E
A
L
Willy
Evander
Aurelius
Ann
-12
-7
11
12
-59
t e a l
['willy', 'evander', 'aurelius', 'ann' ]
[5, 7, 8, 3]
length of longest name: 8
first alphabetically = ann
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 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
Hoos got your back — food insecurity
- Nationwide more than 10% of college students are not able to afford nutrious food. This problem occurs at UVA also. I am aware of two sources at UVA to help out.
- The Engineering School has a food pantry in Thornton A121. It operates on the honor system and is meant to help students with need.
- The unversity assists with the Community Food Pantry in the Runk Green Room. See the website for details.
- If you find yourself experiencing some financial hardships, please consider talking with anyone in the Undergraduate Programs Office. They can help you find even more resources.
- If you can pay it forward, please help by donating items or money to either pantry.
Coney Island Cyclone roller coaster
Willy Mays
Marcus Aurelius
Evander of Pallantium
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |