DEV Community 👩‍💻👨‍💻

Qroia FAK(C)E
Qroia FAK(C)E

Posted on

Codewars Challenge Day 4: ROT13

Details

Name Kata: ROT13
5kuy
Description: How can you tell an extrovert from an introvert at NSA? Va gur ryringbef, gur rkgebireg ybbxf ng gur BGURE thl'f fubrf.

I found this joke on USENET, but the punchline is scrambled. Maybe you can decipher it? According to Wikipedia, ROT13 is frequently used to obfuscate jokes on USENET.

Hint: For this task you're only supposed to substitue characters. Not spaces, punctuation, numbers etc.
Example:

"EBG13 rknzcyr." -->
"ROT13 example."

"This is my first ROT13 excercise!" -->
"Guvf vf zl svefg EBG13 rkprepvfr!"
Enter fullscreen mode Exit fullscreen mode

My Solutions

JavaScript

const rot13 = (str) => {
  return str.replace(/[a-z]/giu, (x) => {
    return String.fromCharCode(
      x.charCodeAt() + (
        x.toLowerCase() <= 'm' ? 13: -13
      ));
  });
}
Enter fullscreen mode Exit fullscreen mode

Python

def rot13(message: str) -> str:
    rot13 = bytes.maketrans(
        b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        b"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
    )
    return message.translate(rot13)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Life is too short to browse without dark mode