8.4.3. Homework#
Use these questions after class to check your understanding of the chapter concepts and coding skills.
Sign in from the account menu before you start working on the homework.
Complete all true/false and coding questions before you submit the homework.
Launch Live Code (the rocket) to write and run code in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. String Immutability
A cleaning script should create a new cleaned string instead of expecting a string method to mutate the original text in place.
2. Normalization
Lowercasing and stripping whitespace can help make text comparisons more consistent.
3. Splitting Text
split() always preserves punctuation as separate tokens without extra cleaning.
4. Regular Expressions
A regular expression is useful when a script needs to find text that follows a pattern such as several digits.
5. Text Frequency
A word-frequency report should usually treat Data and data as different words when capitalization is not meaningful.
### 6. Normalize a label
# Strip extra spaces and title-case the label. Print exactly this line:
# # Label: Quarterly Revenue
label = " quarterly revenue "
### Your code starts here.
### Your code ends here.
Label: Quarterly Revenue
### 7. Split and clean words
# Create lowercase words without commas. Print exactly this line:
# # Words: ['data', 'science', 'data']
text = "Data, science, data"
### Your code starts here.
### Your code ends here.
Words: ['data', 'science', 'data']
### 8. Use a regex pattern
# Use `re.findall` to collect ticket numbers. Print exactly this line:
# # Ticket numbers: ['12', '105']
message = "Ticket 12 moved before ticket 105."
### Your code starts here.
### Your code ends here.
Ticket numbers: ['12', '105']
### 9. Count normalized words
# Build a word count dictionary. Print exactly this line:
# # Counts: {'python': 2, 'data': 1}
text = "Python data PYTHON"
### Your code starts here.
### Your code ends here.
Counts: {'python': 2, 'data': 1}
### 10. Build a successor map
# Split sentence into words. Build successor_map, a dict that maps each
# word to the list of words that follow it in the sentence (use
# setdefault, or check whether the word is already a key).
# Write generate(word, length) that returns a list of words starting
# with word: while the list has fewer than length words and word has
# successors, move word to the first word in its successor list and
# append it. Stop early when word has no successors.
# Print the successors of "the", then the words generated from "the"
# and from "dog" with length 6, joined with spaces.
# Print exactly these lines:
# # Successors of 'the': ['cat', 'mat', 'dog']
# # From 'the': the cat sat on the cat
# # From 'dog': dog ran away
sentence = "the cat sat on the mat and the dog ran away"
### Your code starts here.
### Your code ends here.
Successors of 'the': ['cat', 'mat', 'dog']
From 'the': the cat sat on the cat
From 'dog': dog ran away