Homework

8.4.3. Homework#

Apply the chapter to new text inputs. Each coding cell must define the named function and run independently. Grading checks the printed example and alternate inputs, including malformed and empty cases.

  • Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.

  • Sign in from the account menu before you start working on the assignment.

  • Write and run your code directly in the coding cells.

  • Multiple submissions are allowed. The highest score is kept.

1. String Immutability

A report stores raw text for later review. Assigning the result of strip() to a new variable preserves the original string.

2. Numeric Conversion

A script checks user_input.isdigit() before calling int(user_input). This guarantees that the conversion will succeed.

3. Whitespace Policy

A pipeline intentionally collapses repeated spaces by splitting on whitespace and joining with one space. Its result may differ from the original text.

4. Whole-ID Validation

A validator accepts an employee ID whenever re.match() finds the required prefix, even when extra characters follow it. This validates the entire ID.

5. Successor Probabilities

A successor list contains sat twice and slept once. Choosing a list position uniformly makes sat twice as likely as slept.

### 6: Normalize and document a label
# Define normalize_label(text: str) -> str, with a docstring describing its parameter,
# return value, and ValueError. Strip outer whitespace, reject an empty result with
# ValueError, and return the casefolded label. Print the supplied label's result.
# Print exactly:
# Label: strasse
label = '  Straße  '
### Your code starts here.



### Your code ends here.
### 7: Compare tokenization policies
# Define tokenize(text, split_hyphens=False). Use the lab's boundary punctuation
# . , ! ? ; : " ( ), lowercase, replace em dashes with spaces, and discard empty tokens.
# Preserve internal hyphens unless split_hyphens is True; then replace them with spaces.
# Print both token lists. This question must run without executing the lab.
# Print exactly:
# Keep hyphens: ['data', 'data', 'pocket-handkerchief']
# Split hyphens: ['data', 'data', 'pocket', 'handkerchief']
text = 'DATA, data! pocket-handkerchief !!!'
### Your code starts here.



### Your code ends here.
### 8: Extract ticket-specific captures
# Define ticket_numbers(message) to return strings captured from ticket mentions:
# the whole word Ticket (case-insensitive), one space, then one or more ASCII digits
# ending at a word boundary. Use a capturing group with re.findall().
# Ignore unrelated dates and room numbers. Print the list from message.
# Print exactly:
# Ticket numbers: ['12', '105']
message = 'On 2024-03-15, Ticket 12 moved before ticket 105; room 7.'
### Your code starts here.



### Your code ends here.
### 9: Verify equivalent frequency reports
# Define frequencies(text) to return a manual word-count dictionary. Split on whitespace,
# strip boundary punctuation . , ! ? ; : " ( ), lowercase, and discard empty results.
# Independently build counter_counts with Counter on a cleaned token list using the same
# policy. Print the manual counts and whether the two dictionaries agree.
# Do not import a function from another question.
# Print exactly:
# Counts: {'python': 2, 'data': 1}
# Agree: True
text = 'Python, data PYTHON! !!!'
### Your code starts here.



### Your code ends here.
### 10: Model repeated successors and stop safely
# Define build_successors(sentence) to map each whitespace-separated word to its list
# of following words, keeping repetitions. Do not add an entry solely for a final word.
# Define generate(successor_map, word, length) to return at most length words, including
# the start. Choose the FIRST successor deterministically (this tests traversal, not
# random generation). Stop on a missing or empty successor list. length is a positive int.
# Print the successors of "cat", the probability of "sat" given "cat" to two decimals,
# and the two deterministic sequences shown below. Compute probability from the list.
# Print exactly:
# Successors of cat: ['sat', 'sat', 'slept']
# P(sat | cat): 0.67
# From cat: cat sat cat sat cat sat
# From slept: slept
sentence = 'cat sat cat sat cat slept'
### Your code starts here.



### Your code ends here.