Created
June 21, 2018 15:44
-
-
Save goropikari/2382e3edd00d320784f82e2eb36bd401 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Julia Version 0.6.3\n", | |
"Commit d55cadc (2018-05-28 20:20 UTC)\n", | |
"Platform Info:\n", | |
" OS: Linux (x86_64-pc-linux-gnu)\n", | |
" CPU: Intel(R) Core(TM) i5-4460T CPU @ 1.90GHz\n", | |
" WORD_SIZE: 64\n", | |
" BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY Haswell)\n", | |
" LAPACK: libopenblas\n", | |
" LIBM: libm\n", | |
" LLVM: libLLVM-3.9.1 (ORCJIT, haswell)\n" | |
] | |
} | |
], | |
"source": [ | |
"versioninfo()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Caesar cipher" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"print_table" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"function encdic(n)\n", | |
" m = string.(collect('a':'z'))\n", | |
" c = [m[mod1(n+i, 26)] for i in 1:26]\n", | |
" c = uppercase.(c)\n", | |
" return Dict(zip(m,c))\n", | |
"end\n", | |
"\n", | |
"\n", | |
"\"Encryption\"\n", | |
"function enc(s, n)\n", | |
" d = encdic(n)\n", | |
" function encode(c)\n", | |
" return d[c]\n", | |
" end\n", | |
" \n", | |
" return encode.(split(s, \"\")) |> prod\n", | |
"end\n", | |
"\n", | |
"function decdic(n)\n", | |
" m = string.(collect('a':'z'))\n", | |
" c = [m[mod1(n+i, 26)] for i in 1:26]\n", | |
" c = uppercase.(c)\n", | |
" return Dict(zip(c,m))\n", | |
"end\n", | |
"\n", | |
"\"Decryption\"\n", | |
"function dec(s, n)\n", | |
" d = decdic(n)\n", | |
" function decode(c)\n", | |
" return d[c]\n", | |
" end\n", | |
" \n", | |
" return decode.(split(s, \"\")) |> prod\n", | |
"end\n", | |
"\n", | |
"\"Convertion table\"\n", | |
"function print_table(n)\n", | |
" d = encdic(n)\n", | |
" for item in 'a':'z' \n", | |
" print(item, \" \")\n", | |
" end\n", | |
" println()\n", | |
" for item in string.('a':'z')\n", | |
" print(d[item], \" \")\n", | |
" end\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a b c d e f g h i j k l m n o p q r s t u v w x y z \n", | |
"D E F G H I J K L M N O P Q R S T U V W X Y Z A B C " | |
] | |
} | |
], | |
"source": [ | |
"# shift 3 charactor\n", | |
"print_table(3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"MKD\"" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"enc(\"cat\", 10) |> prod" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"cat\"" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dec(\"MKD\", 10)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Simple substitution cipher" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a: Z\n", | |
"b: V\n", | |
"c: F\n", | |
"d: H\n", | |
"e: U\n", | |
"f: T\n", | |
"g: J\n", | |
"h: K\n", | |
"i: C\n", | |
"j: R\n", | |
"k: Q\n", | |
"l: P\n", | |
"m: I\n", | |
"n: G\n", | |
"o: S\n", | |
"p: E\n", | |
"q: X\n", | |
"r: M\n", | |
"s: O\n", | |
"t: Y\n", | |
"u: L\n", | |
"v: D\n", | |
"w: N\n", | |
"x: A\n", | |
"y: B\n", | |
"z: W\n" | |
] | |
} | |
], | |
"source": [ | |
"srand(2018)\n", | |
"m = string.('a':'z')\n", | |
"c = uppercase.(shuffle(m))\n", | |
"simple_encdic = Dict(zip(m,c))\n", | |
"simple_decdic = Dict(zip(c,m))\n", | |
"\n", | |
"for c in string.('a':'z')\n", | |
" println(c, \": \", simple_encdic[c])\n", | |
"end" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"simple_dec (generic function with 1 method)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"simple_enc(s) = map(x -> simple_encdic[x], string.(split(s, \"\"))) |> prod\n", | |
"simple_dec(s) = map(x -> simple_decdic[x], string.(split(s, \"\"))) |> prod" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"FZY\"" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s = \"cat\"\n", | |
"simple_enc(s)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"cat\"" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"simple_dec(simple_enc(s))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 0.6.2", | |
"language": "julia", | |
"name": "julia-0.6" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "0.6.3" | |
}, | |
"latex_envs": { | |
"LaTeX_envs_menu_present": true, | |
"autoclose": false, | |
"autocomplete": true, | |
"bibliofile": "biblio.bib", | |
"cite_by": "apalike", | |
"current_citInitial": 1, | |
"eqLabelWithNumbers": true, | |
"eqNumInitial": 1, | |
"hotkeys": { | |
"equation": "Ctrl-E", | |
"itemize": "Ctrl-I" | |
}, | |
"labels_anchors": false, | |
"latex_user_defs": false, | |
"report_style_numbering": false, | |
"user_envs_cfg": false | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": "block", | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment