From 553f4788e49e65b4f76a07be8d14449a081ea3a8 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Mon, 23 Oct 2023 15:50:05 +0300 Subject: [PATCH] Add a function to invert the table in a decoded json. --- .../parsers/simple_substitution.clj | 10 ++++++++++ .../parsers/simple_substitution_test.clj | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/src/cipher_analytical_machine/parsers/simple_substitution.clj b/src/cipher_analytical_machine/parsers/simple_substitution.clj index 1b3ca1e..548ce00 100644 --- a/src/cipher_analytical_machine/parsers/simple_substitution.clj +++ b/src/cipher_analytical_machine/parsers/simple_substitution.clj @@ -2,6 +2,7 @@ (:require [cipher-analytical-machine.ciphers.simple-substitution :as ss] [cipher-analytical-machine.parsers.parsers :as ps] + [clojure.set :as set] [cheshire.core :as cc]) (:gen-class)) @@ -42,3 +43,12 @@ (assoc "table" (ss/generate-substitution-table symbols))) (decode-key-and-substitution-table-from-json key-or-json))) +(defn invert-table-in-map + "Invert the table in a decoded json (key, table)" + [decoded-json] + (->> + (-> decoded-json + (get "table") + (set/map-invert)) + (assoc decoded-json "table"))) + diff --git a/test/cipher_analytical_machine/parsers/simple_substitution_test.clj b/test/cipher_analytical_machine/parsers/simple_substitution_test.clj index 74e5478..0300066 100644 --- a/test/cipher_analytical_machine/parsers/simple_substitution_test.clj +++ b/test/cipher_analytical_machine/parsers/simple_substitution_test.clj @@ -39,3 +39,11 @@ "1" {"key" 1 "table" {0 \a 1 \b 2 \c}} "2" {"key" 2 "table" {\a 0 \b 1 \c 2}})))) +(deftest invert-table-in-map-test + (testing "The function invert the table in a decoded json." + (are [decoded-json expected-data] + (= expected-data + (invert-table-in-map decoded-json)) + {"key" 1 "table" {0 \a 1 \b 2 \c}} {"key" 1 "table" {\a 0 \b 1 \c 2}} + {"key" 2 "table" {\a 0 \b 1 \c 2}} {"key" 2 "table" {0 \a 1 \b 2 \c}}))) +