Skip to content

Instantly share code, notes, and snippets.

@peymanmajidi
Last active April 26, 2022 09:29
Show Gist options
  • Save peymanmajidi/1b1da8e0b58b7faca79b2a471eae7678 to your computer and use it in GitHub Desktop.
Save peymanmajidi/1b1da8e0b58b7faca79b2a471eae7678 to your computer and use it in GitHub Desktop.
Code and decode ROX13 & Caesar Cipher
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Caesar Cipher"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"alphabet = 'abcdefghijklmnopqrstuvwxyz'\n",
"def caesar_cipher(text, shift):\n",
" shift = shift + 26 if shift < 0 else shift\n",
" result = ''\n",
" for char in text:\n",
" is_capital = char.isupper()\n",
" char = char.lower()\n",
" if char in alphabet:\n",
" encoded = alphabet[(alphabet.index(char) + shift) % 26]\n",
" result += encoded.upper() if is_capital else encoded\n",
" else:\n",
" result += char\n",
" return result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ROX13"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def rox13(text):\n",
" return caesar_cipher(text, shift=13)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Uryyb Jbeyq\n"
]
}
],
"source": [
"text = input('Say Something:')\n",
"print(rox13(text))\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Rovvy Gybvn'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text = input('Say Something')\n",
"shift = int(input('How many shift? (-25..25)'))\n",
"caesar_cipher(text , shift)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Decode Caesar cipher"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"shift: 16, point: 2010, decoded_text: Hello World\n"
]
}
],
"source": [
"class Result:\n",
" def print(self):\n",
" print('shift: {}, point: {}, decoded_text: {}'.format(self.shift, self.point, self.decoded_text))\n",
"\n",
"def decode_caesar(text):\n",
" english_words = []\n",
" with open('words') as f:\n",
" english_words = f.read().splitlines()\n",
" result = Result()\n",
" max_point = -1\n",
" \n",
" for shift in range(26):\n",
" point = 0\n",
" words = text.split(' ')\n",
" for word in words:\n",
" decoded = caesar_cipher(word, shift)\n",
" if decoded.lower() in english_words:\n",
" point += 1000 + len(word) \n",
" \n",
" if point > max_point:\n",
" result.shift = shift\n",
" result.point = point\n",
" result.decoded_text = caesar_cipher(text, shift)\n",
" max_point = point\n",
" \n",
" return result\n",
"\n",
"\n",
"decode_caesar('Rovvy Gybvn').print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pigpen"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"pigpen_alphabet = {\n",
" \"a\":\"_|\",\n",
" \"b\":\"|_|\",\n",
" \"c\":\"|_\",\n",
" \"d\":\"]\",\n",
" \"e\":\"[]\",\n",
" \"f\":\"[\",\n",
" \"g\":\"-|\",\n",
" \"h\":\"|-|\",\n",
" \"i\":\"|-\",\n",
" \"j\":\"._|\",\n",
" \"k\":\"|._|\",\n",
" \"l\":\"|._\",\n",
" \"m\":\".]\",\n",
" \"n\":\"[.]\",\n",
" \"o\":\"[.\",\n",
" \"p\":\".-|\",\n",
" \"q\":\"|.-|\",\n",
" \"r\":\"|.-\",\n",
" \"s\":\"v\",\n",
" \"t\":\">\",\n",
" \"u\":\"<\",\n",
" \"v\":\"^\",\n",
" \"w\":\".v\",\n",
" \"x\":\".>\",\n",
" \"y\":\".<\",\n",
" \"z\":\".^\",\n",
" \".\":\"\\n\",\n",
" \" \":\" \"\n",
"}\n",
"\n",
"def pigpen(text):\n",
" result = ''\n",
" for ch in text.lower():\n",
" if ch in pigpen_alphabet:\n",
" result += pigpen_alphabet[ch]\n",
" else:\n",
" result += ch\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'_||_||_ ][][ -||-||-'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pigpen(input(\"Pigpenify:\"))"
]
}
],
"metadata": {
"interpreter": {
"hash": "002397faec4d98ddc6ab91b582a8fb0ff165bd4f0530482404e25ead627586fe"
},
"kernelspec": {
"display_name": "Python 3.7.1 32-bit (system)",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
@peymanmajidi
Copy link
Author

Related to this video:
1650954272749
▶️ Watch in Youtube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment