Add functions to get possible keys.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
ddfd5c473d
commit
2cc5be57c8
@ -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…
Reference in new issue