0

I tried using asciii and tried ord() but I could not use the input of the user and increment it by 3 letters. For example I want to be able to have the following scenario:

input: abc output: def

and also it would loop back from z to a so:

input: xyzab output: abcde

these are just examples of input, the code should work on any input of letters.

its starting to be mind boggling for me.

for those who wonder this is just a challenge I encountered in the blackhat event last year in Riyadh and obviously couldn't beat.

Kongming
  • 1
  • 1
  • You triend and failed? or is it the challenge to not convert to ascii? because with ASCII it's just a simple function I think. – Seppe Willems Jan 11 '23 at 22:27
  • @sahasrara62 when I tried to take the code from the question you mentioned it does not work on 3 for some reason. And it does not take any input. – Kongming Jan 16 '23 at 07:31
  • "when I tried to take the code from the question you mentioned it does not work on 3 for some reason" Well, yes, because the other question said to increment by 1. There are many different answers there. For any one of them, you will probably notice that it says `1` in the code in at least one place. Did you try to **understand** how the code works? Perhaps, where the code says `1`, some or all of those should be changed to `3`. If you study the code, maybe you can figure out which ones. Otherwise, perhaps try some trial and error. – Karl Knechtel Jan 16 '23 at 07:47
  • "And it does not take any input." - again, you will see how the code works on a string that was already assigned to some variable, and you will see how the code mentions some variable name. What if you try using the input, to assign to that variable (instead of whatever else is assigning it)? What if you try replacing the variable, with the code to get the input? The reason for closing questions as a duplicate is because questions here are supposed to be **single questions** about a **single problem**. We do not write the complete code for you. – Karl Knechtel Jan 16 '23 at 07:49
  • Yes i did change the 1️⃣ to 3️⃣ of course! And yes thanks i just needed to get back into the art of writing code since i only learned in vb.net and now with python i was a little lost. Thanks for your trying to explain but rest assured the first thing i did was change the 1️⃣ to 3️⃣ lol – Kongming Jan 17 '23 at 10:32

2 Answers2

1

you can use modulo arithmetic to handle the cases near the end

def shift(text):
    result = ""
    for i in range(len(text)):
        char = text[i]
        if (char.isupper()):
            result += chr((ord(char) + 3 - 65) % 26 + 65)
        else:
            result += chr((ord(char) + 3 - 97) % 26 + 97)
    return result

print(shift("ABC"))

print(shift("xyzab"))
ashish singh
  • 6,526
  • 2
  • 15
  • 35
  • ok thanks! I am going to try to tweak your code in order to make it does that to any input. Thanks again – Kongming Jan 14 '23 at 20:30
0

for lower and upper english language alphabets, you can use below code

def func(string, increment=3):
    result = []
    orders = [65, 97]

    for idx, char in enumerate(string):
        is_lower = char.islower()
        new_char = chr(orders[is_lower] + (ord(char)%orders[is_lower]+ increment)%26)
        result.append(new_char)

    return ''.join(result)


string = "xyzab"
result = "abcde"
assert result == func(string, 3)
sahasrara62
  • 10,069
  • 3
  • 29
  • 44