From e296d8f97de462b51c4d01be7056bd72d6fb5e68 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 7 Sep 2023 23:35:58 +0200 Subject: [PATCH] caesar --- Tasks.md | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/caesar.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/caesar.py diff --git a/Tasks.md b/Tasks.md index 9dd9c6b..dabf179 100644 --- a/Tasks.md +++ b/Tasks.md @@ -282,8 +282,51 @@ Use [regex101.com](https://regex101.com) if you are not already a REGEX expert. There should be $374$ matches. -A regex that fullfills the requirements is `\b[a-z][AEIOUaeiou]([a-w]|[A-W])`. +A regex that matches the requirements is `\b[a-z][AEIOUaeiou]([a-w]|[A-W])`. [Code Example](src/tasks/regex.py) + +## Ancient Cryptography + +Difficulty: 2/5 + +
+Text + +```text +Neovim is a refactor, and sometimes redactor, in the tradition of Vim (which itself derives from +Stevie). It is not a rewrite but a continuation and extension of Vim. Many clones and derivatives +exist, some very clever—but none are Vim. Neovim is built for users who want the good parts of +Vim, and more. +```` + +
+ +1. The text above has been cyphered with the Caesar cipher, a timeless classic + algorithm to abstract the meaning away from a text and arguably an early + form of encryption. Your task is to decipher it back into readable text. + +### The caesar cipher + +For the caesar cipher, all letters are shifted by the value of the key. + +**Examples** + +"foo Bar" becomes "gpp Cbs" when shifted by $1$. + + + +
+Hints + + +
+
+Solution + + +[Code Example](src/caesar.py) + +
diff --git a/src/caesar.py b/src/caesar.py new file mode 100644 index 0000000..59c6895 --- /dev/null +++ b/src/caesar.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import sys + +def internal(text: str, key: int) -> str: + cyphertext: str = "" + for c in text: + if ord(c) >= 65 and ord(c) <= 90: # uppercase letters + ci = ord(c) - ord('A') + add = ord('A') + #print(f"{c} is uppercase") + elif ord(c) >= 97 and ord(c) <= 122: # uppercase letters + ci = ord(c) - ord('a') + add = ord('a') + #print(f"{c} is lowercase") + else: + #print(f"not a letter: {c} ({ord(c)})") + # character is not a letter, just skip it + cyphertext += c + continue + ci += key + ci %= 26 # only 23 letters in the alphabet + #print(f"ci for {c}: {ci}") + cyphertext += chr(ci + add) + + return cyphertext + + +if len(sys.argv) <= 2 or len(sys.argv) >= 5: + print("Takes two arguments: [-e]") + exit(1) + +source: str = sys.argv[1] +target: str = "" +key = int(sys.argv[2]) + +if len(sys.argv) >= 4 and sys.argv[3] == "-e": + print("encrypting...") + target = internal(source, -key) + + +else: + print("decrypting...") + target = internal(source, key) + +print("=" * 80) +print("%s" % target)