Class 11 – Friday, February 11
Odds, ends, and beginnings
Each & every day - is a single step closer - to where you will go.
Look both ways
Agenda
- Extend
print()
function functionality
- Loop chrestomathics
- Foreshadow nested loops
Downloads
- Program wowzers.py
- Loop through and analyze lists
- Program all_int.py
- Converts a list of integer inputs in to a list of integers.
- Class Activity: Program summing.py
- Accumulate a sum of values
- Homework: Program big_production.py
- Accumulate a sum of values
- Program on_average.py
- Determine average word length
- Program thats_printastic.py
- Introduce
print()
optional closing parameterend="..."
- Program c_looper.py
- Foreshadow nested looping
- Program secret_decoder.py
- Chrestomathics — display a secret message using a code phrase and indices
- 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 big_production.py homework
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
Accumulation
- Many times problem solving requires we analyze some sequence (e.g, string or list) to determine a property or build a new sequence. The determination can require each element in the sequence, contribute (accumulate) to the answer. For example, from text determine average word length or from numbers determine their absolute values or from a string determine its reverse.
- If the answer requires the items in list contribute to a total, then an accumulation is needed of the individual terms — the accumulator is updated by repeatedly adding to it. For this to work out, the accumulator needs to be initialized before the list is processed. For totaling, the accumulator is initialized to zero.
- Data processing can require a list be built out of some input data. Here, the accumulator needs to be initialized to the empty list [].
- Sometimes data processing can require the product of some numeric data. Here, the accumulator needs to be initialized to 1.
- Data processing can require a string be built character by character. For this to work out, a string accumulator needs to be intialized to the empty string ''.
List accumulation — program all_int.py
-
Task
- Produce a list of integers from user-supplied data.
-
Realizations
- Function
int()
can only be applied to one string at a time.
- List of integers will need to be accumulated by processing the numeric strings one at a time.
- For each numeric string it will need to be converted to an integer and added to the list of integers being built
- The list of integers needs to be initalized to an empty list.
-
Algorithm
- Prompt and get text (i.e., a list of integers).
- Split input into a list of numeric strings.
- Echo list of numeric strings.
- Initialize running list of integers to empty list
- Process each string in the numeric strings list:
- Get integer representation of the string.
- Add integer to the list accumulation
- Print accumulation.
-
Some program runs
Enter integers: 3 1 4 1 5 9
nbr_strings: ['3', '1', '4', '1', '5', '9']
numbers: [3]
numbers: [3, 1]
numbers: [3, 1, 4]
numbers: [3, 1, 4, 1]
numbers: [3, 1, 4, 1, 5]
numbers: [3, 1, 4, 1, 5, 9]
Enter integers: 58 2 37 16 99 1 9 23
nbr_strings: ['58', '2', '37', '16', '99', '1', '9', '23']
numbers: [58]
numbers: [58, 2]
numbers: [58, 2, 37]
numbers: [58, 2, 37, 16]
numbers: [58, 2, 37, 16, 99]
numbers: [58, 2, 37, 16, 99, 1]
numbers: [58, 2, 37, 16, 99, 1, 9]
numbers: [58, 2, 37, 16, 99, 1, 9, 23]
-
Code
# get input
reply = input( 'Enter integers: ' )
print()
# split reply into list of numeric strings
nbr_strings = reply.split()
# see what we got
print( 'nbr_strings:', nbr_strings )
print()
# convert numeric text into list of numbers
# begin with setting up the accumulator
numbers = [] # initialize holder to prepare for adding elements
# process the strings of nbr_strings one-by-one
for ns in nbr_strings :
# process the current numeric string
nbr = int( ns ) # get numeric equivalent of ns
numbers.append( nbr ) # add equivalent to list of numbers
print( 'numbers:', numbers ) # print the updated list of numbers
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
Program on_average.py
-
Problem
- Examines a list of user-supplied words and reports determine average word length.
-
Realizations
- Because the number of words is not know when the program is being written, a loop is needed to process the words.
- To compute the average word length, need to know the number of words and the sum of the word lengths.
- The loop needs to two things for each word: compute its length and add its length to a running total.
- The running total needs to be initialized before the loop.
-
Algorithm
- Prompt and get text (i.e., a bunch of words).
- Split input into a word list.
- Echo word list to user.
- Compute the length of the list.
- Initialize running total of word lengths to 0.
- Process each word in the word list:
- Compute length of the current word of interest.
- Add that length to running total of the word lengths.
- Compute average from the word lengths total and the number of words.
- Print average.
-
Two program runs
Enter text: over the hill and through the dale
words = ['over', 'the', 'hill', 'and', 'through', 'the', 'dale']
Average word length: 4.0
Enter text: row row row your boat
words = ['row', 'row', 'row', 'your', 'boat']
Average word length: 3.4
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']
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |