Add functions to get possible keys.
continuous-integration/drone/push Build is passing Details

dev 0.9.8
KKlochko 2 years ago
parent ddfd5c473d
commit 2cc5be57c8

@ -7,7 +7,8 @@
[org.clojure/tools.cli "1.0.219"] [org.clojure/tools.cli "1.0.219"]
[org.apache.tika/tika-core "1.28.5"] [org.apache.tika/tika-core "1.28.5"]
[org.apache.tika/tika-langdetect "1.28.5"] [org.apache.tika/tika-langdetect "1.28.5"]
[cheshire "5.12.0"]] [cheshire "5.12.0"]
[org.clojure/math.combinatorics "0.2.0"]]
:main ^:skip-aot cipher-analytical-machine.core :main ^:skip-aot cipher-analytical-machine.core
:java-source-paths ["src/main/java"] :java-source-paths ["src/main/java"]
:target-path "target/%s" :target-path "target/%s"

@ -0,0 +1,22 @@
(ns cipher-analytical-machine.analyzers.simple-substitution
(:require [clojure.string :as cs]
[clojure.math.combinatorics :as comb]
[cipher-analytical-machine.ciphers.simple-substitution :as ss]
[cipher-analytical-machine.analyzers.analyzers :as analyzers]
[cipher-analytical-machine.analyzers.caesar :as caesar])
(:gen-class))
(defn get-possible-key-vector
"Conver a possible key map to a vector of possible keys. For example, if you have a map: {\\a [\\a \\b \\c] \\b [\\b \\c] \\c [\\a \\c]}, then the vector is [[\\a \\b \\c] [\\b \\c] [\\a \\c]]."
[possible-key-map symbols]
(reduce
(fn [acc el]
(conj acc (get possible-key-map el)))
[] symbols))
(defn get-possible-keys
"Return possible keys for a possible key vector. For example, if you have a map: {\\a [\\a \\b \\c] \\b [\\b \\c] \\c [\\a \\c]}, then the vector is [[\\a \\b \\c] [\\b \\c] [\\a \\c]] and keys (\"aba\" \"abc\" ...)."
[possible-key-vector]
(->> (apply comb/cartesian-product possible-key-vector)
(map cs/join)))

@ -0,0 +1,23 @@
(ns cipher-analytical-machine.analyzers.simple-substitution-test
(:require
[clojure.test :refer :all]
[cipher-analytical-machine.symbols.frequencies :as sf]
[cipher-analytical-machine.ciphers.simple-substitution :as ss]
[cipher-analytical-machine.analyzers.simple-substitution :refer :all])
(:gen-class))
(deftest get-possible-keys-vector-test
(testing "The vector must be ordered by symbols"
(are [possible-keys-map symbols expected-key-vector]
(= expected-key-vector (get-possible-key-vector possible-keys-map symbols))
{\a [\a \b] \b [\b \a] \c [\a \c]} "abc" [[\a \b] [\b \a] [\a \c]]
{\a [\a \b] \c [\b \a] \b [\a \c]} "abc" [[\a \b] [\a \c] [\b \a]])))
(deftest get-possible-keys-test
(testing "The keys must be the all combinations"
(are [possible-keys-vector expected-keys]
(= expected-keys (get-possible-keys possible-keys-vector))
[[\a \b] [\f] [\a \c] [\g]] '("afag" "afcg" "bfag" "bfcg")
[[\a \b] [\a \c]] '("aa" "ac" "ba" "bc")
[[\a \b \c] [\a \c] [\b \c]] '("aab" "aac" "acb" "acc" "bab" "bac" "bcb" "bcc" "cab" "cac" "ccb" "ccc"))))
Loading…
Cancel
Save