Skip to content

Commit

Permalink
Implement Clojure solutions and tests for chapter 1, question 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitin Punjabi committed Jul 5, 2014
1 parent f7c418a commit a338fac
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions clojure/chapter01/01_01.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
; Clojure solution to question 1.1 from Cracking the Coding Interview, 5th Edition.
; Nitin Punjabi
; github.com/nitinpunjabi
; [email protected]
(ns chapter01.01-01
(:require [clojure.test :as t]))

(defn unique-by-set-count?
"Solution using a set."
[s]
(let [coll (into #{} s)]
(= (count coll) (count s))))

(defn unique-by-partition?
"Solution using partitioning."
[s]
(let [parts (partition-by identity (sort s))]
(not (some #(> (count %) 1) parts))))

(t/deftest unique-by-set-count
(t/is (= (unique-by-set-count? "") true))
(t/is (= (unique-by-set-count? "a") true))
(t/is (= (unique-by-set-count? "ab") true))
(t/is (= (unique-by-set-count? "aab") false))
(t/is (= (unique-by-set-count? "aba") false)))

(t/deftest unique-by-partition
(t/is (= (unique-by-partition? "") true))
(t/is (= (unique-by-partition? "a") true))
(t/is (= (unique-by-partition? "ab") true))
(t/is (= (unique-by-partition? "aab") false))
(t/is (= (unique-by-partition? "aba") false)))

0 comments on commit a338fac

Please sign in to comment.