Skip to content

Commit 07beed5

Browse files
committed
Several new problems and solutions added. Some of the examples are easier to solve with side effects and mutable data structures, so I solved those in Java.
1 parent 6c5dab3 commit 07beed5

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/.lein-failures
77
/checkouts
88
/.lein-deps-sum
9+
*.DS_Store

Diff for: src/coding_exercises/chapter1.clj

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
(ns chapter1
2+
[:use coding-exercises.core]
3+
[:require [clojure.string :as s]])
4+
5+
(def problem1
6+
"Implement an algorithm to determine if a string has all unique
7+
characters What if you can not use additional data structures?")
8+
9+
(comment
10+
"SEE JAVA file for solution. There are 256 ASCII
11+
characters (character encoding scheme). You could create an array
12+
with 256 slots, and then make array[current-char] = true as you
13+
iterate through the chars in the word. As you iterate, check if
14+
array[current-char] already = true, in which case there is a repeat
15+
char (ie not unique). There are 256 ASCII chars because each ASCII
16+
char gets one byte (8bits, ie 2^8 combos). Unicode gets more bytes
17+
so it can support more chars.")
18+
19+
(defn unique-chars?
20+
^{:doc "Problem with this solution is that it
21+
goes all the way through the string, even if a character has already
22+
been repeated. (It builds the whole frequency map before it checks
23+
the count)"}
24+
[s]
25+
(every? #(= 1 %1)
26+
(vals
27+
(frequencies s))))
28+
29+
(defn unique-chars-bit
30+
"Uses one long to store a bit vector. This means we have 8 bytes (64 bits) available for storage. Only A-Z, a-z are allowed in the input string, no numbers or special chars."
31+
[s]
32+
33+
)
34+
35+
(def problem2
36+
"Write code to reverse a string.")
37+
38+
(defn my-reverse
39+
[word]
40+
(apply str
41+
(for [i (range 0 (count word))]
42+
(.charAt word
43+
(- (dec (count word))
44+
i)))))
45+
46+
(defn another-reverse
47+
[word]
48+
(apply str
49+
(reduce conj () word)))
50+
51+
(comment
52+
"This would be easiest to do with (reverse word).")
53+
54+
(def problem3
55+
"Design an algorithm and write code to remove the duplicate
56+
characters in a string without using any additional buffer. Assume
57+
this means keep the first time a char appears, but remove any
58+
subsequent appearances. SEE SOLUTION IN JAVA FILE")
59+
60+
61+
(def problem4
62+
"Write a method to decide if two strings are anagrams or not.")
63+
64+
;;clojure is awesome for this problem
65+
(defn anagrams? ^{:doc "Takes in two strings and returns true if they
66+
are anagrams, false otherwise."}
67+
[s1 s2]
68+
(= (frequencies s1)
69+
(frequencies s2)))
70+
71+
(defn anagrams2?
72+
^{:doc "Checks to see if two strings are anagrams, ignores whitespace and capitalization." }
73+
[s1 s2]
74+
(= (frequencies (s/replace (s/lower-case s1) " " ""))
75+
(frequencies (s/replace (s/lower-case s2) " " ""))))

Diff for: src/coding_exercises/chapter5.clj

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(ns chapter5
2+
[:use coding-exercises.core])
3+
4+
;;BITWISE OPERATIONS
5+
6+
(defn my-encode
7+
^{:doc "Takes in an age (0-127), a gender (0 or 1), and a height (0-127), and encodes them in a 2byte (16bit) short."}
8+
[age gender height]
9+
(short
10+
(bit-or
11+
(bit-shift-left
12+
(bit-or
13+
(bit-shift-left age 1)
14+
gender)
15+
7)
16+
height)))
17+
18+
(defn my-decode
19+
^{:doc "Takes in a short encoded with (my-encode) and spits out a str with the decoded age. height, and gender."}
20+
[num]
21+
(str "age: "
22+
(bit-shift-right num 8)
23+
", gender: "
24+
(bit-and
25+
(bit-shift-right num 7) 1)
26+
", height: "
27+
(bit-and num 127)))
28+
29+
(defn binStr
30+
^{:doc "Outputs the binary string representation of the number."}
31+
[num]
32+
(. Long toBinaryString num))

Diff for: src/coding_exercises/core.clj

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818
(map first
1919
(iterate (fn [[a b]] [b (+ a b)]) [0 1])))
2020

21+
(def map-example
22+
"You have a collection of 2-tuples (user, follower). You want to get out (user, [followers]). Solution: put them all into a map, and use merge-with.")
23+
24+
(def inputs
25+
'(("Angie" "Dave") ("Angie" "Max") ("Bill" "Joe") ("Joe" "Moe") ("Bill" "Dave") ("Mike" "Dave") ("Angie" "Bill")))
26+
27+
(defn user-followers
28+
[uf-list]
29+
(let [usr-map (map #(apply hash-map %1) uf-list)]
30+
(apply merge-with
31+
#(if (seq? %1)
32+
(conj %1 %2)
33+
(list %1 %2))
34+
usr-map)))
35+
2136
(defn clock-angle
2237
^{:doc "Finds the angle in degrees between the hour hand and minute hand."}
2338
[hr min]
@@ -39,3 +54,6 @@
3954
(re-seq #"\w+" sentence)))
4055

4156

57+
(defn power [base exp]
58+
(nth
59+
(iterate #(* %1 base) 1) exp))

Diff for: src/coding_exercises/data-structures.clj

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(ns data-structures
2+
)
3+

0 commit comments

Comments
 (0)