w3resource

Python: Find string s that, when case is flipped gives target where vowels are replaced by chars two later

Python Programming Puzzles: Exercise-40 with Solution

Write a Python program to find strings that, when case is flipped, give a target where vowels are replaced by characters two later.

Input: Python 
Output:
pYTHQN

Input: aeiou
Output:
CGKQW

Input:  Hello, world!
Output:
hGLLQ, WQRLD!

Input: AEIOU 
Output:
cgkqw

Pictorial Presentation:

Python: Find string s that, when case is flipped gives target where vowels are replaced by chars two later.
Python: Find string s that, when case is flipped gives target where vowels are replaced by chars two later.

Sample Solution-1:

Python Code:

#License: https://bit.ly/3oLErEI

def test(strs):
     return strs.translate({ord(c):ord(c)+2 for c in "aeiouAEIOU"}).swapcase()

strs = "Python" 
print("Original string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs))
strs = "aeiou" 
print("\nOriginal string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs))
strs = "Hello, world!" 
print("\nOriginal string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs))
strs = "AEIOU" 
print("\nOriginal string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs)) 

Sample Output:

Original string: Python
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
pYTHQN

Original string: aeiou
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
CGKQW

Original string: Hello, world!
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
hGLLQ, WQRLD!

Original string: AEIOU
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
cgkqw

Flowchart:

Flowchart: Python - Find string s that, when case is flipped gives target where vowels are replaced by chars two later.

Sample Solution-2:

Python Code:

#License: https://bit.ly/3oLErEI

def test(strs):
    return strs.swapcase().translate({ord('a'): ord('c'),
                                        ord('e'): ord('g'),
                                        ord('i'): ord('j'),
                                        ord('o'): ord('q'),
                                        ord('u'): ord('x'),
                                        ord('A'): ord('C'),
                                        ord('E'): ord('G'),
                                        ord('I'): ord('J'),
                                        ord('O'): ord('Q'),
                                        ord('U'): ord('X'),
                                        ord(' '): ord(' ')})

strs = "Python" 
print("Original string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs))
strs = "aeiou" 
print("\nOriginal string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs))
strs = "Hello, world!" 
print("\nOriginal string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs))
strs = "AEIOU" 
print("\nOriginal string:",strs)
print("Find string s that, when case is flipped gives target where vowels are replaced by chars two later:")
print(test(strs))

Sample Output:

Original string: Python
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
pYTHQN

Original string: aeiou
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
CGJQX

Original string: Hello, world!
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
hGLLQ, WQRLD!

Original string: AEIOU
Find string s that, when case is flipped gives target where vowels are replaced by chars two later:
cgjqx

Flowchart:

Flowchart: Python - Find string s that, when case is flipped gives target where vowels are replaced by chars two later.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Determine which triples sum to zero.
Next: Sort numbers based on strings.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.