diff --git a/exercices/2018/leticia/core.clj b/exercices/2018/leticia/core.clj new file mode 100644 index 0000000..b58fe7d --- /dev/null +++ b/exercices/2018/leticia/core.clj @@ -0,0 +1,104 @@ +(ns cipher.core) + + + + +(def encr1 "radyjgtxhpsncpbxrvtctgpaejgedhtegdvgpbbxcvapcvjpvtrdbqxcxcv +iwtpeegdprwpqxaxinpcsxcitgprixktstktadebtciduphrgxeixcvapcvjp +vtlxiwpctuuxrxtcipcsgdqjhixcugphigjrijgtudgbjaixiwgtpstsegdvg +pbbxcvo") + + +(defn to-int + [letra] + (- (int letra) (int \a))) + +(defn adicionar + [valor chave] + (+ valor chave)) + +(defn novo-valor + [posicao] + (char (+ posicao (int \a)))) + +(defn shift + [letra chave] + (-> (to-int letra) + (adicionar chave) + (mod 26) + novo-valor)) + +(defn caesar-encrypt + "encriptando uma palavra w com uma chave k utilizanado a cifra de César" + [w k] + (apply str (mapv #(shift % k) w)) + ) + +(defn caesar-dencrypt + "decriptação uma palavra w com uma chave k utilizanado a cifra de César" + [w k] + (apply str (mapv #(shift % (- k)) w)) + ) + + +(defn get-letters + [vstr] + (->> (filterv #(Character/isLetter %) vstr) + (apply str) + clojure.string/lower-case)) + +(defn encriptar + [w k] + (-> (get-letters w) + (caesar-encrypt k))) + +(defn count-letters + [letra vstr] + (->> (filterv #(= % letra) vstr) + count)) + +(defn to-char + [letra] + (char (+ letra (int \a)))) + +(def alphabet (map to-char (range 26))) + +(defn contar-letras + [frase] + (->> (map #(count-letters % frase) alphabet) + (zipmap alphabet))) + +(defn ordenar-hashmap + [hashmap] + (sort-by second > hashmap)) + +(defn get-tres-primeiros + [frase] + (->> (contar-letras frase) + (ordenar-hashmap) + (take 3))) + +(defn computar-chave + [c2 c1] + (- (int c2) (int c1))) + +(defn descriptografar + [frase letra] + (-> (get-tres-primeiros frase) + (nth 0) + (get 0) + (computar-chave letra) + (->> (caesar-dencrypt frase)))) + + +(defn encrypt-letter + [c1 c2] + (-> (+ (to-int c1) (to-int c2)) + (mod 26) + novo-valor)) + +(def cycle1 (cycle "cipher")) + +(defn encriptar-Vigenere + [] + (mapv #())) \ No newline at end of file diff --git a/exercices/2018/leticia/core_test.clj b/exercices/2018/leticia/core_test.clj new file mode 100644 index 0000000..7cf206e --- /dev/null +++ b/exercices/2018/leticia/core_test.clj @@ -0,0 +1,94 @@ +(ns cipher.core-test + (:require [midje.sweet :refer :all] + [cipher.core :as core])) + + +(def encr1 "radyjgtxhpsncpbxrvtctgpaejgedhtegdvgpbbxcvapcvjpvtrdbqxcxcv +iwtpeegdprwpqxaxinpcsxcitgprixktstktadebtciduphrgxeixcvapcvjp +vtlxiwpctuuxrxtcipcsgdqjhixcugphigjrijgtudgbjaixiwgtpstsegdvg +pbbxcvo") + + +(facts "to-int" + (core/to-int \c) => 2) + +(facts "adicionar" + (core/adicionar 23 3) => 26) + +(facts "mod" + (mod 26 26) => 0) + +(facts "shift" + (core/shift \a 3) => \d + (core/shift \b 20) => \v + (core/shift \x 3) => \a + (core/shift \d -3) => \a) + +(tabular + (fact "caesar-encrypt" + (core/caesar-encrypt ?w ?k) => ?expect) + ?w ?k ?expect + "apple" 20 "ujjfy" + "leticia" 3 "ohwlfld") + +(tabular + (fact "caesar-dencrypt" + (core/caesar-dencrypt ?w ?k) => ?expect) + ?w ?k ?expect + "ujjfy" 20 "apple" + "ohwlfld" 3 "leticia") + +(tabular + (fact "get-letters" + (core/get-letters ?w) => ?expect) + ?w ?expect + "Oii, funcionou!" "oiifuncionou") + +(tabular + (fact "encriptar" + (core/encriptar ?w ?k) => ?expect) + ?w ?k ?expect + "Hello, friend!" 5 "mjqqtkwnjsi") + +(tabular + (fact "count-letters" + (core/count-letters ?letra ?w) => ?expect) + ?letra ?w ?expect + \a "aadvark" 3 + \x "aadvark" 0 + \d "oiii" 0) + +(facts "to-char" + (core/to-char 0) => \a) + +(tabular + (fact "get-tres-primeiros" + (core/get-tres-primeiros ?w) => ?expect) + ?w ?expect + "aabbbc" '([\b 3] [\a 2] [\c 1]) + encr1 '([\p 19] [\t 17] [\x 17])) + +(tabular + (fact "computar-chave" + (core/computar-chave ?a ?b) >= ?expect) + ?a ?b ?expect + \p \e 11 + \p \t -4 + \p \a 15) + +(tabular + (fact "descriptografar" + (core/descriptografar ?a ?b) >= ?expect) + ?a ?b ?expect + encr1 \e "xgjepmzdnvytivhdxbzizmvgkpmkjnzkmjbmvhhdibgvibpvbzxjhwdidibxoczvkkmjvxcvwdgdotviydiozmvxodqzyzqzgjkhziojavnxmdkodibgvibpvxbzrdocvizaadxdzioviymjwpnodiamvnompxopmzajmhpgodocmzvyzykmjbmxvhhdibu" + encr1 \t "vehcnkxbltwrgtfbvzxgxkteinkihlxikhzktffbgzetgzntzxvhfubgbgzvmaxtiikhtvatubebmrtgwbgmxktvmboxwxoxehifxgmhytlvkbimbgzetgzntvzxpbmatgxyybvbxgmtgwkhunlmbgyktlmknvmnkxyhkfnembmakxtwxwikhzkvtffbgzs" + encr1 \a "clojureisadynamicgeneralpurposeprogramminglanguagecombiningctheapproachabilityandinteractivedevelopmentofascriptinglanguacgewithanefficientandrobustinfrastructureformultithreadedprogrcammingz" + "ahixblmaxmabgzpbmayxtmaxklmatmixkvaxlbgmaxlhnetgwlbgzlmaxmngxpbmahnmmaxphkwltgwgxoxklmhiltmtee" \t "hopeisthethingwithfeathersthatperchesinthesoulandsingsthetunewithoutthewordsandneverstopsatall") + + +(tabular + (fact "encrypt-letter" + (core/encrypt-letter ?a ?b) >= ?expect) + ?a ?b ?expect + \e \i \m + \u \i \c)