From ddd4e253d449a7d5b5c12639e42560764ebc1763 Mon Sep 17 00:00:00 2001 From: Jani Rahkola Date: Mon, 22 Oct 2012 03:45:49 +0300 Subject: [PATCH] Added seq-max and max-element. --- src/recursion.clj | 25 +++++++++++++++---------- test/recursion_test.clj | 37 ++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/recursion.clj b/src/recursion.clj index 8cc9a55..95d77aa 100644 --- a/src/recursion.clj +++ b/src/recursion.clj @@ -9,13 +9,16 @@ (defn my-last [coll] :-) -(defn seq-max [a-seq] +(defn max-element [a-seq] :-) +(defn seq-max [seq-1 seq-2] + [:-]) + (defn longest-sequence [a-seq] [:-]) -(defn my-filter [f a-seq] +(defn my-filter [pred? a-seq] [:-]) (defn sequence-contains? [elem a-seq] @@ -33,14 +36,13 @@ (defn my-map [f seq-1 seq-2] [:-]) -(defn power [a b] +(defn power [n k] :-) (defn fib [n] :-) -(defn my-repeat [how-many-times - what-to-repeat] +(defn my-repeat [how-many-times what-to-repeat] [:-]) (defn my-range [up-to] @@ -52,14 +54,14 @@ (defn inits [a-seq] [:-]) -(defn split-into-monotonics [a-seq] +(defn rotations [a-seq] [:-]) -(defn rotations [a-seq] +(defn my-frequencies-helper [freqs a-seq] [:-]) (defn my-frequencies [a-seq] - :-) + [:-]) (defn un-frequencies [a-map] [:-]) @@ -79,9 +81,12 @@ (defn merge-sort [a-seq] [:-]) -(defn permutations [a-seq] +(defn split-into-monotonics [a-seq] + [:-]) + +(defn permutations [a-set] [:-]) -(defn powerset [a-seq] +(defn powerset [a-set] [:-]) diff --git a/test/recursion_test.clj b/test/recursion_test.clj index a870677..29257ff 100644 --- a/test/recursion_test.clj +++ b/test/recursion_test.clj @@ -20,10 +20,14 @@ (my-last [1 2 3]) => 3 (my-last [2 5]) => 5) +(facts "max-element" + (max-element [2 4 1 4]) => 4 + (max-element [2]) => 2 + (max-element []) => nil) + (facts "seq-max" - (seq-max [2 4 1 4]) => 4 - (seq-max [2]) => 2 - (seq-max []) => nil) + (seq-max [1] [1 2]) => [1 2] + (seq-max [1 2] [3 4]) => [3 4]) (facts "longest-sequence" (longest-sequence [[1 2] [] [1 2 3]]) => [1 2 3] @@ -85,26 +89,22 @@ (facts "my-range" (my-range 0) => '() - (my-range 1) => [0] - (my-range 2) => [1 0] - (my-range 3) => [2 1 0]) + (my-range 1) => '(0) + (my-range 2) => '(1 0) + (my-range 3) => '(2 1 0)) (facts "tails" (tails [1 2 3 4]) => (just [[1 2 3 4] [2 3 4] [3 4] [4] []] :in-any-order) - (tails []) => [[]] + (tails []) => (just [[]]) (tails [1]) => (just [[1] []] :in-any-order)) (facts "inits" (inits [1 2 3 4]) => (just [[] [1] [1 2] [1 2 3] [1 2 3 4]] :in-any-order) - (inits []) => [[]] + (inits []) => (just [[]]) (inits [1]) => (just [[] [1]] :in-any-order)) -(facts "split-into-monotonics" - (split-into-monotonics [0 1 2 1 0]) => '((0 1 2) (1 0)) - (split-into-monotonics [0 5 4 7 1 3]) => '((0 5) (4 7) (1 3))) - (facts "rotations" - (rotations []) => [] + (rotations []) => '(()) (rotations [1 2 3]) => (just [[1 2 3] [2 3 1] [3 1 2]] :in-any-order) (rotations [:a :b]) => (just [[:a :b] [:b :a]] :in-any-order) (rotations [1 5 9 2]) => (just '(1 5 9 2) '(2 1 5 9) @@ -148,13 +148,16 @@ (merge-sort [1 2 3]) => '(1 2 3) (merge-sort [5 3 4 17 2 100 1]) => '(1 2 3 4 5 17 100)) +(facts "split-into-monotonics" + (split-into-monotonics [0 1 2 1 0]) => '((0 1 2) (1 0)) + (split-into-monotonics [0 5 4 7 1 3]) => '((0 5) (4 7) (1 3))) + (facts "permutations" - (permutations []) => [] + (permutations []) => '(()) (permutations [1 5 3]) => (just [[1 5 3] [5 1 3] [5 3 1] [1 3 5] [3 1 5] [3 5 1]] :in-any-order)) (facts "powerset" - (powerset []) => '(()) - (powerset [1 2 4]) => (just [[] [4] [2] [2 4] [1] [1 4] [1 2] [1 2 4]] - :in-any-order)) + (powerset []) => #{#{}} + (powerset [1 2 4]) => #{#{} #{4} #{2} #{2 4} #{1} #{1 4} #{1 2} #{1 2 4}}) \ No newline at end of file