Find the Mirror Image of a String using Python


In the given problem statement we are required to find the mirror image of a given string with the help of Python code.

Understanding The Problem

The problem at hand is to find the mirror image of the given string. The mirror image is the changed version of the given string in which every character is replaced with the mirror image of that character. Or we can say it is the reflection of the string. In real life we see ourselves in the mirror so our right part is visible in the left side in the mirror and similarly the right part shows in the left side.

Input-Output Scenario

Following is an input and its output example of mirror image of the given string −

Input string = TUTOR
Output string: ROTUT

Logic for The Above Problem

To solve this problem, first we will reverse the given string with the help of the reversed function of python. And then we will replace each character with the corresponding mirror image of that character.

Algorithm

  • Step 1 − Define the function called get_mirror_image. And pass an argument called str.

  • Step 2 − Define the mirror characters dictionary as mirror_chars, this will contain all the mirror characters as a dictionary.

  • Step 3 − Iterate over the reversed string and replace the character with its mirror image from the above dictionary.

  • Step 4 − Reverse the input string using the reversed function. And store the mirror string in mirror_string variable.

  • Step 5 − Now get the input from the user and print the original and mirror strings.

Example

def get_mirror_image(str):
   mirror_chars = {
      'A': 'A', 'B': 'B', 'C': 'C', 'D': 'D', 'E': 'E', 'F': 'F', 'G': 'G', 'H': 'H', 'I': 'I', 'J': 'J', 'K': 'K', 'L': 'L', 'M': 'M', 'N': 'N', 'O': 'O', 'P': 'P', 'Q': 'Q', 'R' : 'R', 'S': 'S', 'T': 'T', 'U': 'U', 'V': 'V', 'W': 'W', 'X': 'X', 'Y': 'Y', 'Z': 'Z', ' ': ' ', 'a': 'a', 'b': '', 'c':'c', 'd':'d', 'e':'e', 'f':'f', 'g':'g', 'h':'h', 'i':'i', 'j':'j', 'k':'k', 'l':'l', 'm':'m', 'n':'n', 'o':'o', 'p':'p', 'q':'q', 'r':'r', 's':'s', 't':'t', 'u':'u', 'v':'v', 'w':'w', 'x':'x', 'y':'y', 'z':'z'
   }

   # Reverse and replace characters with their mirror characters
   mirror_string = ''.join(mirror_chars.get(char, '') for char in reversed(str))

   return mirror_string

# Testing the function
original_string = input("Enter a String: ")
mirror_string = get_mirror_image(original_string)

print("Your String is: ", original_string)
print("Mirror image of your string: ", mirror_string)

Output

Enter a String: Tutorialspoint 
Your String is:  Tutorialspoint 
Mirror image of your string:   tniopslairotuT

Complexity

In the code we have used a dictionary to store all the mirror characters. And took user input for the string. So based on the input, let’s say its size is n, the time complexity is O(n). Because we have used a reversed function which is used to reverse the given string and it is iterating the string for O(n) time. And to check the mirror character in the defined dictionary it takes O(1), constant time. So overall complexity is O(n).

Conclusion

The code has successfully found the mirror image of the given string. As we have learned the usage of the reversed function of Python and created a dictionary to store the mirror characters.

Updated on: 16-Oct-2023

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements