Class 11 – Friday, February 7
Building and examining lists
An optimist thinks — Elevator close button — Really does something
Look both ways
Agenda
- String manipulation and analysis
- String, loop, and list chrestomathics
- Extend
print()
function functionality
Peer mentoring
- What is it?
Downloads
- Program secret_decoder.py
- Program thats_printastic.py
- Program i_looper.py
- Program nested_looper.py
- Program aeschylus.py
To do list
- Reveal one of your super power(s)
- Share a selfie
- Review class artifacts.
- Complete the homework
Chrestomathics — program secret_decoder.py
Scenario
- You are to first get a code phrase (string of characters) from the program user. You are also to get a list of indices from the user. You are to then 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 ]
- The output is then the string
'ei-ei-o'
, becauses[ 4 ]
equals'e'
,s[ 8 ]
equals'i'
,s[ 14 ]
, equals'o'
, ands[ 26 ]
equals'-'
Mandatory 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
Program thats_printastic.py
— introduce optional closing parameter end="..."
A program run
Wa‐Hoo‐Wa
Rah‐Rah‐Rah
Wa‐Hoo‐Wa!!!Rah‐Rah‐Rah
Wa‐Hoo‐Wa !!! Rah‐Rah‐Rah
t‐a‐t‐t‐a‐r‐r‐a‐t‐t‐a‐t-
saippuakivikauppias
Program i_looper.py
- For a user-supplied
i
, prints a line of the form
i: 0 1 2 ... i
Nested looping — program nested_looper.py
- Introduces nested looping. Where for a user-supplied
n
, the program loopsn
times and on thei
th iteration prints a line of the form
i: 0 1 2 ... i
Two possible program runs
Enter number: 5
0: 0
1: 0 1
2: 0 1 2
3: 0 1 2 3
4: 0 1 2 3 4
Enter number: 12
0: 0
1: 0 1
2: 0 1 2
3: 0 1 2 3
4: 0 1 2 3 4
5: 0 1 2 3 4 5
6: 0 1 2 3 4 5 6
7: 0 1 2 3 4 5 6 7
8: 0 1 2 3 4 5 6 7 8
9: 0 1 2 3 4 5 6 7 8 9
10: 0 1 2 3 4 5 6 7 8 9 10
11: 0 1 2 3 4 5 6 7 8 9 10 11
© 2020 Jim Cohoon | Resources from previous semesters are available. |