Skip to content

Commit 6c5dab3

Browse files
committed
string permutations exercise
1 parent 26e8c6d commit 6c5dab3

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

Diff for: README

-13
This file was deleted.

Diff for: README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ Coding exercises, handy practice for technical interview questions. I will put u
55

66
All of the exercises are coded in my favorite language, Clojure.
77

8-
Most of the exercises are taken from "Cracking the Coding Interview, 4th Edition, by Gayle Laakmann.
8+
Most of the exercises are taken from "Cracking the Coding Interview, 4th Edition" by Gayle Laakmann.
9+
10+
## License
11+
12+
Copyright (C) 2012 David Petrovics
13+
14+
Distributed under the Eclipse Public License, the same as Clojure.

Diff for: src/coding_exercises/core.clj

+9
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@
2929
(float
3030
(math/abs (- hr-angle min-angle)))))
3131

32+
(defn word-frequencies
33+
^{:doc "My implementation of the Clojure 'frequency' function. Takes in a sentence (string) and outputs a map with words as keys and their frequency as values."}
34+
[sentence]
35+
(reduce
36+
(fn [res wrd] (assoc res wrd
37+
(inc (get res wrd 0))))
38+
{}
39+
(re-seq #"\w+" sentence)))
40+
3241

Diff for: src/coding_exercises/string-permutations.clj

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(ns string-permutations
2+
(:require [clojure.string :as str]))
3+
4+
(def problem-statement
5+
"Example: Design an algorithm to print all permutations of a string.")
6+
7+
(comment
8+
"The algorithm checks to see if the word is of length one, if so,
9+
then just return a list with that word. If the word is longer, chop
10+
off the last letter, and insert it in every spot in all permutations
11+
of the word minus the last letter. It is a recursive solution.")
12+
13+
(defn insert-letter
14+
^{:doc "Returns a collection of each new word produced by inserting the letter in all possible spots in the word."}
15+
[word letter]
16+
(let [length (count word)]
17+
(for [num (range 0 (inc length))]
18+
(str (subs word 0 num)
19+
letter
20+
(subs word num length)))))
21+
22+
(defn word-permutations
23+
^{:doc "Returns a collection of all permutations of the given word."}
24+
[s]
25+
(if (= (count s) 1)
26+
(list s)
27+
(let [subword (subs s 0 (dec (count s))) ;;ex: 'bar', subword = 'ba'
28+
letter (str (last (seq s)))] ;;the last letter, ie 'r' in 'bar'
29+
(apply concat
30+
(for [word (word-permutations subword)]
31+
(insert-letter word letter))))))
32+
33+
(defn factorial
34+
^{:doc "Returns the factorial of a number."}
35+
[num]
36+
{:pre [(>= num 1)]}
37+
(if (= num 1)
38+
1
39+
(* num (factorial (dec num)))))
40+
41+
(defn num-permutations
42+
^{:doc "Displays the number of permutations for a given word"}
43+
[word]
44+
(factorial
45+
(count word)))

0 commit comments

Comments
 (0)