0

I have an assignment where I have to decode a string by finding an offset code. The offset code represents the number of positions the character in the string has been moved in the alphabet. If I can find this number, I can supposedly decode the string.

This is the string: vyzhesjdpwqncjae

I was thinking of creating a for-loop to iterate through the string and change each character's position, and see which iteration produces a word. But I'm not quite sure how to do this.

This is the code I have so far:

def decode (newInput):
    for i in range (1,27):
            newInput2 = newInput[i+1]        
    print "Trying to decode:", newInput2

decode(newInput) 

But this isn't working. I would like the output to print the combination of words possible for each iteration - when each character in the string is moved one space along, two, three...until 26. Is this the best way of doing that?

Keep in mind, I'm quite the beginner, and I think I should stick to for-loops. I have seen some posts on using the dictionary function, but I haven't quite learned that yet.

Thank you for your help!

  • 2
    1. You cannot change a string; they're immutable. 2. You're trying to change the position of a symbol in the string, not its position in the alphabet. 3. [Take a look at this](https://stackoverflow.com/questions/8886947/caesar-cipher-function-in-python). – Norrius Mar 13 '18 at 15:32
  • 2
    Possible duplicate of [Python - Increment Characters in a String by 1](https://stackoverflow.com/questions/35820678/python-increment-characters-in-a-string-by-1) : implement your logic with this. – Kaushik NP Mar 13 '18 at 15:49
  • How are you verifying that your decoded word is in fact a word? Are you checking it against a list of words or an entire language's dictionary? Are you looking for a specific word? Or are you just trying to get this decode function to work at this point? – Chad Lewis Mar 13 '18 at 16:21
  • @ChadLewis I know that the word, when shifted by n position will become decoded. That is the assumption of the assignment. So, I'm guessing that if the function works, I'll just be able to read the word. – Lucile Perrot Mar 13 '18 at 20:44
  • @Norrius Sorry - yes, I did not explain myself well. And I'm also guessing that's why my function isn't working. I'll take a look. – Lucile Perrot Mar 13 '18 at 20:45

1 Answers1

0

I wanted to provide a couple different solutions that may help you. You mention you're a beginner, so I'll offer one with for loops, but I'd highly recommend looking into list comprehension - it's a valuable and fun tool.

Note that I use this shift helper function for all solutions:

def shift(char, dist):
    ascii = ord(char) + dist
    if ascii > 122:
        ascii = ascii - 26

    return chr(ascii)

For Loops Only

Take a look at python's lower and format functions for these solutions if you're not familiar with them yet.

def decode_loops(newInput):
    print('For loops only')
    for i in range(1, 27):
        decoded = ''
        for c in newInput.lower():
            decoded += shift(c, i)
        print('{0}: {1}'.format(i, decoded))

Using List Comprehension

For this one, take a look at python's join function if you're not already familiar with it.

def decode_list_comp(newInput):
    print('For loop and list comp')
    for i in range(1, 27):
        decoded = ''.join([shift(c, i) for c in newInput.lower()])
        print('{0}: {1}'.format(i, decoded))

More List Comprehension

This one is just to show how complicated you can get with list comprehension - I'm not saying this is a good solution at all, just providing more options and an insight into what you can do with it. Personally, if I can't "read" a list comprehension pretty close to how it would sound describing it verbally, it may be too complicated. But I digress.

def decode_complex_list_comp(newInput):
    print('Complex list comprehension')
    shifted = [shift(c, i) for i in range(1, 27) for c in newInput.lower()]
    decoded_words = [''.join(decoded) for decoded in [shifted[i:i + len(newInput)] for i in range(0, len(shifted), len(newInput))]]
    for i, decoded in enumerate(decoded_words):
        print('{0}: {1}'.format(i + 1, decoded))

Hopefully these possible solutions provided you some help. Keep in mind, though, there are many ways to do this. These are just some options I came up with.

Chad Lewis
  • 131
  • 10