forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Clojure solutions and tests for chapter 1, question 1
- Loading branch information
Nitin Punjabi
committed
Jul 5, 2014
1 parent
f7c418a
commit a338fac
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |