Skip to content

Instantly share code, notes, and snippets.

@dktn
Created January 2, 2020 12:40
Show Gist options
  • Save dktn/18cf3ef8eb9fb7775fe1ae3cdd8db3e6 to your computer and use it in GitHub Desktop.
Save dktn/18cf3ef8eb9fb7775fe1ae3cdd8db3e6 to your computer and use it in GitHub Desktop.
(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