- Published on
Part 1: Teaching ChatGPT ROT-13
- Authors
- Name
- taylor
The simplest things
Lets get one obvious thing out of the way: ChatGPT already knows what ROT-13 is. In its own words:
ROT13 is a simple letter substitution cipher that replaces a letter with the letter thirteen places down the alphabet. It is often used for obscuring text, such as in online forums, where it serves as a way to hide spoilers or offensive content. ROT13 is a special case of the Caesar Cipher which was developed in ancient Rome.
But merely understanding an algorithm doesn't mean you can execute it. This post is about my journey trying to get ChatGPT to accurately translate text using the ROT-13 algorithm.
The first attempt
Let's see how ChatGPT does with a simple phrase.
Prompt:
How would I say "hello world" in ROT-13?
ChatGPT:
The phrase "hello world" in ROT13 would be "uryyb jbeyq". To encode a message with ROT13, you simply replace each letter in the message with the letter that appears 13 positions later in the alphabet. If you reach the end of the alphabet, you wrap around to the beginning and continue counting. To decode a message encoded with ROT13, you would perform the same substitution in reverse.
This response looks great! But I want to verify the translation without manually counting each letter's position in the alphabet. Lets ask ChatGPT for some help.
Prompt:
Write me the simplest possible python script to perform ROT-13 encoding
ChatGPT:
Here is a simple Python script to perform ROT13 encoding on a string:
def rot13(text):
result = ""
for char in text:
if char.isalpha():
char_code = ord(char)
if char.isupper():
char_code += 13
if char_code > ord("Z"):
char_code -= 26
else:
char_code += 13
if char_code > ord("z"):
char_code -= 26
result += chr(char_code)
else:
result += char
return result
text = "Hello World"
print(rot13(text))
This script takes in a string as input,
text
, and returns the ROT13 encoded string. It iterates over each character in the input string, checking if it is an alphabet. If it is, it converts the character to its ASCII code, adds 13 to it and checks if it is greater than the ASCII code of 'Z' or 'z' (depending on the case of the character), if so, it subtracts 26. Then it converts the ASCII code back to the character and appends to the result string. If the character is not an alphabet, it simply appends it to the result string.
😅
I've seen ChatGPT respond to this kind of prompt with several different implementations; this is definitely one of the more verbose solutions. Just for fun, here's the most minimal one-liner version I could get ChatGPT to write for me:
r=lambda t:''.join([chr((ord(c)-65+13)%26+65)if c.isupper()else chr((ord(c)-97+13)%26+97)if c.islower() else c for c in t])
😍
>>> text = "Hello World"
>>> r(text)
Uryyb Jbeyq
Look familiar? ChatGPT actually ROT-13ed "hello world" perfectly on its first try!
Well, this concludes the shortest blog post series of all time, thank you for reading--okay, wait, let's try something a little less likely to exist in the training data than "hello world"
Prompt:
ROT-13 encode "alphabet soup"
ChatGPT:
'alphabet soup' in ROT13 would be 'nyrk gbcf'. To encode the text using ROT13, you would replace each letter in the text with the letter that appears 13 positions later in the alphabet.
Uhoh. The result hilariously decodes to "alex tops," which is not "alphabet soup," in fact it's not even the right number of letters. Maybe this will prove to be a challenge after all.
The adventure continues in part two