-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.py
82 lines (70 loc) · 2.15 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
You need to start traversing from index k till end of string and then you can traverse back from end to beginning
At index k you need to check if the character is vowel or consonant
If it vowel you need to convert it into any consonant and vice versa and according you need to convert entire
string into one kind of characters i.e vowels or consonants
Now
The cost of moving from one index to next index is 1 coin
And cost of converting a character into another character is also 1 coin
Output should be total cost to get desired output string
"""
# Vowel Verification Function
def isVowelWord(string):
isVowel = True
vowels = ['a','e','i','o','u', 'A', 'E', 'I', 'O', 'U']
for char in string:
if char in vowels:
pass;
else:
isVowel = False
break;
return isVowel
# Consonant Verification Function
def isConsonantWord(string):
isConsonant = True
vowels = ['a','e','i','o','u', 'A', 'E', 'I', 'O', 'U']
for char in string:
if char not in vowels:
pass;
else:
isConsonant = False
break;
return isConsonant
# Random Input String: aeioaaeiou
string = list(input("String: "))
index = int(input("Index: "))
vowels = ['a','e','i','o','u', 'A', 'E', 'I', 'O', 'U']
# Random Consonant
consonant = 'c'
# Random Vowel
vowel = 'a'
cost = 0
length = len(string)
print("length: ",length)
# Traversing the given String
while(True):
if(string[index] in vowels):
# swap
string[index] = consonant
cost+=1
else:
string[index] = vowel
cost+=1
# Check
if(isVowelWord(string) or isConsonantWord(string)):
break;
#move next
if(index == length-1):
index = 0
string.reverse()
else:
index+=1
cost+=1
print("Cost of entire process: ",cost)
print("Updated String: ", ''.join(string))
"""
Code Analysis:
I picked another Input VIJAYAWADA. But process never stops because this string has
Consonant, Vowel and so on. So the program runs life time may be breaks at a point 2^n with an error.
So if you want to look out possible output please enter the Random input String.
"""