DEV Community

Discussion on: Daily Challenge #229 - Haiku Validator

Collapse
 
georgewl profile image
George WL • Edited

Lemme check if this works though:

"Haikus are easy\n
But sometimes they don’t make sense\n
Refrigerator"

test shows false for it :p

Collapse
 
peritract profile image
Dan Keefe

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

if (word.endswith("e") or word.endswith("es")) and len(matches) > 1:

but 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.