Decoding message that was previously encoded

I’m trying to figure out how to reverse a message that was encoded by splitting a string into two arrays then joining them.

The encoding code looks like this:

def encode_railroad(secret_phrase):
    toggle      = True
    upper_track = []
    lower_track = []

    for word in secret_phrase.split(" "):
        if toggle:
            toggle = False
        toggle = True

    return "{}rn{}".format(" ".join(upper_track), " ".join(lower_track))

I’m trying to figure out how I would reverse this. For example, if ‘hi my name is ryan’ becomes ‘hi name ryan my is’ how would I reverse it back to ‘hi my name is ryan’.


You have to split the string, then split the list into two sublists. Then use the sublists to generate target string.

def decode(msg):
    lst = msg.split()
    idx = (len(lst) // 2) + 1 if len(lst) % 2 else len(lst) // 2
    a, b = lst[0: idx], lst[idx:]
    for i in range(len(a) - len(b)):
    return ' '.join([' '.join(x) for x in zip(a, b)])

print(decode('hi name ryan my is'))


hi my name is ryan 

Leave a Reply

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