-3

i didn't understand the question .I need some hints for this :

def cipher(map_from, map_to, code):
""" map_from, map_to: strings where each contain 
                      N unique lowercase letters. 
    code: string (assume it only contains letters also in map_from)
    Returns a tuple of (key_code, decoded).
    key_code is a dictionary with N keys mapping str to str where 
    each key is a letter in map_from at index i and the corresponding 
    value is the letter in map_to at index i. 
    decoded is a string that contains the decoded version 
    of code using the key_code mapping. """
# Your code here

and test case for example :

cipher("abcd", "dcba", "dab") returns (order of entries in dictionary may not be the same) ({'a':'d', 'b': 'c', 'd': 'a', 'c': 'b'}, 'adc').

Anthony Pham
  • 3,096
  • 5
  • 29
  • 38
  • 2
    did you review the course material? – wwii Jul 31 '17 at 13:27
  • What is it that you don't undersand ? All is explained in great details and you even have some example. – bruno desthuilliers Jul 31 '17 at 13:55
  • Don't forget about using the search box? [How can I return two values from a function in Python?](https://stackoverflow.com/questions/9752958/how-can-i-return-two-values-from-a-function-in-python) [How do you return multiple values in Python?](https://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python) [What's the best way to return multiple values from a function in Python?](https://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python) – marcushobson Jul 31 '17 at 13:57

2 Answers2

0

First, you create the requested dictionnary, then you pass the "decoded" string through it.

def cipher(map_from, map_to, code):
    key_code = {}
    decoded = ''
    for i in range(len(map_from)):
        key_code[map_from[i]] = map_to[i]

    for i in code:
        decoded += key_code[i]

    return (key_code,decoded)


print(cipher("abcd", "dcba", "dab"))
0

simple solution using dict() & zip() functions:

def cipher(map_from, map_to, code):
    D = dict(zip(map_from, map_to)) # create dictionary
    msg = ""
    for e in code:
        msg += D[e] # encode message
    return (D, msg) # voilá!
Manu
  • 112
  • 4