From 79ae13aa5a31adefc49c28192f6f1c808d77a50b Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 22 Oct 2023 17:26:54 +0300 Subject: [PATCH] Add a generator for the sorted substitution table. --- .../ciphers/simple_substitution.clj | 7 +++++++ .../ciphers/simple_substitution_test.clj | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/cipher_analytical_machine/ciphers/simple_substitution.clj b/src/cipher_analytical_machine/ciphers/simple_substitution.clj index b49312d..365f405 100644 --- a/src/cipher_analytical_machine/ciphers/simple_substitution.clj +++ b/src/cipher_analytical_machine/ciphers/simple_substitution.clj @@ -17,6 +17,13 @@ (shuffled-numbers) (zipmap symbols))) +(defn generate-sorted-substitution-table + "Generate the map (char, int) for the substittion. BUT a value is the index of a symbol (from 0)." + [symbols] + (-> (count symbols) + (take (range)) + (zipmap symbols))) + (defn find-value-in-table "It uses the substitution table to find the value of a char or a number." [char substitution-table] diff --git a/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj b/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj index a2fac85..6e6495c 100644 --- a/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj +++ b/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj @@ -22,6 +22,13 @@ 1 \a 5 nil)))) +(deftest generate-sorted-substitution-table-test + (testing "If the symbol is in the table as a key, then the result won't nil." + (are [symbols expected-table] + (= expected-table (generate-sorted-substitution-table symbols)) + "abc" {0 \a 1 \b 2 \c} + "a" {0 \a}))) + (deftest encrypt-message-test (let [symbols "abc" table {\a 1 \b 2 \c 3}]