Created
January 2, 2020 12:40
-
-
Save dktn/18cf3ef8eb9fb7775fe1ae3cdd8db3e6 to your computer and use it in GitHub Desktop.
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
(ns bsf) | |
(defn queue | |
([] (clojure.lang.PersistentQueue/EMPTY)) | |
([coll] | |
(reduce conj clojure.lang.PersistentQueue/EMPTY coll))) | |
(def graph1 | |
{"you" '("alice" "bob" "claire") | |
"bob" '("anuj" "peggy") | |
"alice" '("peggy") | |
"claire" '("thom" "johny") | |
"anuj" '() | |
"peggy" '() | |
"thom" '() | |
"johny" '() | |
}) | |
(defn is-seller [n] | |
(= (subs n (dec (count n))) "m")) | |
(defn search [graph name] | |
(letfn | |
[(searchInQueue [searched queue] | |
(let | |
[person (peek queue) | |
persons (pop queue)] | |
(cond | |
(contains? searched person) (recur searched persons) | |
(is-seller person) person | |
:else (recur (conj searched person) (into persons (graph person))) | |
)))] | |
(searchInQueue #{} (into (queue) (graph name))))) | |
(defn -main [] | |
(println (search graph1 "you")) | |
(println "[done]")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment