Apologies for the possibly not-very-useful title, I couldn’t figure out how to summarise this problem into one sentence.
I’m trying to count how many “units” long a word is in Python 3.10. One “unit” is (C for consonant and V for vowel) either CV or VC or C or V (the latter two only being used when no pair can be made). For example, “piece” would be three units (pi-e-ce
or pi-ec-e
), “queue” would be four units (qu-e-u-e
), and “lampshade” would be six units (la-m-p-s-ha-de
).
What I’m struggling with is how exactly I would detect these units without iterating through every combination of every vowel and consonant for each pair of letters. It’d be hugely inefficient to do this with iteration, but I can’t think of anything better with my current knowledge of Python. What would be an efficient way of solving this problem?
As an extra (optional) problem, what if digraphs are introduced, like “gh” and “th”? This would make words like “thigh” (four units, t-hi-g-h
) into only two units (thi-gh
), but also complicates the working-out.
Thanks in advance!
Suntooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.