Reputation: 3
For the following code, I want the output so that there is a space to match each word in the input string after each consonant in a word has been converted. My current strategy is using lists but it prevents me from putting a space between each word.
Example- if input is: "a cat", result should be "a CashaTut", instead of "aCashaTut".
string = input("Enter input string: ")
list = string.split(' ')
output_1 = []
def converted_(word):
for letter in word:
if letter in 'Bb':
return word.replace(letter, "Bub")
elif letter in 'Cc':
return word.replace(letter, 'Cash')
elif letter in 'Dd':
return word.replace(letter, 'Dud')
elif letter in 'Ff':
return word.replace(letter, 'Fud')
elif letter in 'Gg':
return word.replace(letter, 'Gug')
elif letter in 'Hh':
return word.replace(letter, 'Hash')
elif letter in 'Jj':
return word.replace(letter, 'Jay')
elif letter in 'Kk':
return word.replace(letter, 'Kuck')
elif letter in 'Ll':
return word.replace(letter, 'Lul')
elif letter in 'Mm':
return word.replace(letter, 'Mum')
elif letter in 'Nn':
return word.replace(letter, 'Nun')
elif letter in 'Pp':
return word.replace(letter, 'Pub')
elif letter in 'Qq':
return word.replace(letter, 'Quack')
elif letter in 'Rr':
return word.replace(letter, 'Rug')
elif letter in 'Ss':
return word.replace(letter, 'Sus')
elif letter in 'Tt':
return word.replace(letter, 'Tut')
elif letter in 'Vv':
return word.replace(letter, 'Vuv')
elif letter in 'Ww':
return word.replace(letter, 'Wack')
elif letter in 'Xx':
return word.replace(letter, 'Ex')
elif letter in 'Yy':
return word.replace(letter, 'Yub')
elif letter in 'Zz':
return word.replace(letter, 'Zub')
else:
return word
for i in range(len(list)):
word = list[i]
for j in range(len(word)):
letter = word[j]
var_part = funny_2(letter)
output_1.append(var_part)
a = ''.join(output_1)
print(a)
Upvotes: 0
Views: 47
Reputation: 1557
As has been pointed out in other answers, there is no requirement to split
the input str
within your program.
I would have a think about the if
/elif
/else
logic within your code. In cases where no substitution is required, each of these conditions will be checked at runtime until the else
statement is hit. Similarly, for iterations on the character z
/Z
, each of the previous conditions will be checked before a True
value is returned by elif letter in 'Zz':
. This probably won't cause long waits for execution given a sufficiently short input str
, however from an efficiency and readability perspective it probably isn't the best practice.
The below solution makes use of a dict
(mapping
) which has keys for each consonant which are mapped to values representing the value which will be used to replace them if found in the input str
.
mapping = {
'b': 'Bub',
'c': 'Cash',
'd': 'Dud',
'f': 'Fud',
'g': 'Gug',
'h': 'Hash',
'j': 'Jay',
'k': 'Kuck',
'l': 'Lul',
'm': 'Mum',
'n': 'Nun',
'p': 'Pub',
'q': 'Quack',
'r': 'Rug',
's': 'Sus',
't': 'Tut',
'v': 'Vuv',
'w': 'Wack',
'x': 'Ex',
'y': 'Yub',
'z': 'Zub'
}
def converted_(word):
return ''.join([mapping.get(c.lower()) or c for c in word])
string = input("Enter input string: ")
output = converted_(string)
print(output)
Now the converted_
function uses a list
comprehension to return a str
in constructed by iterating across each character of the str
passed at runtime (word
) and adding either:
The value mapped to the current character in mapping
- if the current character converted to lowercase exists as a key within mapping
.
The current character - if a lowercase conversion of it does not exist as a key within mapping
.
In this implementation, only 1 comparison is necessary for each character to be converted properly - i.e. "Is a lowercase version present as a key in mapping
?" - as opposed to your original code in which 1...21
comparisons are made to convert each character depending on its value.
Upvotes: 1
Reputation: 1268
why to split by space at all? just loop over the entire line and spaces will remain.
try this:
def converted_(line):
new_string = ''
for letter in line:
if letter in 'Bb':
new_string+="Bub"
elif letter in 'Cc':
new_string+='Cash'
elif letter in 'Dd':
new_string+='Dud'
elif letter in 'Ff':
new_string+='Fud'
elif letter in 'Gg':
new_string+='Gug'
elif letter in 'Hh':
new_string+='Hash'
elif letter in 'Jj':
new_string+='Jay'
elif letter in 'Kk':
new_string+='Kuck'
elif letter in 'Ll':
new_string+='Lul'
elif letter in 'Mm':
new_string+='Mum'
elif letter in 'Nn':
new_string+='Nun'
elif letter in 'Pp':
new_string+='Pub'
elif letter in 'Qq':
new_string+='Quack'
elif letter in 'Rr':
new_string+='Rug'
elif letter in 'Ss':
new_string+='Sus'
elif letter in 'Tt':
new_string+='Tut'
elif letter in 'Vv':
new_string+= 'Vuv'
elif letter in 'Ww':
new_string+='Wack'
elif letter in 'Xx':
new_string+='Ex'
elif letter in 'Yy':
new_string+='Yub'
elif letter in 'Zz':
new_string+='Zub'
else:
new_string+=letter
return new_string
m_in = input("Enter input string: ")
m_out = converted_(m_in)
print(m_out)
output:
Enter input string: a cat
a CashaTut
Upvotes: 2