A substitution cipher for the digits 0,1,2,3,....9 substitutes each digit in 0,1
ID: 3531600 • Letter: A
Question
A substitution cipher for the digits 0,1,2,3,....9 substitutes each digit in 0,1,2,3,...9 with another digit in 0,1,2,3,...9. It can be represented as a 10-digit string specifying how each digit in 0,1,2,3,...9 is substituted. For example , the 10-digit string '3941068257' specifies a substitution cipher in which digit 0 is substituted with digit 3, 1 with 9 etc. impliment or define function cipher().
shell output should look like this:
>>> cipher('3941068257', '132')
'914'
---------------------------
Here is how i did it which it does work, but the teacher said keep working this is not the solution he is looking for. he wants an iterator for the solution. I am having trouble figuring out how to do a for loop properly for this. Any ideas out there
number_String = '0123456789'
def cipher(new_Number_String, encrypt_Numbers):
table = str.maketrans(number_String, new_Number_String)
return encrypt_Numbers.translate(table)
Explanation / Answer
This uses each "key" in the string, and uses that to look up the appropriate character.
In the encrypt function you could create a map of lookups using the first string parameter. Then go through each character in the second parameter string and use this map for substitution and create and return a new string
In the encrypt function you could create a map of lookups using the first string parameter. Then go through each character in the second parameter string and use this map for substitution and create and return a new string
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.