- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Index Mapping Cypher in Python
The Index Mapping Cypher is defined by passing the integer string to get the specific string. In Python, we have some built−in functions such as str(), len(), join(), and, int() will be used to solve the problem based on Index Mapping Cypher.
Let’s take an example to understand this:
The given string,
my_str = “Hyderabad”
The given index,
idx = 101456
The final result becomes yHyrab
The following syntax is used in the examples-
This is a built−in function in Python that accepts any value to convert it into string.
This is a built−in method in Python that returns the length of the object.
The join() is an in−built function in Python to join the iterable element. It also combines every element in sequential order.
The built−in function int converts the numbers or string in the form of integer.
Using for loop
The program uses for loop to iterate through the length of the original string and initialize the index value to print the specific string.
In the following example, we will start the program by defining a function named idx_map_cypher() which accepts two parameters− r_str and index. Both these parameters receive the value during the function call and it is called a recursive function. The variable result initializes the value by an empty string which will store the new specific string based on the condition and given index value. Next, use the for loop where variable i iterate into the str(index). Then apply the if−statement to set the condition for comparison between idx and r_str with the help of < operator and using += operator it will form the new string and return the result. Now start creating the original string in the variable my_str and set the value to store in the variable idx. Then use the function call and store it in the variable res. Finally, we are printing the formatted output with the help few variables such as− my_str, index, and, res.
def idx_map_cypher(r_str, index): result = "" for i in str(index): index = int(i) if index < len(r_str): result += r_str[index] return result # Create the string my_str = "Tutorialspoint" # Set the index value index = 6875 res = idx_map_cypher(my_str, index) print("The original string:", my_str, "\nThe index value:", index, "\nString printing based on the index:", res)
The original string: Tutorialspoint The index value: 6875 String printing based on the index: asli
Using list Comprehension
The program uses list comprehension where a method named join will be used to combine the specific string based on a given index value and it helps to build the Index Mapping Cypher.
In the following example, begin the program by creating the input string in the variable my_str and display the variable. Next, initialize the variable named index to store the random integer value which specifies the original string based on index value. Now it will create the list comprehension where indexing the characters in my_str using the values in an index through for loop and store these processes in the variable res. Moving ahead to display the result with the help of variable index and str(res).
# Create the string my_str = "BROWSER" print("Original string: " + my_str) # Create the index index = "4154" # list comprehension res = ''.join([my_str[int(i)] for i in index]) # Display the result print("The index value:", index, "\nstring is", str(res))
Original string: BROWSER The index value: 4154 string is SRES
Using Reverse Index Mapping Cypher
The program uses a reverse index which means if the variable index specifies any integer value it will return the result in the form of reverse order. For example-
my_str = “PLOT” index = 12
The input string initializes in the variable my_str whereas the index variable set the value as 12. For reversing the index it will use some conditions and possible to print the index string in reverse form.
In the following example, we will start the program by using a recursive function named rev_index_map_cypher that accepts two parameters− r_str and index that will fetch the value during the function call. Then store the empty string in the variable result that will store the specific string based on reverse index. Now using for loop it iterates the string index value along its slicing. Moving ahead to create the original string in the variable my_str and initialize the variable named index to set the integer value. Then use the function call and store it in the variable res and print the result.
def rev_index_map_cypher(r_str, index): result = "" for i in str(index)[::-1]: idx = int(i) if idx < len(r_str): result += r_str[idx] return result # Create the string my_str = "INTERSTELLAR" # Set the index value index = 7455 res = rev_index_map_cypher(my_str, index) print("The original string:", my_str, "\nThe index value:", index, "\nString printing based on index:", res)
The original string: INTERSTELLAR The index value: 7455 String printing based on the index: SSRE
We discussed the various ways to solve the problem statement. The Index Mapping Cypher is generally used in the field of cryptography and computer science. The term Cypher defines the value as index to find the specific string.
Kickstart Your Career
Get certified by completing the courseGet Started