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,177
  • 4
  • 18
  • 35

2 Answers2

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
kalehmann
  • 4,821
  • 6
  • 26
  • 36
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 :)

GeeTransit
  • 1,458
  • 9
  • 22