diff --git a/src/cipher_analytical_machine/analyzers/simple_substitution.clj b/src/cipher_analytical_machine/analyzers/simple_substitution.clj index 4f1d16c..2c03881 100644 --- a/src/cipher_analytical_machine/analyzers/simple_substitution.clj +++ b/src/cipher_analytical_machine/analyzers/simple_substitution.clj @@ -7,13 +7,22 @@ (: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]]." + "Convert 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-key-vector-from-reversed-count-map + "Convert a reversed count map to a vector. Example, from {2 [\\a \\b] 1 [\\d] 3 [\\e]} to [[\\e] [\\a \\b] [\\d]])." + [reversed-count-map] + (->> reversed-count-map + (into []) + (sort-by #(- (first %))) + (map second) + (into []))) + (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] diff --git a/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj index f71ff9b..5b77935 100644 --- a/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj +++ b/test/cipher_analytical_machine/analyzers/simple_substitution_test.clj @@ -13,6 +13,13 @@ {\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-key-vector-from-reversed-count-map-test + (testing "" + (are [reversed-count-map expected-key-vector] + (= expected-key-vector (get-possible-key-vector-from-reversed-count-map reversed-count-map)) + {1 [\a \c] 2 [\b]} [[\b] [\a \c]] + {2 [\a] 1 [\b] 3 [\c]} [[\c] [\a] [\b]]))) + (deftest get-possible-keys-test (testing "The keys must be the all combinations" (are [possible-keys-vector expected-keys]