Skip to content

Commit

Permalink
rewrite trampoline test
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbicodes committed Sep 4, 2023
1 parent 4c19c15 commit 97e7fa6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
6 changes: 3 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ function testExercisesUntilFail() {
console.log("Fails:", fails)
}

testSolution(randExercise())
//testSolution("poker")
//testSolution(randExercise())
//testSolution("minesweeper")
//loadExercise("all_your_base")
//testExercisesUntilFail()
testExercisesUntilFail()
//testExercises()
25 changes: 24 additions & 1 deletion scratch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,27 @@
(recur (drop step s) (conj p (take n s)))
(conj p (concat (take n s) pad))))

(take 2 '())
(def e [[:a :a] [:b :b]])

(if (#{0 2} (count (filter odd? (vals (frequencies (mapcat seq e))))))
(not (next (reduce
(fn [g e]
(let [[a b] (map (fn [n] (or (some #(if (% n) %) g) #{n})) e)]
(conj (disj g a b) (into a b))))
#{}
e)))
false)

(defn graph
([nodes]
(graph (set (first nodes)) (rest nodes) []))
([connected new-paths parked-nodes]
(if (empty? new-paths)
(empty? parked-nodes)
(let [[a b] (first new-paths)]
(if (or (connected a) (connected b))
(graph (conj connected a b) (concat (rest new-paths) parked-nodes) [])
(graph connected (rest new-paths) (conj parked-nodes [a b])))))))

(graph #{[1 2] [2 3] [3 1]
[4 5] [5 6] [6 4] [3 4]})
2 changes: 1 addition & 1 deletion test/exercises.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"leap" : "(defn leap-year? [year]\r\n (let [pcs (map #(= 0 (rem year %1)) [4 100 400])\r\n p (first pcs)\r\n c (second pcs)\r\n s (last pcs)]\r\n (or (and p (not c))\r\n (and c s))))\r\n",
"zipper" : "(defn from-trail [tree last]\r\n (if (= (nth last 0) \"left\")\r\n {:value (nth last 1), :left tree, :right (nth last 2)}\r\n {:value (nth last 1), :left (nth last 2), :right tree}))\r\n\r\n(defn from-tree [tree]\r\n {:tree tree :trail []})\r\n\r\n(defn value [z]\r\n (:value (:tree z)))\r\n\r\n(defn zipper [tree trail]\r\n {:tree tree :trail trail})\r\n\r\n(defn left [z]\r\n (when (:left (:tree z))\r\n (zipper (:left (:tree z))\r\n (conj [[\"left\" (:value (:tree z)) (:right (:tree z))]]\r\n (:trail z)))))\r\n(defn right [z]\r\n (when (:right (:tree z))\r\n (zipper (:right (:tree z))\r\n (conj [[\"right\" (:value (:tree z)) (:left (:tree z))]]\r\n (:trail z)))))\r\n\r\n(defn rebuild-tree [tree trail]\r\n (if (= 0 (count trail))\r\n tree\r\n (recur (from-trail tree (first trail)) (fnext trail))))\r\n\r\n(defn to-tree [z]\r\n (rebuild-tree (:tree z) (:trail z)))\r\n\r\n(defn up [z]\r\n (when-not (zero? (count (:trail z)))\r\n (zipper (from-trail (:tree z) (first (:trail z)))\r\n (fnext (:trail z)))))\r\n\r\n(defn set-value [z value]\r\n (zipper {:value value,\r\n :left (:left (:tree z)),\r\n :right (:right (:tree z))}\r\n (:trail z)))\r\n\r\n(defn set-left [z left]\r\n (zipper {:value (:value (:tree z)),\r\n :left left,\r\n :right (:right (:tree z))}\r\n (:trail z)))\r\n\r\n(defn set-right [z right]\r\n (zipper {:value (:value (:tree z)),\r\n :left (:left (:tree z)),\r\n :right right}\r\n (:trail z)))\r\n",
"roman_numerals" : "(def numeral-mapping\r\n [[1000 \"M\"]\r\n [900 \"CM\"]\r\n [500 \"D\"]\r\n [400 \"CD\"]\r\n [100 \"C\"]\r\n [90 \"XC\"]\r\n [50 \"L\"]\r\n [40 \"XL\"]\r\n [10 \"X\"]\r\n [9 \"IX\"]\r\n [5 \"V\"]\r\n [4 \"IV\"]\r\n [1 \"I\"]])\r\n\r\n(defn largest-factor [number]\r\n (first (filter\r\n (fn [pl] (<= (first pl) number))\r\n numeral-mapping)))\r\n\r\n(defn numerals [number]\r\n (if (zero? number) \"\"\r\n (let [pl (largest-factor number)\r\n remainder (- number (first pl))]\r\n (str (last pl) (numerals remainder)))))\r\n",
"minesweeper" : "(def ordinals\r\n (disj (set (for [x [-1 0 1]\r\n y [-1 0 1]]\r\n [x y])) [0 0]))\r\n\r\n(def glyphs {:bomb \\*\r\n :empty \" \"})\r\n\r\n(def bomb? #{(:bomb glyphs)})\r\n\r\n(defn bombs [tiles coords]\r\n (reduce + (for [terms ordinals\r\n :when (bomb? (get-in tiles (map + coords terms)))]\r\n 1)))\r\n\r\n(defn sweep [tiles coords]\r\n (let [hits (bombs tiles coords)]\r\n (if (zero? hits)\r\n (:empty glyphs)\r\n hits)))\r\n\r\n(defn flag [tiles coords]\r\n (if (bomb? (get-in tiles coords))\r\n (:bomb glyphs)\r\n (sweep tiles coords)))\r\n\r\n(defn draw [board]\r\n (let [tiles (string/split-lines board)\r\n width (count (first tiles))\r\n height (count tiles)\r\n reveal (for [y (range width) x (range height)]\r\n (flag tiles [x y]))]\r\n (string/join \\newline (map string/join (partition width reveal)))))\r\n",
"minesweeper" : "(def ordinals\r\n (disj (set (for [x [-1 0 1]\r\n y [-1 0 1]]\r\n [x y])) [0 0]))\r\n\r\n(def glyphs {:bomb \\*\r\n :empty \" \"})\r\n\r\n(def bomb? #{(:bomb glyphs)})\r\n\r\n(defn bombs [tiles coords]\r\n (reduce + (for [terms ordinals\r\n :when (bomb? (get-in tiles (map + coords terms)))]\r\n 1)))\r\n\r\n(defn sweep [tiles coords]\r\n (let [hits (bombs tiles coords)]\r\n (if (zero? hits)\r\n (:empty glyphs)\r\n hits)))\r\n\r\n(defn flag [tiles coords]\r\n (if (bomb? (get-in tiles coords))\r\n (:bomb glyphs)\r\n (sweep tiles coords)))\r\n\r\n(defn draw [board]\r\n (let [tiles (str/split-lines board)\r\n width (count (first tiles))\r\n height (count tiles)\r\n reveal (for [y (range width) x (range height)]\r\n (flag tiles [x y]))]\r\n (str/join \\newline (map str/join (partition width reveal)))))\r\n",
"pov" : "(defn path-to [tree to]\r\n (loop [tree tree\r\n path []\r\n stack '()]\r\n (cond\r\n (= to (first tree)) (conj path (first tree))\r\n (= (count tree) 2) (recur (second tree) (conj path (first tree)) stack)\r\n (> (count tree) 2) (recur (second tree) (conj path (first tree))\r\n (concat stack (for [x (drop 2 tree)] [x (conj path (first tree))])))\r\n (empty? stack) nil\r\n :else (let [stack-elem (first stack)]\r\n (recur (first stack-elem) (second stack-elem) (drop 1 stack))))))\r\n\r\n(defn find-elem-in-tree [tree elem]\r\n (loop [tree tree\r\n remaining (drop 1 tree)]\r\n (cond\r\n (= (first tree) elem) tree\r\n (empty? remaining) nil\r\n :else (recur (first remaining) (drop 1 remaining)))))\r\n\r\n(defn remove-elem-in-tree [tree elem]\r\n (into [] (remove\r\n #(\r\n or (= % elem) (and (vector? %) (= (first %) elem)))\r\n tree)))\r\n\r\n(defn tree-elems-from-path [tree path]\r\n (loop [elems []\r\n branch tree\r\n path path]\r\n (cond (empty? path) elems\r\n :else (let [elem (find-elem-in-tree branch (first path))]\r\n (recur (conj elems (remove-elem-in-tree elem (second path))) elem (drop 1 path))))))\r\n\r\n(defn resort-elems [elems]\r\n (loop [tree (first elems)\r\n elems (drop 1 elems)]\r\n (cond\r\n (empty? elems) tree\r\n :else (recur (conj (first elems) tree) (drop 1 elems)))))\r\n\r\n(defn of [to tree]\r\n (->> (path-to tree to)\r\n (tree-elems-from-path tree)\r\n (resort-elems)))\r\n\r\n(defn path-from-to [from to tree]\r\n (path-to (of from tree) to))\r\n",
"matching_brackets" : "(defn valid? [s]\r\n (let [pairs {\")\" \"(\" \"]\" \"[\" \"}\" \"{\"}\r\n opening (set (vals pairs))\r\n closing (set (keys pairs))]\r\n (loop [stack [] s s]\r\n (cond (empty? s) (empty? stack)\r\n (contains? opening (first s)) (recur (conj stack (first s)) (rest s))\r\n (contains? closing (first s)) (if (= (peek stack) (get pairs (first s)))\r\n (recur (pop stack) (rest s))\r\n false)\r\n :else (recur stack (rest s))))))\r\n",
"dominoes" : "(defn is-connected? [graph]\r\n (cond\r\n (< (count graph) 2) true\r\n :else (let [start-point (first (keys graph))]\r\n (loop [vertices []\r\n explored #{start-point}\r\n frontier [start-point]]\r\n (if (empty? frontier)\r\n (= (set vertices) (set (keys graph)))\r\n (let [v (peek frontier)\r\n neighbors (get graph v)]\r\n (recur\r\n (concat vertices [v])\r\n (into explored neighbors)\r\n (into (pop frontier) (remove explored neighbors)))))))))\r\n\r\n(defn can-chain? [stones]\r\n (and (is-connected? (reduce\r\n (fn [graph stone] (-> graph\r\n (update (first stone) #(concat % [(second stone)]))\r\n (update (second stone) #(concat % [(first stone)]))))\r\n {} stones))\r\n (every? even? (vals (frequencies (flatten stones))))))\r\n",
Expand Down
Loading

0 comments on commit 97e7fa6

Please sign in to comment.