1

I'm new to Python and just playing around with some code. I am trying to build a "secret message generator" which takes a string (e.g. "1234567890") and outputs it based on a simple pattern (e.g. "1357924680"). I have the encoder working 90% (currently it can't handle apostrophes), but the decoder is giving me a lot of trouble. For anything over 6 characters, there is no problem. Inputting "1357924680" outputs "1234567890". However, for shorter odd numbered strings (e.g. "Hello"), it does not show the last character (e.g. it outputs "Hell"). My code is below. There may be a simpler way to write it, but since I built this myself, I'd appreciate working with my code rather than rewriting It. So, how can it be fixed?

#simple decoder

def decode(s2):
    oddlist = []
    evenlist = []
    final = []
    s2 = s2.lower() #makes a string lowercase
    size = len(s2) #gets the string size
    print "Size " + str(size) #test print
    half = size / 2
    print "Half " + str(half)
    i = 0
    while i < half:
        if size % 2 == 0: #checks if it is even
            split = size / 2 #splits it
            oddlist.append(s2[0:split]) #makes a list of the first half
            evenlist.append(s2[split:]) #makes a list of the second half
            joinodd = ''.join(oddlist) #list -> string
            joineven = ''.join(evenlist) #list -> string
        else:
            split = (size / 2) + 1
            print split
            oddlist.append(s2[0:split]) #makes a list of the first half
            evenlist.append(s2[split:]) #makes a list of the second half
            joinodd = ''.join(oddlist) #list -> string
            joineven = ''.join(evenlist) #list -> string
        string = joinodd[i] + joineven[i]
        final.append(string)
        i = i + 1
    decoded = ''.join(final)
    print final
    return decoded

print decode("hello")

2 Answers 2

1

Maybe another answer will give you the error in your code but I want to make you a recommendation, if you are using python slice notation, use it ALL! This is an example of how you can do what you want in a more pythonic way:

import itertools

def encode(s):
    return s[::2] + s[1::2]

def decode(s):
    lim = (len(s)+1)/2
    return ''.join([odd + even for odd,even in itertools.izip_longest(s[:lim], s[lim:],fillvalue="")])


def test(s):
    print "enc:",encode(s)
    print "dec:",decode(encode(s))
    print "orig:",s
    print 

test("")
test("1")
test("123")
test("1234")
test("1234567890")
test("123456789")
test("Hello")

Output:

enc: 
dec: 
orig: 

enc: 1
dec: 1
orig: 1

enc: 132
dec: 123
orig: 123

enc: 1324
dec: 1234
orig: 1234

enc: 1357924680
dec: 1234567890
orig: 1234567890

enc: 135792468
dec: 123456789
orig: 123456789

enc: Hloel
dec: Hello
orig: Hello
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain this part: odd + even for odd,even in
@Acornrevolution Concatenate odd and even for odd,even in the list returned from itertools.izip_longest, the syntax is just list comprehension.
0

Your code is splitting the text into groups of two.

Which doesn't really work with words of odd length. So either you are skipping over one with

while i < half:
> ['hl', 'eo']

Or you make sure that you are getting all values with:

while i <= half:

> ['hl', 'eo', 'll']

Though this adds an extra letter to the output since it's technically adding another pair. You might need to re-think that algorithm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.