See explaination
Explanation:
This function reverses the position
of the consonants keeping the
position of vowel constant.
Parameters:
data : type(str)
The string on which operation
needs to be performed
start : type(int)
Starting index of the string
end : type(int)
Ending index of the string
"""
def consonantReverse(data, start = 0, end = None):
# By default user will psas the data only.
# End will be none which will be set to length of string - 1
if(end == None):
return consonantReverse(data, 0, len(data)-1)
# return as this is invalid
if(start >= end):
return data
# split the data into list
if type(data)== str:
data = list(data)
# initialize index1 and index2
index1 = len(data)
index2 = -1
# find out the first index from beggining
# where consonant is present
for i in range(start, end+1):
if(data[i] not in ['a','e','i','o','u','A','E','I','O','U']):
index1 = i
break
# find out the index from the end
# where consonant is present
for i in range(end, start-1, -1):
if(data[i] not in ['a','e','i','o','u','A','E','I','O','U']):
index2 = i
break
# return as this is not valid case
if(index1 >= index2):
return ''.join(data)
# swap the data in the two index
data[index1], data[index2] = data[index2], data[index1]
# call the function recursively
data = consonantReverse(data, index1+1, index2-1)
# if it is a list, join the list into string
if(type(data) == list):
return ''.join(data)
# otherwise return the data
else:
return data