In: Computer Science
Problem 1 - Find the Diphthongs
A diphthong is a composite vowel sound composed of two simple vowel sounds. We're going to be looking for these in words. For instance "ay" is normally a diphthong for instance in the words "lay" and "day", as is "ou" for instance in "loud," "lout."
We're going to simplify this by looking only for two vowels in a row in a word regardless of whether they are sounded out as diphthongs or not.
Let me show some examples of the counting:
[Ae]rat[io]n; count = 2
F[ou]ndat[io]n; count = 2
Q[ue][ue]ing; count = 2
The reason that we are counting this as two, is because once we've used a vowel as part of a diphthong do not use it again.
Note also that after the last pair, there's another vowel, but since the last e was used, the i doesn't pair, so it is not part of a diphthong.
P[ae]an; count = 1
Several; count = 0
[Ae][ae][ae][ae][ae]; count = 5
This is obviously an invented word, but who cares, it's composed of five diphthong pairs.
Glor[ia]; count = 1
For this exercise, the vowels are a, e, i, o, u, and y.
Take in a word, output each diphthong pair and finally output the count.
In terms of spaces, commas, and periods, let the words be divided, so don't count a dipthong over a space or other grammatical mark, meaning that for instance "a year" should group like "a [ye]ar" rather than "[a y][ea]r."
Sample Output
linux1[150]% python3 find_diphthongs.py Enter a string with a lot of diphthongs: aeration ae io The diphthong count is 2 linux1[151]% python3 find_diphthongs.py Enter a string with a lot of diphthongs: aeitiour ae io The diphthong count is 2 linux1[152]% python3 find_diphthongs.py Enter a string with a lot of diphthongs: ayayay ay ay ay The diphthong count is 3 linux1[153]% python3 find_diphthongs.py Enter a string with a lot of diphthongs: argument The diphthong count is 0 |
If you want to eliminate a space between your letters and you're printing the characters separately, use:
print('a', 'e', sep='')
save as find_diphthongs.py
# Function that return true
# if character ch is a vowel
def isVowel(ch):
if ch in ['a', 'e', 'i', 'o', 'u','y']:
return True
else:
return False
# Function to return the count of adjacent
# vowel pairs in the given string
def vowelPairs(s, n):
cnt = 0
i=0
while i<n-1:
step=1
# If current character and the
# character after it are both vowels
if (isVowel(s[i]) and isVowel(s[i + 1])):
print(s[i]+s[i+1])
step = 2 #skip the used a vowel as part of a diphthong do not use it again
cnt += 1 #count the diphthong
i= i + step
return cnt
# Driver code
def main():
text=input("Enter a string with a lot of diphthongs: ")
n = len(text)
c=vowelPairs(text, n)
print("The diphthong count is ",c)
if __name__ == "__main__":
main()