Find first 5-7-5 haiku in sentence
Problem Description
Implement a function find_haiku(sentence: str, syllable_dict: dict) -> list[str] | None that identifies and returns the first contiguous three-line haiku within the given sentence. A haiku is defined as three consecutive lines with 5, 7, and 5 syllables respectively.
The syllable_dict maps lowercase words to their corresponding syllable counts.
Requirements:
- Input Handling: The input
sentencemay contain mixed case and punctuation. - Dictionary Lookup:
- Word tokens should be treated case-insensitively for dictionary lookups.
- Leading and trailing punctuation must be stripped from tokens before matching keys in the
syllable_dict.
- Output Format: If a haiku is found, return a list of three strings, where each string represents a line of the haiku. The original casing and punctuation of the words within these lines must be preserved.
- Haiku Structure: The three lines of the haiku must be formed by partitioning a single contiguous subsequence of words from the original sentence into three consecutive segments.
- Efficiency: Aim for O(N) time complexity and O(N) space complexity, where N is the number of words in the sentence. Techniques like prefix sums and hashing are suggested.
- Return Value: Return
Noneif no such haiku subsequence exists in the sentence.
Edge Cases to Consider:
- Handling of unknown words (words not present in
syllable_dict). - Words containing apostrophes (e.g., "don't").
- Words with trailing punctuation (e.g., "word,").
- Sentences with multiple spaces between words.
AI interview prep
Core dilemma
Efficiently scanning a sequence of words for specific syllable sum patterns (5, 7, 5) while robustly handling complex text normalization for dictionary lookups and preserving original formatting for output. This involves balancing precise word boundary detection and normalization with the need to retain original tokens.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find first 5-7-5 haiku in sentence
Problem Description
Implement a function find_haiku(sentence: str, syllable_dict: dict) -> list[str] | None that identifies and returns the first contiguous three-line haiku within the given sentence. A haiku is defined as three consecutive lines with 5, 7, and 5 syllables respectively.
The syllable_dict maps lowercase words to their corresponding syllable counts.
Requirements:
- Input Handling: The input
sentencemay contain mixed case and punctuation. - Dictionary Lookup:
- Word tokens should be treated case-insensitively for dictionary lookups.
- Leading and trailing punctuation must be stripped from tokens before matching keys in the
syllable_dict.
- Output Format: If a haiku is found, return a list of three strings, where each string represents a line of the haiku. The original casing and punctuation of the words within these lines must be preserved.
- Haiku Structure: The three lines of the haiku must be formed by partitioning a single contiguous subsequence of words from the original sentence into three consecutive segments.
- Efficiency: Aim for O(N) time complexity and O(N) space complexity, where N is the number of words in the sentence. Techniques like prefix sums and hashing are suggested.
- Return Value: Return
Noneif no such haiku subsequence exists in the sentence.
Edge Cases to Consider:
- Handling of unknown words (words not present in
syllable_dict). - Words containing apostrophes (e.g., "don't").
- Words with trailing punctuation (e.g., "word,").
- Sentences with multiple spaces between words.