0

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".

Heinz Schilling
2,3474 gold badges23 silver badges36 bronze badges
asked Jun 26, 2019 at 19:03

2 Answers 2

0

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 i is a z then return an A
  • otherwise check if i is 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
answered Jun 26, 2019 at 19:27
Sign up to request clarification or add additional context in comments.

Comments

0

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 :)

answered Jun 26, 2019 at 19:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.