Add a function to get the ordered sequence that can form a key.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -7,13 +7,22 @@
|
|||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn get-possible-key-vector
|
(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]
|
[possible-key-map symbols]
|
||||||
(reduce
|
(reduce
|
||||||
(fn [acc el]
|
(fn [acc el]
|
||||||
(conj acc (get possible-key-map el)))
|
(conj acc (get possible-key-map el)))
|
||||||
[] symbols))
|
[] 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
|
(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\" ...)."
|
"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]
|
[possible-key-vector]
|
||||||
|
|||||||
@@ -13,6 +13,13 @@
|
|||||||
{\a [\a \b] \b [\b \a] \c [\a \c]} "abc" [[\a \b] [\b \a] [\a \c]]
|
{\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]])))
|
{\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
|
(deftest get-possible-keys-test
|
||||||
(testing "The keys must be the all combinations"
|
(testing "The keys must be the all combinations"
|
||||||
(are [possible-keys-vector expected-keys]
|
(are [possible-keys-vector expected-keys]
|
||||||
|
|||||||
Reference in New Issue
Block a user