Class 20 – Monday Octover 14
Control, control
Think before I act — My problem solving mantra — Make it your also
Look both ways
Agenda
- Gain familiarity with decision and iteration control constructs
Downloads
- Program spell_check.py
Datasets
To do list
- Really examine today's discussion and the artifacts
Problem who_is_the_longest_of_them_all.py
- Determine the longest word in user text. A constraint on your solution is that the only permitted list is the list of input words.
- Human being-based solution strategy — scan through the words one-at-a-time and determine whether the current word is the longest word so far.
Three possible program runs
Enter text: Think before I act — My problem solving mantra — Make it your also
problem
Enter text: Remember always — Seek to be both great and good — The world needs your aid
Remember
Enter text:
Brainstorming
- Because we do not know the number of words to be given when we are writing our solution, we definitely need a loop that can iterate over the list of input words.
- When we process a word we need to be able to determine whether it is in contention for being the longest word.
- If it is the longest word we seen so far, then we need to remember it in order to compare it to the remaining words.
- If it is not the longest word we seen so far, we take no further interest in it.
- For such loop processing to work, we need an accumulator variable keeping track of the longest word found so far. There are two obvious candidates for initializing that variable — making it the empty string or making it the first word. Either can work and there are good arguments for both of them.
- One thing for sure, the variable keeping track of the longest word so far needs to be initialized before the loop — during the loop we are updating it.
Problem spell_check.py
- Determine misspelled words in user text. To support this activity, there is a GitHub-acquired web file with the 10,000 most commonly used English words on the webs. For those interested, a more-complete list of 173,541 English words is available.`
- Human being-based solution strategy — scan through the text of interest and determine whether current word is not in the correct-spelling word list.
Three possible program runs
Enter text: It was the beste of times It was the wurst of times It was Oktoberfest
beste
wurst
oktoberfest
Enter text: how much wod can a woodchuck chuck
wod
woodchuck
Enter text: to be or not to be that is the question
Enter text:
Brainstorming
- To accomplish the task there are two streams of information to process.
- One stream is the input text; the other stream is the list of word spellings. Processing these streams requires different actions, so we need to be sure when we read from a stream, that we pick the right one.
- To make a decision on a word, we need to know if its in the list of correctly-spelled words. This fact tell us that we should get the list of word spellings before we get the user text — it would not really help to get the words first, and it would be a real mess to try to interleaving the stream reading.
- Together the above tell us a plan of action.
- Import our
url
module.
- Specify the URL for the spelling dictionary.
- Read the contents of the URL and store it as a list of word spellings.
- Get the user text and convert it into a list of words.
- For each user word determine whether it is correctly spelled. If it is incorrectly spelled, print it out
Problem who_are_the_longest_of_them_all.py
- Determine the words of maximum length in user text
Three possible program runs
Enter text: Think before I act — My problem solving mantra — Make it your also
['problem', 'solving']
Enter text: Remember always — Seek to be both great and good — The world needs your aid
['Remember']
Enter text:
[]
Brainstorming
- ???
© 2019 Jim Cohoon | Resources from previous semesters are available. |