-4

Having some trouble decoding below, I have been trying to return to the original string. Is there anyone who could assist?

def encode(n, i=0, key=5):
   if i >= len(n):
      return n 

   chars = list(n)
   chars[i] = chr(ord(list(n)[i]) + key) 
   return encode("".join(chars), i=i+1)
Cryptic
  • 3
  • 2
  • 1
    NB: this function has an effect that is probably not intended: if the given key is different than 5, it is only applied to the first character. All other characters are still encoded with key equal to 5. What's the use to provide a decoder for an encoder that doesn't do the job as intended? – trincot Feb 24 '22 at 16:48
  • 2
    How you guy and [this person](https://stackoverflow.com/q/71255182/12671057) have the same broken code? – Kelly Bundy Feb 24 '22 at 16:55
  • `All characters should be encoded with key 5, 5 is added to all individual ord characters of the string. However I have been trying to decode by subtracting 5 from the function inverse, but just can't seem to get anything that makes perfect sense, yet it seems so simple. – Cryptic Feb 24 '22 at 16:57
  • Can you reply to the above comments, and clarify? The reason why you cannot make it work with the subtraction (negative), is because of that flaw in the function. So what do you want to do now? Get it fixed, or work with the broken code? – trincot Feb 24 '22 at 16:57
  • Ohh, it's actually a class thing. though I never expected anyone else to consult here, but our lectures did suggest it. seems to have gotten the easier question though. Would be easier to decode the list that string. – Cryptic Feb 24 '22 at 17:00
  • Does this app have like a messaging feature, wanna contact him see if he could help. Looks like he's done good so far – Cryptic Feb 24 '22 at 17:01
  • So what do you want to do now? Get the given code fixed, or work with the broken code? – trincot Feb 24 '22 at 17:02
  • Well I guess resolve this broken code then try inversing it. I now think it's going to be used as a local function. – Cryptic Feb 24 '22 at 17:07
  • Also, I am a first year. I do a lot of reading and practice but I hope you don't mind if I as questions if there is anything my research does not show me. If you do mind that is okay too. I understand you guys are busy. – Cryptic Feb 24 '22 at 17:08

1 Answers1

1

The function probably has an unintended behaviour when a key is provided to the call that is different from 5. In that case the recursive calls will not get that key and still work with the default 5.

The recursive call should logically be return encode("".join(chars), i=i+1, key=key)

If the code is fixed like that, you can call it with a negative value for k and it will work:

def encode(n, i=0, key=5):
   if i >= len(n):
      return n 

   chars = list(n)
   chars[i] = chr(ord(list(n)[i]) + key) 
   return encode("".join(chars), i=i+1, key=key)
   #                                  ^^^^^^^^^ fix

s = "What's another year"
s2 = encode(encode(s), 0, -5)
print(s == s2)  # True

Note that the function is unnecessarily using recursion. It also turns the given string to a list and back to a string, repeatedly. It is an example of how not to do it.

Use this function instead:

def encode(s, key=5):
    return "".join(chr(ord(ch) + key) for ch in s)

And use as:

s = "What's another year"
s2 = encode(encode(s), -5)   # Without the i-argument.
print(s == s2)  # True
trincot
  • 317,000
  • 35
  • 244
  • 286