From 26e5d635eaa735d45f2824ecdacbd9ca6792dd40 Mon Sep 17 00:00:00 2001 From: Jani Rahkola Date: Mon, 22 Oct 2012 01:57:59 +0300 Subject: [PATCH] Added exercises seq-max, longest-sequence, take-while and drop-while. --- src/recursion.clj | 12 ++++++++++++ test/recursion_test.clj | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/recursion.clj b/src/recursion.clj index 71be8e7..8cc9a55 100644 --- a/src/recursion.clj +++ b/src/recursion.clj @@ -9,12 +9,24 @@ (defn my-last [coll] :-) +(defn seq-max [a-seq] + :-) + +(defn longest-sequence [a-seq] + [:-]) + (defn my-filter [f a-seq] [:-]) (defn sequence-contains? [elem a-seq] :-) +(defn my-take-while [pred? a-seq] + [:-]) + +(defn my-drop-while [pred? a-seq] + [:-]) + (defn seq= [a-seq b-seq] :-) diff --git a/test/recursion_test.clj b/test/recursion_test.clj index 891e4d0..a870677 100644 --- a/test/recursion_test.clj +++ b/test/recursion_test.clj @@ -20,6 +20,16 @@ (my-last [1 2 3]) => 3 (my-last [2 5]) => 5) +(facts "seq-max" + (seq-max [2 4 1 4]) => 4 + (seq-max [2]) => 2 + (seq-max []) => nil) + +(facts "longest-sequence" + (longest-sequence [[1 2] [] [1 2 3]]) => [1 2 3] + (longest-sequence [[1 2]]) => [1 2] + (longest-sequence []) => nil) + (facts "my-filter" (my-filter odd? [1 2 3 4]) => '(1 3) (my-filter (fn [x] (> x 9000)) [12 49 90 9001]) => '(9001) @@ -30,6 +40,18 @@ (sequence-contains? 3 [4 7 9]) => false (sequence-contains? :pony []) => false) +(facts "my-take-while" + (my-take-while odd? [1 2 3 4]) => '(1) + (my-take-while odd? [1 3 4 5]) => '(1 3) + (my-take-while even? [1 3 4 5]) => '() + (my-take-while odd? []) => '()) + +(facts "my-drop-while" + (my-drop-while odd? [1 2 3 4]) => '(2 3 4) + (my-drop-while odd? [1 3 4 5]) => '(4 5) + (my-drop-while even? [1 3 4 5]) => '(1 3 4 5) + (my-drop-while odd? []) => '()) + (facts "seq=" (seq= [1 2 4] '(1 2 4)) => true (seq= [1 2 3] [1 2 3 4]) => false