-2

I have written the following code in Python 2.7

class BinaryCode:
    def decode(self, message):
        result = str(0)
        result += str(message[0])
        for i in range(1, len(message)-1):
            result += str(int(message[i])-int(result[i])-int(result[i-1]))
        return result

When I initialise message as 123210122, I want the result to be 011100011 but instead it's {"0", "1", "1", "1", "0", "0", "0", "1", "1"}. How to do this?

This is a topcoder SRM 144 Div 2 problem and this was the result.

enter image description here

Now, when I run this code in IDLE it gives me a string but then why its not working in topcoder?

10
  • ''.join(result) just before return result Commented Jun 16, 2015 at 17:55
  • I just ran that, it returns 011100011 (so either this is not the code you run, or this is not the input you obtain) Commented Jun 16, 2015 at 17:55
  • @sunny: why would that work? do you not think result is already supposed to be a string? Commented Jun 16, 2015 at 17:56
  • ok i figured it out. he wants the result to be the number in binary, not a string. not very obvious but after running his code that's the only interpretation i can have. Commented Jun 16, 2015 at 17:58
  • 1
    If I make sure message is a string, then this works fine... Commented Jun 16, 2015 at 18:01

1 Answer 1

0

Just change last line of function to return ''.join(result).

Sign up to request clarification or add additional context in comments.

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.