Class 9 – Monday, February 22
Do that again
Did you not expect – The repetition to come – Did you not expect
Look both ways
Agenda
- String processing
- Accumulation
- List chrestomathics (e.g., greedy programming)
Word of the day
- Queueing
String processing downloads
- Program case_by_case.py
- Program countability.py
- Program find_the_spot.py
- Program replacement.py
List processing Downloads
- Program on_average.py
- Program all_int.py
To do list
- Review class artifacts.
- Read the epistle on lists.
- Check out my documentation for the
list
module.
Program case_by_case.py
- Introduces string case functions
capitalize()
,lower()
, andupper()
-
A possible program run
Enter text: thanks VERY mucH.
text: thanks VERY mucH.
text.lower(): thanks very much. # all lower case
text.upper(): THANKS VERY MUCH. # all upper case
text.capitalize(): Thanks very much. # initial character capitalized, rest lower case
Program countability.py
- Introduces string function
count()
-
A possible program run
Enter text: any bananas
Enter substring: an
Enter index: 7
text.count( substring ): 3 # count all
text.count( substring, 7 ): 1 # count starting from index 7
Program find_the_spot.py
- Introduces string member function
find()
-
A possible program run
Enter text: aardvark
Enter substring: a
text.find( substring ): 0 # first occurrence
text.find( substring, 1 ): 1 # second occurrence
text.find( substring, 2 ): 5 # third occurrence
Program replacement.py
- Introduces string function
replace()
-
A possible program run
Enter text: yellow bellied sapsucker
Enter substring (s): ll
Enter substring (r): L L
text.replace( s, r ): yeL Low beL Lied sapsucker # s's in text replaced with r's
Enter text: yellow bellied sapsucker
Enter substring (s): e
Enter substring (r):
text.replace( s, r ): yllow bllid sapsuckr # s's in text replaced with r's
Acccumulation
- 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 ''.
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
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
I can smile, see
Schrödinger's cat
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |