Mirror Characters using Dictionary in Python

In this tutorial, we will be solving a task to mirror characters from a given point of a string using dictionary in Python. If you have worked with Python before, you should know what a string in Python is by now. But, let’s recap what are strings for our Python beginner readers !!

Here we come across a term called Python Dictionary.

Let’s understand the basics of Python Dictionary first, before proceeding further ahead!!!

What is Python Dictionary?

Python Dictionary is nothing but Python language’s form of data structure and another term we use for this can be an associative array. It is a list of items where each item consists of two parts: key and value and these form a pair, the key-value pair. These pairs are comma-separated and are enclosed inside curly braces and the key and value are linked by a colon(:). Let’s take an example :

      d  =  { 1  : 'cat' , 2  :  'dog' , 3 :  'rabbit' }

where, the numbers represent the keys and the terms like cats, dogs, rabbits represent values.

Furthermore, Python supports in-built methods for the dictionary as well and one such function which will be seeing in the problem, later on, is dict(). Its function is simple; to create a dictionary from any type of combination. Hence, let’s see an example :

d = dict ( { 1 : ‘cat’, 2 : ‘dog’ } ) # converted from a combination to a dictionary

In a dictionary, we access the elements,i.e the values, using the keys and this can be done either by enclosing key in square brackets([]) or by using the get() method. So, let’s understand this better using examples :

d = {1 : 'Rose', 2 : 'Lotus' , 3 : 'Orchid'} # given dictionary

# using [] brackets
print(d[1])

# using get() method
print(d.get(3))
Output :
Rose
Orchid

 

Implementation Of The Concept in the Given Problem

In this task, we have a string and a position n and we have to mirror the characters, i.e, convert the characters ‘a’ to ‘z’ and ‘b’ to ‘y’ so on, from the nth position to the end of the string. Here, we are taking n as 4 by default, you can change it as per your will.

The approach is as follows :

  •  First, initialize the strings start and the mirr variables with the proper sequence of alphabets and their mirror characters respectively.
  • Secondly, use the zip() function to collect the similar index of the iterables start and mirr and this generates an iterator or a series of tuples.
  • Next, this iterator must be converted into a dictionary for better access. As a result, we use the dict() method. This new dictionary is stored in mchars.
  • The original characters in start form the keys and the mirror characters in mirr form the values of mchars.
  • Next, we divide the given string term into two portions : the portion before nth position([0:n-1]) and the portion after([n-1:]). And we can achieve this using String Slicing.
  • The portion before n is stored in bef and the portion after n in aft. Here, we take n-1 as position n means n-1 th index and we include this index in the aft string. We will work with the portion after n.
  • Further, we create a for loop where i iterates over a range, from 0 to length of aft string where aft[i] serves as the key for the dictionary mchars. As a result, from mchars, the required value,i.e, the mirror character is extracted using the respective key; the original characters of aft.
  • Next, mirror characters are stored in nstr.
  • Add nstr to bef and that will be our output !
# mirror function to get the mirrored characters from nth position

def mirror(term,n): 

  start = 'abcdefghijklmnopqrstuvwxyz'
  mirr = 'zyxwvutsrqponmlkjihgfedcba'
  mchars = dict(zip(start,mirr)) # creating the dictionary for mirror characters

 # divide string into terms before and after nth position
  bef = term[0:n-1] 
  aft = term[n-1:] 
  nstr = ""
  for i in range(0,len(aft)): 
    nstr = nstr + mchars[aft[i]] #  mirror characters formed for given string
  print (bef+nstr) 	# mirrored characters added to before nth position part and printed
    
# Main code 
if __name__ == "__main__": 
    term = input('Enter the string: ')
    n = 4
    mirror(term,n)

Conclusion

Output :

Enter the string: Codespeedy                                                                                                                  

Codvhkvvwb

Here, we took the term as ‘Codespeedy’ and the output clearly shows how the characters got mirrored after the 4th position.

Thank you for going through this article by sparing your valuable time. Do check out the following recommended articles :

 

Leave a Reply

Your email address will not be published. Required fields are marked *