0

I'm creating a simple cryptography project for school. I'm working with Python.

Target: user enters string, e.g. hello world! I'll have to convert it into an array: space=0, a=1, b=2, ... z=26, .=27, ,=28, ?=29 and !=30

I use a dictionary:

dict = {' ': 0, 'a': 1, 'b': 2, 'c': 3,...}

My code

def messageToCode(message):
    xarray = [None]
    length = len(message)
    ctr = 0
    while not ctr == length:
        xarray.append = dict[message.charAt(ctr)]
        ctr = ctr + 1
    return xarray

This won't work. Any suggestions?

Ward Segers
  • 519
  • 5
  • 17
  • 4
    What doesn't work about it? Does it throw an error? Is the output different? How so? – Celeo May 18 '16 at 17:32
  • Relevant: http://stackoverflow.com/questions/8848294/how-to-get-char-from-string-by-index – jakevdp May 18 '16 at 17:34
  • 1
    `[ dict[x] for x in message ]`, maybe? – twalberg May 18 '16 at 17:48
  • voted to close because there's no specific question. FYI: your question is likely to be closed pretty soon if you don't provide more information. see this article: http://stackoverflow.com/help/mcve – Rick May 18 '16 at 18:48
  • To the close voters: I know OP didn't *say* what was wrong, but if it's really obvious, why close it? – Alex Hall May 18 '16 at 18:50
  • @AlexHall Perhaps because it's not really obvious. I can see at least one obvious Java-ism that somehow snuck into Python code, which would cause it to fail if certain assumptions are made about the type of `message`, but otherwise "This won't work." just isn't all that specific, and there are other obvious issues in the code as well... – twalberg May 18 '16 at 19:44

3 Answers3

5

There are some serious problems:

  • xarray should start out empty, so just []. What you've written is a list with a single element: None. None is an actual value, just like 5 or "a" and you don't want it in your final result.
  • = is for assignment, so xarray.append = ... is assigning a value to the method append, which you don't want. You want to call the function, i.e. xarray.append(...).
  • .charAt is from Java and other languages. To get a character from a string you index it just like a list or a dictionary, i.e with square brackets: message[ctr].

And some less serious ones:

  • not ctr == length is better written ctr != length. Imagine that != is ≠. In this case it's even better to say ctr < length because that's what the intent really is: message[ctr] is only valid if ctr < length (and ctr >= 0), not just when ctr != length. You also don't want the loop to go on forever if you change the code to increment ctr by 2 or more each iteration and ctr skips past length.
  • ctr = ctr + 1 is better written ctr += 1, so you don't have to write the variable twice.

Make these changes to your code, make sure it works, edit it into your question, and then I'll tell you bit by bit how to improve it even more. Eventually we'll end up with the optimal version in the other answer.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • 5
    based on the answers of yours I have seen I am amazed you do not do [code review](http://codereview.stackexchange.com) – Tadhg McDonald-Jensen May 19 '16 at 15:19
  • @TadhgMcDonald-Jensen I'd probably never get anything done. Also I'm less motivated when I know everyone else is probably going to say the same thing. – Alex Hall May 19 '16 at 15:21
  • 3
    You'll get stuff done.. unless you get sucked into the productivity sink that is [the 2nd monitor](https://chat.stackexchange.com/rooms/8595/the-2nd-monitor)..... please send help! In all seriousness, we'd welcome you on CR. Even if other people are going to say the same thing, no one would submit any answers with that attitude! – Dan May 19 '16 at 15:22
0

Try this,

def encode_message(message, d):
  return [d[c] for c in message]

# Test
message = 'hello world!'
d = {'!': 0, ' ': 1, 'e': 2, 'd': 3, 'h': 4, 'l': 5, 'o': 6, 'r': 7, 'w': 8}

l = encode_message(message, d)
print(l)
# Output
[4, 2, 5, 5, 6, 1, 8, 6, 7, 5, 3, 0]
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
0

My suggestion:

import string 

# Lists os components
chars = [' '] + list(string.ascii_lowercase) + ['.',',','?','!']

# Your target
target = 'hello world!'

# Your array
array = [chars.index(target[t]) for t in range(len(target))] 

In this case you have array equals: [8, 5, 12, 12, 15, 0, 23, 15, 18, 12, 4, 30]

awulll
  • 161
  • 14