diff --git a/main.js b/main.js index 17d92ba..06a0023 100644 --- a/main.js +++ b/main.js @@ -170,7 +170,7 @@ function testExercisesUntilFail() { } //testSolution(randExercise()) -testSolution("binary") +testSolution("wordy") //loadExercise("go_counting") //testExercisesUntilFail() //testExercises() \ No newline at end of file diff --git a/scratch.clj b/scratch.clj index 214f214..9e9f8df 100644 --- a/scratch.clj +++ b/scratch.clj @@ -484,4 +484,10 @@ (if fs__2853 (concat fs__2853 (iter__2850 (rest s__2851))) (#function[do-mod] (rest s__2851))))))))] - (remove nil? (G__2849 (range 3)))) \ No newline at end of file + (remove nil? (G__2849 (range 3)))) + +(def expr "What is 1 plus 1?") + +(re-matches #"What is (.+)\?" expr) + +(partition-all 2 ["plus" "1"]) \ No newline at end of file diff --git a/src/clj/core.clj b/src/clj/core.clj index ff0a490..0da949a 100644 --- a/src/clj/core.clj +++ b/src/clj/core.clj @@ -444,13 +444,14 @@ (defn str/split-lines [s] (str/split s #"\r?\n")) -(defn partition-all [n step coll] - (if-not coll - (partition-all n n step) - (loop* [s coll p []] - (if (= 0 (count s)) p - (recur (drop step s) - (conj p (take n s))))))) +(defn partition-all + ([n coll] + (partition-all n n coll)) + ([n step coll] + (loop* [s coll p []] + (if (= 0 (count s)) p + (recur (drop step s) + (conj p (take n s))))))) (defn partition-by [f coll] (loop* [s (seq coll) res []] @@ -551,9 +552,9 @@ `(if-let ~bindings ~then nil) (let* [form (get bindings 0) tst (get bindings 1) temp# (gensym)] - `(let* [temp# ~tst] + `(let [temp# ~tst] (if temp# - (let* [~form temp#] + (let [~form temp#] ~then) ~else))))) diff --git a/test/exercises.json b/test/exercises.json index 024c582..7c4205c 100644 --- a/test/exercises.json +++ b/test/exercises.json @@ -126,7 +126,7 @@ "meetup" : "(def day-structure\r\n {1 :sunday 2 :monday 3 :tuesday 4 :wednesday\r\n 5 :thursday 6 :friday 7 :saturday})\r\n\r\n(defn leap-year? [year]\r\n (cond (zero? (mod year 400)) true\r\n (zero? (mod year 100)) false\r\n :else (zero? (mod year 4))))\r\n\r\n(defn zellers-congruence [input_year input_month input_day]\r\n (let [month (+ (mod (+ input_month 9) 12) 3)\r\n year (- input_year (quot (- month input_month) 12))\r\n century (quot year 100)\r\n century-year (mod year 100)]\r\n (mod (+ input_day\r\n (quot (* 26 (inc month)) 10)\r\n century-year\r\n (quot century-year 4)\r\n (quot century 4)\r\n (* 5 century)) 7)))\r\n\r\n(defn get-day-counts [year]\r\n {1 31, 2 (if (leap-year? year) 29 28), 3 31, 4 30\r\n 5 31, 6 30, 7 31, 8 31, 9 30, 10 31, 11 30, 12 31})\r\n\r\n(defn get-days\r\n ([year month]\r\n (get-days year month\r\n (zellers-congruence year month 1)\r\n (get-in (get-day-counts year) [month])))\r\n ([year month start-day limit]\r\n (loop [count 2\r\n day (inc start-day)\r\n day-arrangement {1 (get-in day-structure [start-day])}]\r\n (if (not= count (inc limit))\r\n (recur (inc count)\r\n (if (= (inc day) 8) 1 (inc day))\r\n (assoc day-arrangement count\r\n (get-in day-structure [day])))\r\n day-arrangement))))\r\n\r\n(defn filter-by-day [year month day]\r\n (let [days (get-days year month)]\r\n (apply hash-map (flatten (filter #(-> % val (= day)) days)))))\r\n\r\n(defn filter-keys [year month day style]\r\n (let [days (filter-by-day year month day)\r\n dates (sort (keys days))]\r\n (cond\r\n (= style :first)\r\n (nth dates 0)\r\n (= style :second)\r\n (nth dates 1)\r\n (= style :third)\r\n (nth dates 2)\r\n (= style :fourth)\r\n (nth dates 3)\r\n (= style :last)\r\n (nth dates (dec (count dates)))\r\n (= style :teenth)\r\n (first (filter #(and (> % 12) (< % 20)) (vec dates))))))\r\n\r\n(defn meetup [month year day style]\r\n [year month (filter-keys year month day style)])", "two_fer" : "(defn two-fer\r\n ([] \"One for you, one for me.\")\r\n ([name]\r\n (str \"One for \" name \", one for me.\")\r\n \"One for you, one for me.\"))\r\n", "run_length_encoding" : "(defn encoder-groups [string]\r\n (re-seq #\"(.)\\\\1*\" string))\r\n\r\n(defn encoder-values [group]\r\n (str (if (> (count group) 1)\r\n (count group)\r\n \"\")\r\n (first group)))\r\n\r\n(defn run-length-encode [s]\r\n (let [groups (re-seq #\"(.)\\\\1*\" s)]\r\n (apply str\r\n (map encoder-values groups))))\r\n\r\n(defn decoder-groups [string]\r\n (re-seq #\"(\\\\d+)?(.)\" string))\r\n\r\n(defn decoder-values [group]\r\n (apply str\r\n (repeat (when-let [n (first (re-seq #\"\\\\d+\" group))]\r\n (Integer/parseInt n))\r\n (last group))))\r\n\r\n(defn run-length-decode [s]\r\n (let [groups (re-seq #\"(\\\\d+)?(.)\" s)]\r\n (apply str\r\n (map decoder-values groups))))\r\n", - "wordy" : "(def ops {\"plus\" +\r\n \"minus\" -\r\n \"multiplied by\" *\r\n \"divided by\" /})\r\n\r\n(def tokens-pattern (re-pattern\r\n (str (join \"|\" (keys ops)) \"|-?\\\\d+|\\\\S+\")))\r\n\r\n(defn parse-op [op-str]\r\n (or (ops op-str)\r\n (throw (str \"unknown operator \" op-str))))\r\n\r\n(defn evaluate [expr]\r\n (if-let [[_ exprs] (re-matches #\"What is (.+)\\?\" expr)]\r\n (if-let [[token & tokens] (re-seq tokens-pattern exprs)]\r\n (reduce (fn [acc [op x]]\r\n ((parse-op op) acc (Integer/parseInt x)))\r\n (Integer/parseInt token) (partition-all 2 tokens))\r\n (throw \"no arithmetic expression found\"))\r\n (throw \"cannot recognize question\")))\r\n", + "wordy" : "(def ops {\"plus\" +\r\n \"minus\" -\r\n \"multiplied by\" *\r\n \"divided by\" /})\r\n\r\n(def tokens-pattern\r\n (str (join \"|\" (keys ops)) \"|-?\\\\d+|\\\\S+\"))\r\n\r\n(defn parse-op [op-str]\r\n (or (ops op-str)\r\n (throw (str \"unknown operator \" op-str))))\r\n\r\n(defn evaluate [expr]\r\n (if-let [[_ exprs] (re-matches #\"What is (.+)\\?\" expr)]\r\n (if-let [[token & tokens] (re-seq tokens-pattern exprs)]\r\n (reduce (fn [acc [op x]]\r\n ((parse-op op) acc (Integer/parseInt x)))\r\n (Integer/parseInt token) (partition-all 2 tokens))\r\n (throw \"no arithmetic expression found\"))\r\n (throw \"cannot recognize question\")))\r\n", "sieve" : "(defn prime? [n]\r\n (->> n\r\n Math/sqrt\r\n Math/floor\r\n inc\r\n (range 2)\r\n (filter #(zero? (rem n %)))\r\n empty?))\r\n(defn sieve [n]\r\n (->> n\r\n inc\r\n (range 2)\r\n (filter prime?)))\r\n", "binary" : "(defn power [[exponent bit]]\r\n (if (= \"1\" bit)\r\n (Math/pow 2 exponent)\r\n 0))\r\n\r\n(defn bits [string]\r\n (->> string\r\n (re-seq #\"[10]\")\r\n reverse\r\n (map-indexed vector)))\r\n\r\n(defn to-decimal [string]\r\n (->> string\r\n bits\r\n (map power)\r\n (apply +)))\r\n", "pig_latin" : "(defn starts-with-any [prefixes word]\r\n (some (partial str/starts-with? word) prefixes))\r\n\r\n(defn starts-with-vowel-like? [word]\r\n (starts-with-any #{\"yt\" \"xr\"} word))\r\n\r\n(defn starts-with-vowel? [word]\r\n (starts-with-any #{\"a\" \"e\" \"i\" \"o\" \"u\"} word))\r\n\r\n(defn starts-with-two-letter-prefix? [word]\r\n (starts-with-any #{\"ch\" \"qu\" \"th\" \"rh\"} word))\r\n\r\n(defn starts-with-three-letter-prefix? [word]\r\n (starts-with-any #{\"thr\" \"sch\"} word))\r\n\r\n(defn starts-with-qu-and-preceding-consonant? [word]\r\n (and (not (starts-with-vowel? word))\r\n (str/starts-with? (subs word 1) \"qu\")))\r\n\r\n(defn rotate [word n]\r\n (str (subs word n) (subs word 0 n)))\r\n\r\n(defn append-ay [word]\r\n (str word \"ay\"))\r\n\r\n(defn translate-word [word]\r\n (cond\r\n (or (starts-with-vowel? word)\r\n (starts-with-vowel-like? word))\r\n (append-ay word)\r\n\r\n (or\r\n (starts-with-three-letter-prefix? word)\r\n (starts-with-qu-and-preceding-consonant? word))\r\n (append-ay (rotate word 3))\r\n\r\n (starts-with-two-letter-prefix? word)\r\n (append-ay (rotate word 2))\r\n\r\n :else\r\n (append-ay (rotate word 1))))\r\n\r\n(defn translate [words]\r\n (->> (str/split words #\" \")\r\n (map translate-word)\r\n (str/join \" \")))\r\n",