Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Index Mapping Cypher

Improve Article
Save Article
  • Last Updated : 09 Sep, 2022
Improve Article
Save Article

Sometimes, while working with Python, we can have problems in security or gaming domain, in which we need to create certain cyphers, they can be different types of. This includes Index Mapping Cypher in which we pass a string of integers and we get element characters in that order. Lets discuss certain ways to construct this. Method #1 : Using loop This is brute force manner in which this can be constructed. In this, we manually check for each character and map it as index number to value in string. 

Python3




# Python3 code to demonstrate working of
# Index Mapping Cypher
# Using loop
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing cypher string
cyp_str = "53410"
 
# Index Mapping Cypher
# Using loop
res = ""
temp = [int(idx) for idx in cyp_str]
for ele in temp:
    res += test_str[ele]
 
# printing result
print("The deciphered value string : " + str(res))

Output : 

The original string is : geeksforgeeks
The deciphered value string : fkseg

  Method #2 : Using join() + loop The combination of above functions can also be used to solve this problem. In this, we reduce a final loop by using join() to construct decipher string. 

Python3




# Python3 code to demonstrate working of
# Index Mapping Cypher
# Using loop + join()
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing cypher string
cyp_str = "53410"
 
# Index Mapping Cypher
# Using loop + join()
res = [test_str[int(idx)] for idx in cyp_str]
res = ''.join(res)
 
# printing result
print("The deciphered value string : " + str(res))

Output : 

The original string is : geeksforgeeks
The deciphered value string : fkseg

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!