Have the function LetterChanges(str). Take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a).
Then capitalize every vowel in this new string (a, e, i, o, u) and finallyreturn this modified string.
I want this problem done in 2 lines
a= lambda stri:([(chr(ord(i) + 1)) for i in stri]) #if i not in ("a","e","i","o","u")
print(a("bcdefgh"))
I know that if part is wrong, to understand clearly, I included it in comment.
Expected output is "cdEfgI".
2 Answers 2
Your expected output does not match your input, you are missing an h before the final I.
Assuming this is just a typo, your problem can be solved with:
>>> a = lambda stri: ''.join('A' if i == 'z' else chr(ord(i) + (-31 if i in "dhnt" else 1)) for i in stri)
>>> print(a("bcdefgh"))
cdEfghI
Explanation:
- first check if
iis azthen return anA - otherwise check if
iis any character preceding a vowel in the alphabet then subtract 31 from it to get the capitalized vowel - if that is not the case, increase the character by one
Comments
Here's the normal code (that you should have already made):
def change_letters(string):
result = ''
for letter in string:
# This makes sure that 'z' goes to 'a'
next_letter = chr((ord(letter) - 98)%26 + 97)
if next_letter in 'aeiou':
result += letter.upper()
else:
result += letter
return string
If we follow the steps in my other answer here, we end up with the following:
change_letters = lambda string: ''.join(chr((ord(letter) - 98)%26 + 97).upper() if chr((ord(letter) - 98)%26 + 97) in 'aeiou' else chr((ord(letter) - 98)%26 + 97) for letter in string)
But please note, this is a HORRIBLE thing to do, especially for your future self and others you may one day be working with. I would not want to see this again :)