Open In App

Python – Mirror Image of String

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a String, perform its mirror imaging, return “Not Possible” if mirror image not possible using english characters.

Input : test_str = ‘boid’ Output : doib Explanation : d replaced by b and vice-versa as being mirror images. Input : test_str = ‘gfg’ Output : Not Possible Explanation : Valid Mirror image not possible.

Method : Using loop + lookup dictionary

This is one way in which this task can be performed. In this, we construct lookup dictionary for all valid mirrorable english characters, then perform task of access from them.

Python3




# Python3 code to demonstrate working of
# Mirror Image of String
# Using Mirror Image of String
 
# initializing strings
test_str = 'void'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing mirror dictionary
mir_dict = {'b':'d', 'd':'b', 'i':'i', 'o':'o', 'v':'v', 'w':'w', 'x':'x'}
res = ''
 
# accessing letters from dictionary
for ele in test_str:
    if ele in mir_dict:
        res += mir_dict[ele]
     
    # if any character not present, flagging to be invalid
    else:
        res = "Not Possible"
        break
 
# printing result
print("The mirror string : " + str(res))


Output

The original string is : void
The mirror string : voib

Time Complexity: O(n)

Space Complexity: O(n)

 Method 2  : using string slicing to reverse the string and a lookup dictionary to replace the mirrored characters.

 step by step approach:

Define the input string.
Define a dictionary that maps mirrored characters to their respective values.
Reverse the input string using string slicing.
Iterate over the reversed string and replace each character with its mirror image from the dictionary. If the character is not present in the dictionary, set the result string to “Not Possible” and break the loop.
Reverse the result string back to its original order.
Print the original and mirror strings.

Python3




# Python3 code to demonstrate working of
# Mirror Image of String
# Using String Slicing and Lookup Dictionary
 
# Define input string
test_str = 'void'
 
# Define mirror dictionary
mir_dict = {'b': 'd', 'd': 'b', 'i': 'i', 'o': 'o', 'v': 'v', 'w': 'w', 'x': 'x'}
 
# Reverse the input string
rev_str = test_str[::-1]
 
# Initialize result string
res = ''
 
# Iterate over reversed string and replace mirrored characters
for ele in rev_str:
    if ele in mir_dict:
        res += mir_dict[ele]
    else:
        res = "Not Possible"
        break
 
# Reverse the result string
mir_str = res[::-1]
 
# Print the original and mirror strings
print("The original string is : " + str(test_str))
print("The mirror string : " + str(mir_str))


Output

The original string is : void
The mirror string : voib

The time complexity of this approach is O(n) as it involves iterating over the string only once. 

The auxiliary space complexity is also O(n) because we create a new reversed string and a new mirrored string.



Similar Reads

Python Dictionary to find mirror characters in a string
Given a string and a number N, we need to mirror the characters from the N-th position up to the length of the string in alphabetical order. In mirror operation, we change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on. Examples: Input : N = 3 paradox Output : paizwlc We mirror characters from position 3 to end. Input : N = 6 pneumonia Output : pneumlmrz We hav
2 min read
Python Program for focal length of a spherical mirror
Focal length is the distance between the center of the mirror to the principal foci. In order to determine the focal length of a spherical mirror we should know the radius of curvature of that mirror. The distance from the vertex to the center of curvature is called radius of curvature. The focal length is half the radius of curvature. Formula : F
2 min read
Python Program for Mirror of matrix across diagonal
Given a 2-D array of order N x N, print a matrix that is the mirror of the given tree across the diagonal. We need to print the result in a way: swap the values of the triangle above the diagonal with the values of the triangle below it like a mirror image swap. Print the 2-D array obtained in a matrix layout. Examples: Input : int mat[][] = {{1 2
4 min read
Image processing with Scikit-image in Python
scikit-image is an image processing Python package that works with NumPy arrays which is a collection of algorithms for image processing. Let's discuss how to deal with images in set of information and its application in the real world. Important features of scikit-image : Simple and efficient tools for image processing and computer vision techniqu
2 min read
Convert OpenCV image to PIL image in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a
3 min read
Overlay an image on another image in Python
Overlaying an image over another refers to the process of copying the image data of one image over the other. Overlaying could refer to other types of image processing methods as well such as overlaying similar images for noise reduction, Blending, etc. But for now, we will concentrate on the former one. In this article, we will learn how to overla
4 min read
Convert Text Image to Hand Written Text Image using Python
In this article, we are going to see how to convert text images to handwritten text images using PyWhatkit, Pillow, and Tesseract in Python. Module needed: Pytesseract: Sometimes known as Python-tesseract, is a Python-based optical character recognition (OCR) program. It can read and recognize text in photos, license plates, and other documents. To
2 min read
Image Segmentation using Python's scikit-image module
The process of splitting images into multiple layers, represented by a smart, pixel-wise mask is known as Image Segmentation. It involves merging, blocking, and separating an image from its integration level. Splitting a picture into a collection of Image Objects with comparable properties is the first stage in image processing. Scikit-Image is the
14 min read
Converting an image to ASCII image in Python
Introduction to ASCII art ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII-compliant character sets with proprietary extended characters (beyond the 128 characters of standard
9 min read
Mahotas - Labelled Image from the Normal Image
In this article we will see how we can create a labelled image from the normal image in mahotas. For this we are going to use the fluorescent microscopy image from a nuclear segmentation benchmark. We can get the image with the help of command given below mahotas.demos.nuclear_image() Below is the nuclear_image Labelled images are integer images wh
2 min read
Practice Tags :
three90RightbarBannerImg