Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Krisch
committed
Nov 13, 2011
1 parent
e2548f3
commit ea5d3cd
Showing
5 changed files
with
61 additions
and
9 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
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
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,25 @@ | ||
(ns link-shortener.counter) | ||
|
||
(def | ||
^{:private true} | ||
alpha "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
|
||
(def alpha-length (.length alpha)) | ||
|
||
(defn allButLast [s] | ||
(if (= (.length s) 0) | ||
"" | ||
(.substring s 0 (- (.length s) 1)))) | ||
|
||
(defn nextInAlpha [c] | ||
(let [index (inc (.indexOf alpha (str c))) | ||
index (if (= alpha-length index) 0 index)] | ||
(str (.charAt alpha index)))) | ||
|
||
(defn n [s] | ||
(loop [f (allButLast s) | ||
l (nextInAlpha (last s)) | ||
acc ""] | ||
(if (not= l "0") | ||
(str f l acc) | ||
(recur (allButLast f) (nextInAlpha (last f)) (str l acc))))) |
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
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,22 @@ | ||
(ns link-shortener.test.counter | ||
(:use [link-shortener.counter]) | ||
(:use clojure.test)) | ||
|
||
(deftest test_nextInAlpha | ||
(is (= (nextInAlpha "z") "A") "Should have been A.") | ||
(is (= (nextInAlpha "Z") "0") "Should have been 0.") | ||
(is (= (nextInAlpha "0") "1") "Should have been 1.") | ||
(is (= (nextInAlpha \Z) "0") "Didn't handle char correctly.") | ||
(is (= (nextInAlpha "") "1") "Didn't handle emptry string correctly.")) | ||
|
||
(deftest test_allButLast | ||
(is (= (allButLast "asdf") "asd") "Why didn't it return asd?") | ||
(is (= (allButLast "a") "") "This shouldn't return anything.") | ||
(is (= (allButLast "") "") "This shouldn't return anything.")) | ||
|
||
(deftest test_multichar | ||
(is (= (n "ab") "ac") "Why didn't it return ac?") | ||
(is (= (n "Z") "10") "Why didn't it return 10?") | ||
(is (= (n "ZZ") "100") "Why didn't it return 100?") | ||
(is (= (n "zZ") "A0") "Why didn't it return A0?") | ||
(is (= (n "asdfZZ") "asdg00") "Why didn't it return asdg00?")) |