In: Computer Science
You are a songwriter, tired and upset because of all the lame easy rhymes that are in the most popular songs today. You decide to write a program that checks if two words rhyme. The program ignores and discards perfect rhymes such as rat and bat or glove and dove. The program returns a rhyme percentage based on the pronunciation of the words.
Because the program ignores perfect rhymes, words that rhyme perfectly will receive a score higher than a certain amount. If a pair of pronunciations receive a score of 75% or higher, it is considered a perfect rhyme. In such a case the program must return -1. However, if the pronunciations receive a score of 40% or lower, the rhyme is too weak to use and the program must return -999. Otherwise the program must return the rhyme (percentage) score.
A rhyme score is calculated by comparing the letters of each syllable of two words:
The rhyme score is then determined by the total score divided by the total number of letters in the largerpronunciation (excluding the underscores if any).
Write a function bRhymes() that accepts 2 pronunciations as strings and returns the rhyme percentage rounded to 1 decimal place, -1 if it is a perfect rhyme or -999 if the rhyme is too weak.
Code is already provided for the program which runs your function bRhymes().
Input Format
[PROGRAM INPUT]
Two lines each containing a string, such as ahi_dee_uhl_ahiz (in Line 1) and ree_uhl_ahiz (in Line 2). The first line for the pronunciation of the first word and the second line for the pronunciation of the second word. An example of the exact input would appear as:
ahi_dee_uhl_ahiz
ree_uhl_ahiz
.
[FUNCTION bRhymes() INPUT]
2 strings which represents rhyme pronunciations
Constraints
where w1 and w2 are pronunciations
Output Format
[PROGRAM OUTPUT]
A single float, such as 69.2 which represents the rhyme percentage rounded to 1 decimal place, -1 if it is a perfect rhyme or -999 if the rhyme is too weak. An example of the exact output would appear as:
69.2
def bRhymes(f,s): #function definition
f1=f.split("_") #splitting
pronounciations into lists
f2=s.split("_")
if len(f1)>len(f2): #finding the largest
pronounciation to l1
#smallest tp l2
l1=f1
l2=f2
else:
l1=f2
l2=f1
s1=len(l1) #finding the lenght of
lists
s2=len(l2)
score=0
for i in l1:
for j in l2:
#for every word in l1 get l2
if len(i)<=len(j): #if l1
word is less than l2 word
l=0 #l
to access the largest word character
count=0 #count=0
for k in range(len(i)):
if i[k]==j[l]: #if charatcers are equal count++
count=count+1
if(count>1): #if more than one
consecutive charatcers are equal add it to score
score=score+count
l=l+1 # l to access largest word character
else: #if l2 word greater
than l1 word
l=0 #l to access smallest word
charatcer
count=0
for k in range(len(j)):
if j[k]==i[l]: #if characters are equal
count++
count=count+1
if(count>1): #if more than one consecutive
characters are equal add it to score
score=score+count
l=l+1
score=score+count
totalCharacters=0
#finding the total characters in largest pronounciation
for i in l1:
totalCharacters=totalCharacters+len(i)
score=score/len(l1)
#total score= score/ letters in largest pronounciation
total=score/(len(l1)+len(l2))*100
#finding score percentage
total=round(total,1) #rounding
percentage to one decimal
if(total>=75): #if
percentage >75 rhyming is perfect so return -1
return -1
elif(total<=40): #if
percentage<40 rhyming is weak so return -999
return -999
else:
#returning the score percentage
return total
f='ahiz_ahi_dee_uhl'
s='ree_uhl_ahiz'
print(bRhymes(f,s))
output:
-1