I enjoyed this one; I'm always a fan of mixing poetry and code. My Python solution is below.
I decided that the haiku came as single strings, with lines separated by \n, so the solution only works with haiku in that format. The only module required is re, for vowel counting.
Code
The solution rests on three small functions. The first is the clean_haiku function, which takes a single haiku string and processes it, returning a list of lower-case lines with all punctuation removed.
The next function takes a single line as a string and returns a list of syllables. It does this by splitting the line into words, and then, for each word, counting clusters (1+) of vowels. The number of clusters in a word is (once you account for edge cases) the number of syllables.
The final function - validate_haiku takes a haiku string and calls clean_haiku to get processed lines, and then count_syllables for each line. If the final list of syllable counts matches [5, 7, 5], then the function returns True.
Web and Software Frontend Developer. Used to work in Full-stack JavaScript.Been at this for a few years now, and think I now understand my specific domain pretty well. Still a lot to learn though.
Location
Manchester, UK
Education
Web Systems Design BSc at Sheffield Hallam University, Sheffield, UK
You're absolutely right - my code passes the tests outlined above, but there are various edge cases that trip it up. "Sometimes" is one of them, because it's only two syllables really but contains two silent "e"s which register as syllables with this simplistic definition. Another one is "abalone", because the "e" at the end is pronounced as a separate syllable.
You can get the function closer to correct by changing the count_syllables function like so
Counting syllables is actually a really complex problem if you want to hit all the edge cases - this article goes into it more deeply. In the end, the best way is to use some kind of dictionary lookup, but that will still fail with unknown words.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I enjoyed this one; I'm always a fan of mixing poetry and code. My Python solution is below.
I decided that the haiku came as single strings, with lines separated by
\n
, so the solution only works with haiku in that format. The only module required isre
, for vowel counting.Code
The solution rests on three small functions. The first is the
clean_haiku
function, which takes a single haiku string and processes it, returning a list of lower-case lines with all punctuation removed.The next function takes a single line as a string and returns a list of syllables. It does this by splitting the line into words, and then, for each word, counting clusters (1+) of vowels. The number of clusters in a word is (once you account for edge cases) the number of syllables.
The final function -
validate_haiku
takes a haiku string and callsclean_haiku
to get processed lines, and thencount_syllables
for each line. If the final list of syllable counts matches[5, 7, 5]
, then the function returnsTrue
.Tests
Lemme check if this works though:
test shows false for it :p
You're absolutely right - my code passes the tests outlined above, but there are various edge cases that trip it up. "Sometimes" is one of them, because it's only two syllables really but contains two silent "e"s which register as syllables with this simplistic definition. Another one is "abalone", because the "e" at the end is pronounced as a separate syllable.
You can get the function closer to correct by changing the
count_syllables
function like sobut it still struggles with that "ome".
Counting syllables is actually a really complex problem if you want to hit all the edge cases - this article goes into it more deeply. In the end, the best way is to use some kind of dictionary lookup, but that will still fail with unknown words.