├── .clj-kondo └── config.edn ├── .dir-locals.el ├── .github └── workflows │ └── deploy.yml ├── .gitignore ├── CHANGELOG.md ├── Makefile ├── README.md ├── bb.edn ├── bin ├── bb ├── dev └── rewrite-data ├── deps.edn ├── dev └── user.clj ├── package-lock.json ├── package.json ├── public ├── css │ ├── editor.css │ └── sakura-ink.css ├── data │ └── problems.json └── index.template.html ├── shadow-cljs.edn └── src └── app ├── core.cljs ├── data.cljc ├── editor.cljs ├── editor_ex.cljs ├── editor_settings.cljs ├── error.cljs ├── home.cljs ├── modal.cljs ├── problem.cljs ├── routes.cljs ├── sci.cljs ├── solution.cljs └── state.cljs /.clj-kondo/config.edn: -------------------------------------------------------------------------------- 1 | {:lint-as {reagent.core/with-let clojure.core/let 2 | applied-science.js-interop/defn clojure.core/defn} 3 | :linters {:unsorted-required-namespaces {:level :warning}}} 4 | -------------------------------------------------------------------------------- /.dir-locals.el: -------------------------------------------------------------------------------- 1 | ((nil . ((cider-clojure-cli-aliases . "-A:dev") 2 | (cider-custom-cljs-repl-init-form . "(user/cljs-repl)") 3 | (cider-default-cljs-repl . custom) 4 | (cider-preferred-build-tool . clojure-cli) 5 | (cider-redirect-server-output-to-repl . t) 6 | (cider-repl-display-help-banner . nil) 7 | (clojure-toplevel-inside-comment-form . t) 8 | (eval . (progn 9 | (make-variable-buffer-local 'cider-jack-in-nrepl-middlewares) 10 | (add-to-list 'cider-jack-in-nrepl-middlewares "shadow.cljs.devtools.server.nrepl/middleware"))) 11 | (eval . (define-clojure-indent 12 | (assoc 0) 13 | (ex-info 0)))))) 14 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Build and Deploy 2 | on: 3 | push: 4 | branches: 5 | - main 6 | permissions: 7 | contents: read 8 | pages: write 9 | id-token: write 10 | jobs: 11 | build-and-deploy: 12 | concurrency: ci-${{ github.ref }} # Recommended if you intend to make multiple deployments in quick succession. 13 | runs-on: ubuntu-latest 14 | steps: 15 | 16 | - name: Checkout 🛎️ 17 | uses: actions/checkout@v3 18 | 19 | - name: Prepare java 20 | uses: actions/setup-java@v3 21 | with: 22 | distribution: 'zulu' 23 | java-version: '11' 24 | 25 | - name: Install clojure tools 26 | uses: DeLaGuardo/setup-clojure@10.1 27 | with: 28 | # Install just one or all simultaneously 29 | # The value must indicate a particular version of the tool, or use 'latest' 30 | # to always provision the latest version 31 | cli: latest 32 | bb: latest 33 | 34 | - name: Install and Build 🔧 # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built. 35 | run: | 36 | bb build 37 | 38 | # To be able to deploy your own version you have to choose the 39 | # "GitHub Actions" option in your project settings. 40 | # It may not work if "deploy from a branch" is chosen. 41 | - name: Setup Pages 42 | uses: actions/configure-pages@v2 43 | 44 | - name: Upload artifact 45 | uses: actions/upload-pages-artifact@v1 46 | with: 47 | # Upload entire repository 48 | path: 'public' 49 | 50 | - name: Deploy to GitHub Pages 51 | id: deployment 52 | uses: actions/deploy-pages@v1 53 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /public/js/ 3 | /.shadow-cljs/ 4 | /.cpcache 5 | /node_modules 6 | /.nrepl-port 7 | /.store/ 8 | /.clj-kondo/.cache 9 | .cache 10 | /public/index.html 11 | .clj-kondo/babashka/ 12 | .clj-kondo/rewrite-clj/ 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Unreleased 4 | 5 | ## 0.6.0 6 | 7 | - sci upgrade, removed hack for evaluating "infinite" sequences 8 | - add title and difficulty info to problem page 9 | - dedupe solutions to only show 1 of each 10 | - enable gzip 11 | 12 | ## 0.5.0 13 | 14 | - input field bug fixes 15 | - fix some problems to work with js 16 | - added traffic light on each test case 17 | - improved design 18 | - macro not working in editor fix 19 | 20 | ## 0.4.0 21 | 22 | - improved bracket matching 23 | - fixes to problem routing state 24 | - improved stacktraces, upgraded sci 25 | 26 | ## 0.3.0 27 | 28 | - added solutions archive for each problem 29 | 30 | ## 0.2.0 31 | 32 | - cache busting 33 | - store solutions in localstorage 34 | - improve js interop 35 | - improved sci 36 | 37 | ## 0.1.0 38 | 39 | - first version deployed 40 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY=deploy 2 | 3 | deploy: 4 | bin/dev deploy 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 4ever-clojure 2 | 3 | Unfortunately 4clojure is shutting down: 4 | https://twitter.com/borkdude/status/1412117420173561861 5 | 6 | Original thread that explains why 4clojure is shutting down: 7 | https://groups.google.com/g/clojure/c/ZWmDEzvn-Js 8 | 9 | > Now that 4clojure is shutting down (thanks for all the year of hosting it!), 10 | > perhaps it's time to consider some alternatives. - borkdude 11 | 12 | @borkdude suggested this in his tweet: 13 | 14 | > Another alternative would be to port it to self-hosted CLJS or sci and host it 15 | > on Github pages and use localstorage. No need to maintain a running JVM server 16 | > somewhere that way. Perhaps with the option to download your solutions as an 17 | > archive. 18 | 19 | And so I made it, using the awesome [sci](https://github.com/borkdude/sci) and 20 | special thanks to [4bb](https://github.com/porkostomus/4bb) from where I copied 21 | the problems edn. 22 | 23 | ## Live 24 | 25 | Deployed at [https://4clojure.oxal.org/](https://4clojure.oxal.org/) 26 | 27 | ## Solutions archive 28 | 29 | You can view the solutions from each individual page eg: https://4clojure.oxal.org/#/problem/102/solutions 30 | 31 | The archive repo is available at: https://github.com/oxalorg/4clojure-solutions-archive/ 32 | 33 | ## Develop 34 | 35 | The easiest way to start up a local server would be to install [Babashka](https://babashka.org/) and run the `dev` script defined in `bb.edn`: 36 | 37 | ``` 38 | bb dev 39 | ``` 40 | 41 | Alternatively, you can carry out the instructions of the Babashka script manually: 42 | 43 | ``` 44 | npm install # only needed once 45 | cp "public/index.template.html" "public/index.html" 46 | npx shadow-cljs watch :my-build 47 | ``` 48 | 49 | If you are a fan of emacs and cider, then you can open up emacs and run 50 | `cider-jack-in-cljs`. It will automatically start shadow-cljs in watch 51 | mode. 52 | 53 | shadow hosts the dev server at http://localhost:8000 54 | 55 | ## Test your own version live 56 | 57 | You can deploy your own instance via GitHub Actions: 58 | 59 | 1. Go to Settings -> Pages 60 | 1. Choose Build and deployment -> Github Actions 61 | 1. Upload your repository or trigger the build 62 | 63 | ## Todos 64 | 65 | - [x] Ship a crude version 66 | - [x] Make it noice! 67 | - [x] use localstorage to store solutions 68 | - [x] easy navigation 69 | - [x] scrape problem difficulty (can perhaps also get ranks data?) 70 | - [ ] better alerts (modals?) 71 | - [x] show user which problems they have solved 72 | - [ ] import /export data of the user in a .edn file 73 | - [ ] github actions auto deployment 74 | - [ ] create a new section of problems "community powered questions" 75 | - [ ] add new community problems directly via github 76 | -------------------------------------------------------------------------------- /bb.edn: -------------------------------------------------------------------------------- 1 | {:tasks 2 | {npm:install {:doc "Installs NPM deps" 3 | :task (shell "npm install")} 4 | dev {:doc "watches build with shadow-cljs" 5 | :depends [npm:install] 6 | :requires ([babashka.fs :as fs]) 7 | :task (do 8 | (fs/copy "public/index.template.html" "public/index.html" {:replace-existing true}) 9 | (shell "npx shadow-cljs watch my-build"))} 10 | build {:doc "builds production build" 11 | :depends [npm:install] 12 | :task (shell "bb bin/dev build")}}} 13 | -------------------------------------------------------------------------------- /bin/bb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Wrapper script for babashka to be dropped into projects, will run `bb` from 4 | # the PATH if it exists, or otherwise download it and store it inside the 5 | # project. When using the system `bb` it will do a version check and warn if the 6 | # version is older than what is requested. 7 | # 8 | # Will look for a `bb_deps.edn` and run that through `clojure` to compute a 9 | # classpath. 10 | # 11 | # Will use rlwrap if it is available. 12 | 13 | name=babashka 14 | babashka_version="0.3.8" 15 | store_dir="$(pwd)/.store" 16 | install_dir="${store_dir}/$name-$babashka_version" 17 | 18 | system_bb="$(which bb)" 19 | set -e 20 | 21 | # https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash 22 | vercomp () { 23 | if [[ $1 == $2 ]] 24 | then 25 | return 0 26 | fi 27 | local IFS=. 28 | local i ver1=($1) ver2=($2) 29 | # fill empty fields in ver1 with zeros 30 | for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) 31 | do 32 | ver1[i]=0 33 | done 34 | for ((i=0; i<${#ver1[@]}; i++)) 35 | do 36 | if [[ -z ${ver2[i]} ]] 37 | then 38 | # fill empty fields in ver2 with zeros 39 | ver2[i]=0 40 | fi 41 | if ((10#${ver1[i]} > 10#${ver2[i]})) 42 | then 43 | return 1 44 | fi 45 | if ((10#${ver1[i]} < 10#${ver2[i]})) 46 | then 47 | return 2 48 | fi 49 | done 50 | return 0 51 | } 52 | 53 | if [[ -f "$system_bb" ]]; then 54 | bb_path="$system_bb" 55 | elif [[ -f "$install_dir/bb" ]]; then 56 | bb_path="$install_dir/bb" 57 | else 58 | case "$(uname -s)" in 59 | Linux*) 60 | ext=tar.gz 61 | unpack_bb() { 62 | tar -xzf "bb.$ext" -C "$install_dir" 63 | } 64 | platform=linux;; 65 | Darwin*) 66 | ext=tar.gz 67 | unpack_bb() { 68 | tar -xzf "bb.$ext" -C "$install_dir" 69 | } 70 | platform=macos;; 71 | MINGW64*) 72 | ext=zip 73 | unpack_bb() { 74 | unzip -qqo "bb.$ext" -d "$install_dir" 75 | } 76 | platform=windows;; 77 | esac 78 | 79 | echo "$name $babashka_version not found, installing to $install_dir..." 80 | download_url="https://github.com/borkdude/babashka/releases/download/v$babashka_version/babashka-$babashka_version-$platform-amd64.$ext" 81 | 82 | mkdir -p "$install_dir" 83 | echo -e "Downloading $download_url." 84 | curl -o "bb.$ext" -sL "$download_url" 85 | unpack_bb 86 | rm "bb.$ext" 87 | bb_path="$install_dir/bb" 88 | fi 89 | 90 | set +e 91 | actual_version="$($bb_path --version | sed 's/babashka v//')" 92 | 93 | vercomp $actual_version $babashka_version 94 | case $? in 95 | 0) ;; # = 96 | 1) ;; # > 97 | 2) echo "WARNING: babashka version is $actual_version, expected $babashka_version" ;; # < 98 | esac 99 | set -e 100 | 101 | try_exec_rlwrap() { 102 | if [ -x "$(command -v rlwrap)" ]; then 103 | exec "rlwrap" "$@" 104 | else 105 | exec "$@" 106 | fi 107 | } 108 | 109 | deps_clj="$(pwd)/.store/deps.clj" 110 | 111 | ensure_deps_clj() { 112 | if [[ ! -f "${deps_clj}" ]]; then 113 | mkdir -p "${store_dir}" 114 | curl -sL https://raw.githubusercontent.com/borkdude/deps.clj/master/deps.clj -o "${deps_clj}" 115 | fi 116 | } 117 | 118 | if [[ -f bb_deps.edn ]]; then 119 | ensure_deps_clj 120 | # Note this will install clojure-tools in ~/.deps.clj/ClojureTools 121 | cp="$($bb_path $deps_clj -Srepro -Sdeps-file bb_deps.edn -Spath)" 122 | try_exec_rlwrap "$bb_path" "-cp" "${cp}" "$@" 123 | else 124 | try_exec_rlwrap "$bb_path" "$@" 125 | fi 126 | 127 | ;; local variables: 128 | ;; mode:shell 129 | ;; end: -------------------------------------------------------------------------------- /bin/dev: -------------------------------------------------------------------------------- 1 | #!bin/bb 2 | 3 | (require '[clojure.string :as str] 4 | '[clojure.java.io :as io] 5 | '[clojure.core :as c] 6 | '[clojure.pprint] 7 | '[clojure.java.shell :as sh] 8 | '[cheshire.core :as json] 9 | '[babashka.fs :as fs]) 10 | 11 | ;; Helpers 12 | (defn process-builder [args] 13 | (doto (ProcessBuilder. args) 14 | (.redirectInput java.lang.ProcessBuilder$Redirect/INHERIT) 15 | (.redirectError java.lang.ProcessBuilder$Redirect/INHERIT) 16 | (.redirectOutput java.lang.ProcessBuilder$Redirect/INHERIT))) 17 | 18 | (defn cmd->str [args] 19 | (str/join " " (map #(if (str/includes? % " ") (pr-str %) %) args))) 20 | 21 | (defn sh [& args] 22 | (let [[opts args] (if (map? (last args)) 23 | [(last args) (butlast args)] 24 | [{} args]) 25 | dir (:dir opts)] 26 | (println "=>" (cmd->str args) (if dir (str "(in ./" dir ")") "")) 27 | (-> (process-builder args) 28 | (cond-> dir 29 | (.directory (io/file dir))) 30 | .start 31 | .waitFor))) 32 | 33 | (defn print-help [prefix commands] 34 | (println "usage:" prefix "[command] [command_args...]") 35 | (println) 36 | (doseq [[cmd {:keys [description]}] commands] 37 | (println (format " %-17s%s" cmd description)))) 38 | 39 | (defn cli-run! 40 | [args commands] 41 | (if-let [{:keys [command]} (get commands (first args))] 42 | (command (next args)) 43 | ((-> commands 44 | (get "help") 45 | :command) nil))) 46 | 47 | (defn hash-index [] 48 | (let [original-index (slurp "./public/index.template.html") 49 | hashed-file-name (some->> "./public/js/manifest.edn" 50 | slurp 51 | read-string 52 | (filter #(= (:module-id %) :my-main)) 53 | first 54 | :output-name) 55 | hashed-index (str/replace original-index 56 | #"my-main.js" 57 | hashed-file-name)] 58 | (spit "./public/index.html" hashed-index))) 59 | 60 | (defn build [_] 61 | (sh "npx" "shadow-cljs" "release" ":my-build") 62 | (hash-index)) 63 | 64 | (def commands 65 | (sorted-map 66 | "build" 67 | {:description "shadow production build & deploy" 68 | :command (fn [_] 69 | (build {}))} 70 | "deploy" 71 | {:description "shadow production build & deploy" 72 | :command (fn [_] 73 | (build {}) 74 | (sh "rsync" "-a" "public/" "ark2:/srv/ox/4clojure" "-v"))} 75 | "help" 76 | {:description "show this help information" 77 | :command (fn [_] (print-help "bin/dev" commands))})) 78 | 79 | 80 | (cli-run! *command-line-args* commands) 81 | 82 | ;; local variables: 83 | ;; mode:clojure 84 | ;; end: 85 | -------------------------------------------------------------------------------- /bin/rewrite-data: -------------------------------------------------------------------------------- 1 | (require '[clojure.string :as str] 2 | '[clojure.java.io :as io] 3 | '[clojure.core :as c] 4 | '[clojure.pprint] 5 | '[rewrite-clj.zip :as z] 6 | '[rewrite-clj.node :as node] 7 | '[clojure.java.shell :as sh] 8 | '[cheshire.core :as json] 9 | '[babashka.fs :as fs]) 10 | 11 | (defn csv-data->maps [csv-data] 12 | (map zipmap 13 | (->> (first csv-data) ;; First row is the header 14 | (map keyword) ;; Drop if you want string keys instead 15 | repeat) 16 | (rest csv-data))) 17 | 18 | (def data 19 | ;; thanks @humorless for list.csv 20 | (with-open [reader (io/reader (io/file "list.csv"))] 21 | (doall 22 | (-> 23 | (csv/read-csv reader) 24 | csv-data->maps)))) 25 | 26 | (def data2 27 | (into {} 28 | (comp 29 | (map #(assoc % :id (-> % 30 | :url 31 | (str/replace "https://www.4clojure.com/problem/" "") 32 | Integer/parseInt))) 33 | (map #(dissoc % :url)) 34 | (map #(update % :tags (fn [tag-str] 35 | (if (= tag-str "") 36 | [] 37 | (str/split (str/lower-case tag-str) #" "))))) 38 | (map #(hash-map (:id %) %))) 39 | data)) 40 | 41 | (def zloc (z/of-file (io/file "src/app/data.cljc"))) 42 | 43 | #_(def new-data 44 | (-> (z/find-value zloc z/next 'problems) 45 | z/right 46 | (z/edit (fn [problems] 47 | (into [] 48 | (comp 49 | (map #(merge % (get data2 (:id %)))) 50 | (map #(dissoc % :id))) 51 | problems))) 52 | z/root)) 53 | 54 | (def problem-loc (-> (z/find-value zloc z/next 'problems) 55 | z/right 56 | z/down)) 57 | 58 | (def out 59 | (let [rewrite-loc (loop [loc problem-loc 60 | previous-loc problem-loc] 61 | (cond 62 | (nil? loc) previous-loc 63 | :else 64 | (let [problem (z/sexpr loc) 65 | {:keys [difficulty tags]} (get data2 (:id problem) {})] 66 | (recur (-> loc 67 | (z/append-child (node/newlines 1)) 68 | (z/append-child (node/spaces 4)) 69 | (z/append-child :difficulty) 70 | (z/append-child (if difficulty 71 | (str/lower-case difficulty) 72 | "")) 73 | (z/append-child (node/newlines 1)) 74 | (z/append-child (node/spaces 4)) 75 | ;; FIXME: need to update existing tag node 76 | ;; not add a new one 77 | (z/append-child :tags) 78 | (z/append-child tags) 79 | z/right) 80 | loc))))] 81 | (-> rewrite-loc 82 | z/root))) 83 | 84 | (comment 85 | (spit "src/app/data.cljc" out :encoding "UTF-8")) 86 | 87 | ;; local variables: 88 | ;; mode:clojure 89 | ;; end: 90 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["src" "resources"] 2 | :deps 3 | {reagent/reagent {:mvn/version "1.1.0"} 4 | org.babashka/sci {:mvn/version "0.6.37"} 5 | alandipert/storage-atom {:mvn/version "2.0.1"} 6 | metosin/reitit {:mvn/version "0.5.13"} 7 | applied-science/js-interop {:mvn/version "0.2.5"} 8 | lambdaisland/fetch {:mvn/version "1.0.33"} 9 | nextjournal/clojure-mode {:git/url "https://github.com/nextjournal/clojure-mode" 10 | :git/sha "a83c87cd2bd2049b70613f360336a096d15c5518"}} 11 | 12 | :aliases 13 | {:dev 14 | {:extra-paths ["dev"] 15 | :extra-deps {thheller/shadow-cljs {:mvn/version "2.14.5"} 16 | cider/cider-nrepl {:mvn/version "0.28.3"}}}}} 17 | -------------------------------------------------------------------------------- /dev/user.clj: -------------------------------------------------------------------------------- 1 | (ns user) 2 | 3 | (defmacro jit [sym] 4 | `(requiring-resolve '~sym)) 5 | 6 | (defn start-shadow-cljs 7 | ([] 8 | (start-shadow-cljs :main)) 9 | ([build-id] 10 | (when (nil? @@(jit shadow.cljs.devtools.server.runtime/instance-ref)) 11 | ((jit shadow.cljs.devtools.server/start!)) 12 | ((jit shadow.cljs.devtools.api/watch) build-id) 13 | (loop [] 14 | (when (nil? @@(jit shadow.cljs.devtools.server.runtime/instance-ref)) 15 | (Thread/sleep 250) 16 | (recur)))))) 17 | 18 | (defn cljs-repl 19 | ([] 20 | (cljs-repl :my-build)) 21 | ([build-id] 22 | (start-shadow-cljs build-id) 23 | ((jit shadow.cljs.devtools.api/nrepl-select) build-id))) 24 | 25 | (defn browse [] 26 | ((jit clojure.java.browse/browse-url) 27 | "http://localhost:8000")) 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "reagent-example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "dependencies": { 7 | "@codemirror/autocomplete": "0.18.8", 8 | "@codemirror/closebrackets": "^0.18.0", 9 | "@codemirror/commands": "^0.18.3", 10 | "@codemirror/comment": "0.18.1", 11 | "@codemirror/fold": "^0.18.1", 12 | "@codemirror/gutter": "^0.18.4", 13 | "@codemirror/highlight": "^0.18.4", 14 | "@codemirror/history": "^0.18.1", 15 | "@codemirror/language": "0.18.2", 16 | "@codemirror/lint": "0.18.4", 17 | "@codemirror/matchbrackets": "0.18.0", 18 | "@codemirror/rectangular-selection": "0.18.0", 19 | "@codemirror/search": "0.18.4", 20 | "@codemirror/state": "^0.18.7", 21 | "@codemirror/view": "^0.18.18", 22 | "ace-builds": "^1.4.12", 23 | "lezer": "^0.13.5", 24 | "lezer-clojure": "^0.1.10", 25 | "lezer-generator": "^0.13.4", 26 | "lezer-tree": "^0.13.2", 27 | "platform": "^1.3.6", 28 | "react": "^17.0.2", 29 | "react-ace": "^9.4.1", 30 | "react-dom": "^17.0.2", 31 | "stacktrace-js": "^2.0.2", 32 | "w3c-keyname": "^2.2.4" 33 | }, 34 | "devDependencies": { 35 | "shadow-cljs": "^2.16.8" 36 | }, 37 | "scripts": { 38 | "test": "echo \"Error: no test specified\" && exit 1" 39 | }, 40 | "keywords": [], 41 | "author": "", 42 | "license": "ISC" 43 | } 44 | -------------------------------------------------------------------------------- /public/css/editor.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --dark-teal-color: #095960; 3 | --darkest-teal-color: #052e31; 4 | --dark-teal-transparent-color: rgba(7, 73, 78, .1); 5 | 6 | --teal-color: #31afd0; 7 | --light-teal-color: #eff8fb; 8 | --teal-transparent-color: rgba(0, 153, 170, .04); 9 | 10 | --silver-color: #e3e7ef; 11 | --light-silver-color: #eef1f6; 12 | 13 | --near-white-color: #f8f8f8; 14 | --near-black-color: #2e2e2c; 15 | --gray-color: #555; 16 | 17 | --green-color: #74c080; 18 | --green-transparent-color: rgba(116, 192, 128, .1); 19 | 20 | --red-color: #d64242; 21 | --red-transparent-color: rgba(214, 66, 66, .1); 22 | 23 | --dark-red-color: #ca2834; 24 | --orange-color: #ff6300; 25 | --blue-color: #0073d9; 26 | 27 | --checkerboard-color: #eee; 28 | } 29 | 30 | .fill-white { fill: white; } 31 | 32 | .dark-teal { color: var(--dark-teal-color); } 33 | .bg-dark-teal { background-color: var(--dark-teal-color); } 34 | .hover-bg-dark-teal:hover { background-color: var(--dark-teal-color); } 35 | .fill-dark-teal { fill: var(--dark-teal-color); } 36 | .border-color-teal { border-color: var(--dark-teal-color); } 37 | 38 | .teal { color: var(--teal-color); } 39 | .bg-teal { background-color: var(--teal-color); } 40 | .hover-bg-teal:hover { background-color: var(--teal-color); } 41 | .fill-teal { fill: var(--teal-color); } 42 | .border-color-teal { border-color: var(--teal-color); } 43 | .bg-teal-transparent { background-color: var(--teal-transparent-color); } 44 | .hover-bg-teal-transparent:hover { background-color: var(--teal-transparent-color); } 45 | 46 | .light-teal { color: var(--light-teal-color); } 47 | .bg-light-teal { background-color: var(--light-teal-color); } 48 | .hover-bg-light-teal:hover { background-color: var(--light-teal-color); } 49 | .fill-light-teal { fill: var(--light-teal-color); } 50 | .border-color-light-teal { border-color: var(--light-teal-color); } 51 | 52 | .red { color: var(--red-color); } 53 | .bg-red { background-color: var(--red-color); } 54 | .hover-bg-red:hover { background-color: var(--red-color); } 55 | .fill-red { fill: var(--red-color); } 56 | .border-color-red { border-color: var(--red-color); } 57 | 58 | .dark-red { color: var(--dark-red-color); } 59 | .bg-dark-red { background-color: var(--dark-red-color); } 60 | .hover-bg-dark-red:hover { background-color: var(--dark-red-color); } 61 | .fill-dark-red { fill: var(--dark-red-color); } 62 | .border-color-red { border-color: var(--dark-red-color); } 63 | 64 | .green { color: var(--green-color); } 65 | .bg-green { background-color: var(--green-color); } 66 | .hover-bg-green:hover { background-color: var(--green-color); } 67 | .fill-green { fill: var(--green-color); } 68 | .border-color-green { border-color: var(--green-color); } 69 | .green-contrast { color: #469c48; } 70 | 71 | .silver { color: var(--silver-color); } 72 | .bg-silver { background-color: var(--silver-color); } 73 | .hover-bg-silver:hover { background-color: var(--silver-color); } 74 | .fill-silver { fill: var(--silver-color); } 75 | .border-color-silver { border-color: var(--silver-color); } 76 | 77 | .light-silver { color: var(--light-silver-color); } 78 | .bg-light-silver { background-color: var(--light-silver-color); } 79 | .hover-bg-light-silver:hover { background-color: var(--light-silver-color); } 80 | .fill-light-silver { fill: var(--light-silver-color); } 81 | .border-color-light-silver { border-color: var(--light-silver-color); } 82 | 83 | .near-white { color: var(--near-white-color); } 84 | .bg-near-white { background-color: var(--near-white-color); } 85 | .hover-bg-near-white:hover { background-color: var(--near-white-color); } 86 | .fill-near-white { fill: var(--near-white-color); } 87 | .border-color-near-white { border-color: var(--near-white-color); } 88 | 89 | .near-black { color: var(--near-black-color); } 90 | .bg-near-black { background-color: var(--near-black-color); } 91 | .hover-bg-near-black:hover { background-color: var(--near-black-color); } 92 | .fill-near-black { fill: var(--near-black-color); } 93 | .border-color-near-black { border-color: var(--near-black-color); } 94 | 95 | .fill-black-50 { fill: rgba(0,0,0,.5); } 96 | .fill-orange { fill: var(--orange-color); } 97 | .fill-blue { fill: var(--blue-color); } 98 | 99 | .bg-checkerboard { 100 | background-position: 0px 0px, 10px 10px; 101 | background-size: 20px 20px; 102 | background-image: 103 | linear-gradient(45deg, var(--checkerboard-color) 25%, transparent 25%, transparent 75%, var(--checkerboard-color) 75%, var(--checkerboard-color) 100%), 104 | linear-gradient(45deg, var(--checkerboard-color) 25%, white 25%, white 75%, var(--checkerboard-color) 75%, var(--checkerboard-color) 100%); 105 | } 106 | 107 | .black-95 { color: rgba(0,0,0,.95); } 108 | .black-90 { color: rgba(0,0,0,.9); } 109 | .black-80 { color: rgba(0,0,0,.8); } 110 | .black-70 { color: rgba(0,0,0,.7); } 111 | .black-60 { color: rgba(0,0,0,.6); } 112 | .black-50 { color: rgba(0,0,0,.5); } 113 | .black-40 { color: rgba(0,0,0,.4); } 114 | .black-30 { color: rgba(0,0,0,.3); } 115 | .black-20 { color: rgba(0,0,0,.2); } 116 | .black-10 { color: rgba(0,0,0,.1); } 117 | .black-05 { color: rgba(0,0,0,.05); } 118 | 119 | .bg-black-95 { background-color: rgba(0,0,0,.95); } 120 | .bg-black-90 { background-color: rgba(0,0,0,.9); } 121 | .bg-black-80 { background-color: rgba(0,0,0,.8); } 122 | .bg-black-70 { background-color: rgba(0,0,0,.7); } 123 | .bg-black-60 { background-color: rgba(0,0,0,.6); } 124 | .bg-black-50 { background-color: rgba(0,0,0,.5); } 125 | .bg-black-40 { background-color: rgba(0,0,0,.4); } 126 | .bg-black-30 { background-color: rgba(0,0,0,.3); } 127 | .bg-black-20 { background-color: rgba(0,0,0,.2); } 128 | .bg-black-10 { background-color: rgba(0,0,0,.1); } 129 | .bg-black-05 { background-color: rgba(0,0,0,.05); } 130 | 131 | .hover-bg-black-95:hover { background-color: rgba(0,0,0,.95); } 132 | .hover-bg-black-90:hover { background-color: rgba(0,0,0,.9); } 133 | .hover-bg-black-80:hover { background-color: rgba(0,0,0,.8); } 134 | .hover-bg-black-70:hover { background-color: rgba(0,0,0,.7); } 135 | .hover-bg-black-60:hover { background-color: rgba(0,0,0,.6); } 136 | .hover-bg-black-50:hover { background-color: rgba(0,0,0,.5); } 137 | .hover-bg-black-40:hover { background-color: rgba(0,0,0,.4); } 138 | .hover-bg-black-30:hover { background-color: rgba(0,0,0,.3); } 139 | .hover-bg-black-20:hover { background-color: rgba(0,0,0,.2); } 140 | .hover-bg-black-10:hover { background-color: rgba(0,0,0,.1); } 141 | .hover-bg-black-05:hover { background-color: rgba(0,0,0,.05); } 142 | 143 | .border-color-black-95 { border-color: rgba(0,0,0,.95); } 144 | .border-color-black-90 { border-color: rgba(0,0,0,.9); } 145 | .border-color-black-80 { border-color: rgba(0,0,0,.8); } 146 | .border-color-black-70 { border-color: rgba(0,0,0,.7); } 147 | .border-color-black-60 { border-color: rgba(0,0,0,.6); } 148 | .border-color-black-50 { border-color: rgba(0,0,0,.5); } 149 | .border-color-black-40 { border-color: rgba(0,0,0,.4); } 150 | .border-color-black-30 { border-color: rgba(0,0,0,.3); } 151 | .border-color-black-20 { border-color: rgba(0,0,0,.2); } 152 | .border-color-black-10 { border-color: rgba(0,0,0,.1); } 153 | .border-color-black-05 { border-color: rgba(0,0,0,.05); } 154 | 155 | .white-95 { color: rgba(255,255,255,.95); } 156 | .white-90 { color: rgba(255,255,255,.9); } 157 | .white-80 { color: rgba(255,255,255,.8); } 158 | .white-70 { color: rgba(255,255,255,.7); } 159 | .white-60 { color: rgba(255,255,255,.6); } 160 | .white-50 { color: rgba(255,255,255,.5); } 161 | .white-40 { color: rgba(255,255,255,.4); } 162 | .white-30 { color: rgba(255,255,255,.3); } 163 | .white-20 { color: rgba(255,255,255,.2); } 164 | .white-10 { color: rgba(255,255,255,.1); } 165 | .white-05 { color: rgba(255,255,255,.05); } 166 | 167 | .bg-white-95 { background-color: rgba(255,255,255,.95); } 168 | .bg-white-90 { background-color: rgba(255,255,255,.9); } 169 | .bg-white-80 { background-color: rgba(255,255,255,.8); } 170 | .bg-white-70 { background-color: rgba(255,255,255,.7); } 171 | .bg-white-60 { background-color: rgba(255,255,255,.6); } 172 | .bg-white-50 { background-color: rgba(255,255,255,.5); } 173 | .bg-white-40 { background-color: rgba(255,255,255,.4); } 174 | .bg-white-30 { background-color: rgba(255,255,255,.3); } 175 | .bg-white-20 { background-color: rgba(255,255,255,.2); } 176 | .bg-white-10 { background-color: rgba(255,255,255,.1); } 177 | .bg-white-05 { background-color: rgba(255,255,255,.05); } 178 | 179 | .text-near-black { 180 | color: var(--near-black-color); 181 | } 182 | 183 | .dim { 184 | opacity: 1; 185 | transition: opacity .15s ease-in; 186 | } 187 | .dim:hover, 188 | .dim:focus { 189 | opacity: .5; 190 | transition: opacity .15s ease-in; 191 | } 192 | 193 | .opacity-05 { opacity: 0.5; } 194 | .opacity-10 { opacity: 0.1; } 195 | .opacity-20 { opacity: 0.2; } 196 | .opacity-30 { opacity: 0.3; } 197 | .opacity-40 { opacity: 0.4; } 198 | .opacity-60 { opacity: 0.6; } 199 | .opacity-70 { opacity: 0.7; } 200 | .opacity-80 { opacity: 0.8; } 201 | .opacity-90 { opacity: 0.9; } 202 | 203 | .z-max { 204 | z-index: 2147483647; 205 | } 206 | -------------------------------------------------------------------------------- /public/css/sakura-ink.css: -------------------------------------------------------------------------------- 1 | /* Sakura.css v1.3.1 2 | * ================ 3 | * Minimal css theme. 4 | * Project: https://github.com/oxalorg/sakura/ 5 | */ 6 | /* Body */ 7 | html { 8 | font-size: 62.5%; 9 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif; } 10 | 11 | body { 12 | font-size: 1.8rem; 13 | line-height: 1.618; 14 | max-width: 38em; 15 | margin: auto; 16 | color: rgba(0, 0, 0, 0.85); 17 | background-color: #ffffff; 18 | padding: 13px; } 19 | 20 | @media (max-width: 684px) { 21 | body { 22 | font-size: 1.53rem; } } 23 | 24 | @media (max-width: 382px) { 25 | body { 26 | font-size: 1.35rem; } } 27 | 28 | h1, h2, h3, h4, h5, h6 { 29 | line-height: 1.1; 30 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif; 31 | font-weight: 700; 32 | margin-top: 3rem; 33 | margin-bottom: 1.5rem; 34 | overflow-wrap: break-word; 35 | word-wrap: break-word; 36 | -ms-word-break: break-all; 37 | word-break: break-word; } 38 | 39 | h1 { 40 | font-size: 2.35em; } 41 | 42 | h2 { 43 | font-size: 2.00em; } 44 | 45 | h3 { 46 | font-size: 1.75em; } 47 | 48 | h4 { 49 | font-size: 1.5em; } 50 | 51 | h5 { 52 | font-size: 1.25em; } 53 | 54 | h6 { 55 | font-size: 1em; } 56 | 57 | p { 58 | margin-top: 0px; 59 | margin-bottom: 2.5rem; } 60 | 61 | small, sub, sup { 62 | font-size: 75%; } 63 | 64 | hr { 65 | border-color: #3b22ea; } 66 | 67 | a { 68 | text-decoration: none; 69 | color: #3b22ea; } 70 | a:hover { 71 | color: #DA4453; 72 | border-bottom: 2px solid rgba(0, 0, 0, 0.85); } 73 | a:visited { 74 | color: #2913c6; } 75 | 76 | ul { 77 | padding-left: 1.4em; 78 | margin-top: 0px; 79 | margin-bottom: 2.5rem; } 80 | 81 | li { 82 | margin-bottom: 0.4em; } 83 | 84 | blockquote { 85 | margin-left: 0px; 86 | margin-right: 0px; 87 | padding-left: 1em; 88 | padding-top: 0.8em; 89 | padding-bottom: 0.8em; 90 | padding-right: 0.8em; 91 | border-left: 5px solid #3b22ea; 92 | margin-bottom: 2.5rem; 93 | background-color: #f7f7f7; } 94 | 95 | blockquote p { 96 | margin-bottom: 0; } 97 | 98 | img, video { 99 | height: auto; 100 | max-width: 100%; 101 | margin-top: 0px; 102 | margin-bottom: 2.5rem; } 103 | 104 | /* Pre and Code */ 105 | pre { 106 | background-color: #f7f7f7; 107 | display: block; 108 | padding: 1em; 109 | overflow-x: auto; 110 | margin-top: 0px; 111 | margin-bottom: 2.5rem; } 112 | 113 | code { 114 | font-size: 0.9em; 115 | padding: 0 0.5em; 116 | background-color: #f7f7f7; 117 | white-space: pre-wrap; } 118 | 119 | pre > code { 120 | padding: 0; 121 | background-color: transparent; 122 | white-space: pre; } 123 | 124 | /* Tables */ 125 | table { 126 | text-align: justify; 127 | width: 100%; 128 | border-collapse: collapse; } 129 | 130 | td, th { 131 | padding: 0.5em; 132 | border-bottom: 1px solid #f7f7f7; } 133 | 134 | /* Buttons, forms and input */ 135 | input, textarea { 136 | border: 1px solid rgba(0, 0, 0, 0.85); } 137 | input:focus, textarea:focus { 138 | border: 1px solid #3b22ea; } 139 | 140 | textarea { 141 | width: 100%; } 142 | 143 | .button, button, input[type="submit"], input[type="reset"], input[type="button"] { 144 | display: inline-block; 145 | padding: 5px 10px; 146 | text-align: center; 147 | text-decoration: none; 148 | white-space: nowrap; 149 | background-color: #3b22ea; 150 | color: #ffffff; 151 | border-radius: 1px; 152 | border: 1px solid #3b22ea; 153 | cursor: pointer; 154 | box-sizing: border-box; } 155 | .button[disabled], button[disabled], input[type="submit"][disabled], input[type="reset"][disabled], input[type="button"][disabled] { 156 | cursor: default; 157 | opacity: .5; } 158 | .button:focus:enabled, .button:hover:enabled, button:focus:enabled, button:hover:enabled, input[type="submit"]:focus:enabled, input[type="submit"]:hover:enabled, input[type="reset"]:focus:enabled, input[type="reset"]:hover:enabled, input[type="button"]:focus:enabled, input[type="button"]:hover:enabled { 159 | background-color: #DA4453; 160 | border-color: #DA4453; 161 | color: #ffffff; 162 | outline: 0; } 163 | 164 | textarea, select, input { 165 | color: rgba(0, 0, 0, 0.85); 166 | padding: 6px 10px; 167 | /* The 6px vertically centers text on FF, ignored by Webkit */ 168 | margin-bottom: 10px; 169 | background-color: #f7f7f7; 170 | border: 1px solid #f7f7f7; 171 | border-radius: 4px; 172 | box-shadow: none; 173 | box-sizing: border-box; } 174 | textarea:focus, select:focus, input:focus { 175 | border: 1px solid #3b22ea; 176 | outline: 0; } 177 | 178 | input[type="checkbox"]:focus { 179 | outline: 1px dotted #3b22ea; } 180 | 181 | label, legend, fieldset { 182 | display: block; 183 | margin-bottom: .5rem; 184 | font-weight: 600; } 185 | -------------------------------------------------------------------------------- /public/data/problems.json: -------------------------------------------------------------------------------- 1 | [{ "_id" : 1, "description" : "This is a clojure form. Enter a value which will make the form evaluate to true. Don't over think it! If you are confused, see the getting started page. Hint: true is equal to true.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ true)" ], "times-solved" : 1432, "title" : "Nothing but the Truth", "user" : "dbyrne" }, 2 | { "_id" : 2, "description" : "

If you are not familiar with polish notation, simple arithmetic might seem confusing.

Note: Enter only enough to fill in the blank (in this case, a single number) - do not retype the whole problem.

", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= (- 10 (* 2 3)) __)" ], "times-solved" : 1389, "title" : "Simple Math", "user" : "dbyrne" }, 3 | { "_id" : 3, "description" : "Clojure strings are Java strings. This means that you can use any of the Java string methods on Clojure strings.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ (.toUpperCase \"hello world\"))" ], "times-solved" : 1373, "title" : "Intro to Strings", "user" : "dbyrne" }, 4 | { "_id" : 4, "description" : "Lists can be constructed with either a function or a quoted form.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= (list __) '(:a :b :c))" ], "times-solved" : 1294, "title" : "Intro to Lists", "user" : "dbyrne" }, 5 | { "_id" : 6, "description" : "Vectors can be constructed several ways. You can compare them with lists.\r\n

Note: the brackets [] surrounding the blanks __ are part of the test case.", "difficulty" : "Elementary", "tags" : null, "tests" : [ "(= [__] (list :a :b :c) (vec '(:a :b :c)) (vector :a :b :c))" ], "times-solved" : 1177, "title" : "Intro to Vectors", "user" : "dbyrne", "restricted" : null }, 6 | { "_id" : 7, "description" : "When operating on a Vector, the conj function will return a new vector with one or more items \"added\" to the end.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ (conj [1 2 3] 4))", "(= __ (conj [1 2] 3 4))" ], "times-solved" : 1165, "title" : "Vectors: conj", "user" : "dbyrne" }, 7 | { "_id" : 8, "description" : "Sets are collections of unique values.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ (set '(:a :a :b :c :c :c :c :d :d)))", "(= __ (clojure.set/union #{:a :b :c} #{:b :c :d}))" ], "times-solved" : 1128, "title" : "Intro to Sets", "user" : "dbyrne" }, 8 | { "_id" : 9, "description" : "When operating on a set, the conj function returns a new set with one or more keys \"added\".", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= #{1 2 3 4} (conj #{1 4 3} __))" ], "times-solved" : 1108, "title" : "Sets: conj", "user" : "dbyrne" }, 9 | { "_id" : 10, "description" : "Maps store key-value pairs. Both maps and keywords can be used as lookup functions. Commas can be used to make maps more readable, but they are not required.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ ((hash-map :a 10, :b 20, :c 30) :b))", "(= __ (:b {:a 10, :b 20, :c 30}))" ], "times-solved" : 1082, "title" : "Intro to Maps", "user" : "dbyrne" }, 10 | { "_id" : 11, "description" : "When operating on a map, the conj function returns a new map with one or more key-value pairs \"added\".", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= {:a 1, :b 2, :c 3} (conj {:a 1} __ [:c 3]))" ], "times-solved" : 1061, "title" : "Maps: conj", "user" : "dbyrne" }, 11 | { "_id" : 12, "description" : "All Clojure collections support sequencing. You can operate on sequences with functions like first, second, and last.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ (first '(3 2 1)))", "(= __ (second [2 3 4]))", "(= __ (last (list 1 2 3)))" ], "times-solved" : 1051, "title" : "Intro to Sequences", "user" : "dbyrne" }, 12 | { "_id" : 13, "description" : "The rest function will return all the items of a sequence except the first.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ (rest [10 20 30 40]))" ], "times-solved" : 1045, "title" : "Sequences: rest", "user" : "dbyrne" }, 13 | { "_id" : 14, "description" : "Clojure has many different ways to create functions.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ ((fn add-five [x] (+ x 5)) 3))", "(= __ ((fn [x] (+ x 5)) 3))", "(= __ (#(+ % 5) 3))", "(= __ ((partial + 5) 3))" ], "times-solved" : 1038, "title" : "Intro to Functions", "user" : "dbyrne" }, 14 | { "_id" : 15, "description" : "Write a function which doubles a number.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= (__ 2) 4)", "(= (__ 3) 6)", "(= (__ 11) 22)", "(= (__ 7) 14)" ], "times-solved" : 1025, "title" : "Double Down", "user" : "dbyrne" }, 15 | { "_id" : 16, "description" : "Write a function which returns a personalized greeting.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= (__ \"Dave\") \"Hello, Dave!\")", "(= (__ \"Jenn\") \"Hello, Jenn!\")", "(= (__ \"Rhea\") \"Hello, Rhea!\")" ], "times-solved" : 986, "title" : "Hello World", "user" : "dbyrne" }, 16 | { "_id" : 17, "description" : "The map function takes two arguments: a function (f) and a sequence (s). Map returns a new sequence consisting of the result of applying f to each item of s. Do not confuse the map function with the map data structure.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ (map #(+ % 5) '(1 2 3)))" ], "times-solved" : 986, "title" : "Sequences: map", "user" : "dbyrne" }, 17 | { "_id" : 18, "description" : "The filter function takes two arguments: a predicate function (f) and a sequence (s). Filter returns a new sequence consisting of all the items of s for which (f item) returns true.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= __ (filter #(> % 5) '(3 4 5 6 7)))" ], "times-solved" : 978, "title" : "Sequences: filter", "user" : "dbyrne" }, 18 | { "_id" : 19, "description" : "Write a function which returns the last element in a sequence.", "difficulty" : "Easy", "restricted" : [ "last" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ [1 2 3 4 5]) 5)", "(= (__ '(5 4 3)) 3)", "(= (__ [\"b\" \"c\" \"d\"]) \"d\")" ], "times-solved" : 911, "title" : "Last Element", "user" : "dbyrne" }, 19 | { "_id" : 20, "description" : "Write a function which returns the second to last element from a sequence.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (__ (list 1 2 3 4 5)) 4)", "(= (__ [\"a\" \"b\" \"c\"]) \"b\")", "(= (__ [[1 2] [3 4]]) [1 2])" ], "times-solved" : 884, "title" : "Penultimate Element", "user" : "dbyrne" }, 20 | { "_id" : 21, "description" : "Write a function which returns the Nth element from a sequence.", "difficulty" : "Easy", "restricted" : [ "nth" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ '(4 5 6 7) 2) 6)", "(= (__ [:a :b :c] 0) :a)", "(= (__ [1 2 3 4] 1) 2)", "(= (__ '([1 2] [3 4] [5 6]) 2) [5 6])" ], "times-solved" : 782, "title" : "Nth Element", "user" : "dbyrne" }, 21 | { "_id" : 22, "description" : "Write a function which returns the total number of elements in a sequence.", "difficulty" : "Easy", "restricted" : [ "count" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ '(1 2 3 3 1)) 5)", "(= (__ \"Hello World\") 11)", "(= (__ [[1 2] [3 4] [5 6]]) 3)", "(= (__ '(13)) 1)", "(= (__ '(:a :b :c)) 3)" ], "times-solved" : 723, "title" : "Count a Sequence", "user" : "dbyrne" }, 22 | { "_id" : 23, "description" : "Write a function which reverses a sequence.", "difficulty" : "Easy", "restricted" : [ "reverse", "rseq" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ [1 2 3 4 5]) [5 4 3 2 1])", "(= (__ (sorted-set 5 7 2 7)) '(7 5 2))", "(= (__ [[1 2][3 4][5 6]]) [[5 6][3 4][1 2]])" ], "times-solved" : 650, "title" : "Reverse a Sequence", "user" : "dbyrne" }, 23 | { "_id" : 24, "description" : "Write a function which returns the sum of a sequence of numbers.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (__ [1 2 3]) 6)", "(= (__ (list 0 -2 5 5)) 8)", "(= (__ #{4 2 1}) 7)", "(= (__ '(0 0 -1)) -1)", "(= (__ '(1 10 3)) 14)" ], "times-solved" : 722, "title" : "Sum It All Up", "user" : "dbyrne" }, 24 | { "_id" : 25, "description" : "Write a function which returns only the odd numbers from a sequence.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (__ #{1 2 3 4 5}) '(1 3 5))", "(= (__ [4 2 1 6]) '(1))", "(= (__ [2 2 4 6]) '())", "(= (__ [1 1 1 3]) '(1 1 1 3))" ], "times-solved" : 697, "title" : "Find the odd numbers", "user" : "dbyrne" }, 25 | { "_id" : 26, "description" : "Write a function which returns the first X fibonacci numbers.", "difficulty" : "Easy", "tags" : [ "Fibonacci", "seqs" ], "tests" : [ "(= (__ 3) '(1 1 2))", "(= (__ 6) '(1 1 2 3 5 8))", "(= (__ 8) '(1 1 2 3 5 8 13 21))" ], "times-solved" : 507, "title" : "Fibonacci Sequence", "user" : "dbyrne" }, 26 | { "_id" : 27, "description" : "Write a function which returns true if the given sequence is a palindrome.

\n Hint: \"racecar\" does not equal '(\\r \\a \\c \\e \\c \\a \\r)", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(false? (__ '(1 2 3 4 5)))", "(true? (__ \"racecar\"))", "(true? (__ [:foo :bar :foo]))", "(true? (__ '(1 1 3 3 1 1)))", "(false? (__ '(:a :b :c)))" ], "times-solved" : 546, "title" : "Palindrome Detector", "user" : "dbyrne" }, 27 | { "_id" : 28, "description" : "Write a function which flattens a sequence.", "difficulty" : "Easy", "restricted" : [ "flatten" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6))", "(= (__ [\"a\" [\"b\"] \"c\"]) '(\"a\" \"b\" \"c\"))", "(= (__ '((((:a))))) '(:a))" ], "times-solved" : 356, "title" : "Flatten a Sequence", "user" : "dbyrne" }, 28 | { "_id" : 29, "description" : "Write a function which takes a string and returns a new string containing only the capital letters.", "difficulty" : "Easy", "tags" : [ "strings" ], "tests" : [ "(= (__ \"HeLlO, WoRlD!\") \"HLOWRD\")", "(empty? (__ \"nothing\"))", "(= (__ \"$#A(*&987Zf\") \"AZ\")" ], "times-solved" : 444, "title" : "Get the Caps", "user" : "dbyrne" }, 29 | { "_id" : 30, "description" : "Write a function which removes consecutive duplicates from a sequence.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (apply str (__ \"Leeeeeerrroyyy\")) \"Leroy\")", "(= (__ [1 1 2 3 3 2 2 3]) '(1 2 3 2 3))", "(= (__ [[1 2] [1 2] [3 4] [1 2]]) '([1 2] [3 4] [1 2]))" ], "times-solved" : 339, "title" : "Compress a Sequence", "user" : "dbyrne" }, 30 | { "_id" : 31, "description" : "Write a function which packs consecutive duplicates into sub-lists.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (__ [1 1 2 1 1 1 3 3]) '((1 1) (2) (1 1 1) (3 3)))", "(= (__ [:a :a :b :b :c]) '((:a :a) (:b :b) (:c)))", "(= (__ [[1 2] [1 2] [3 4]]) '(([1 2] [1 2]) ([3 4])))" ], "times-solved" : 279, "title" : "Pack a Sequence", "user" : "dbyrne" }, 31 | { "_id" : 32, "description" : "Write a function which duplicates each element of a sequence.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (__ [1 2 3]) '(1 1 2 2 3 3))", "(= (__ [:a :a :b :b]) '(:a :a :a :a :b :b :b :b))", "(= (__ [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4]))", "(= (__ [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4]))" ], "times-solved" : 368, "title" : "Duplicate a Sequence", "user" : "dbyrne" }, 32 | { "_id" : 33, "description" : "Write a function which replicates each element of a sequence a variable number of times.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (__ [1 2 3] 2) '(1 1 2 2 3 3))", "(= (__ [:a :b] 4) '(:a :a :a :a :b :b :b :b))", "(= (__ [4 5 6] 1) '(4 5 6))", "(= (__ [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4]))", "(= (__ [44 33] 2) [44 44 33 33])" ], "times-solved" : 331, "title" : "Replicate a Sequence", "user" : "dbyrne" }, 33 | { "_id" : 34, "description" : "Write a function which creates a list of all integers in a given range.", "difficulty" : "Easy", "restricted" : [ "range" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ 1 4) '(1 2 3))", "(= (__ -2 2) '(-2 -1 0 1))", "(= (__ 5 8) '(5 6 7))" ], "times-solved" : 358, "title" : "Implement range", "user" : "dbyrne" }, 34 | { "_id" : 35, "description" : "Clojure lets you give local names to values using the special let-form.", "difficulty" : "Elementary", "tags" : [ "syntax" ], "tests" : [ "(= __ (let [x 5] (+ 2 x)))", "(= __ (let [x 3, y 10] (- y x)))", "(= __ (let [x 21] (let [y 3] (/ x y))))" ], "times-solved" : 496, "title" : "Local bindings", "user" : "amalloy" }, 35 | { "_id" : 36, "description" : "Can you bind x, y, and z so that these are all true?", "difficulty" : "Elementary", "tags" : [ "math", "syntax" ], "tests" : [ "(= 10 (let __ (+ x y)))", "(= 4 (let __ (+ y z)))", "(= 1 (let __ z))" ], "times-solved" : 491, "title" : "Let it Be", "user" : "amalloy" }, 36 | { "_id" : 37, "description" : "Regex patterns are supported with a special reader macro.", "difficulty" : "Elementary", "tags" : [ "regex", "syntax" ], "tests" : [ "(= __ (apply str (re-seq #\"[A-Z]+\" \"bA1B3Ce \")))" ], "times-solved" : 468, "title" : "Regular Expressions", "user" : "dbyrne" }, 37 | { "_id" : 38, "description" : "Write a function which takes a variable number of parameters and returns the maximum value.", "difficulty" : "Easy", "restricted" : [ "max", "max-key" ], "tags" : [ "core-functions" ], "tests" : [ "(= (__ 1 8 3 4) 8)", "(= (__ 30 20) 30)", "(= (__ 45 67 11) 67)" ], "times-solved" : 412, "title" : "Maximum value", "user" : "dbyrne" }, 38 | { "_id" : 39, "description" : "Write a function which takes two sequences and returns the first item from each, then the second item from each, then the third, etc.", "difficulty" : "Easy", "restricted" : [ "interleave" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ [1 2 3] [:a :b :c]) '(1 :a 2 :b 3 :c))", "(= (__ [1 2] [3 4 5 6]) '(1 3 2 4))", "(= (__ [1 2 3 4] [5]) [1 5])", "(= (__ [30 20] [25 15]) [30 25 20 15])" ], "times-solved" : 321, "title" : "Interleave Two Seqs", "user" : "dbyrne" }, 39 | { "_id" : 40, "description" : "Write a function which separates the items of a sequence by an arbitrary value.", "difficulty" : "Easy", "restricted" : [ "interpose" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ 0 [1 2 3]) [1 0 2 0 3])", "(= (apply str (__ \", \" [\"one\" \"two\" \"three\"])) \"one, two, three\")", "(= (__ :z [:a :b :c :d]) [:a :z :b :z :c :z :d])" ], "times-solved" : 306, "title" : "Interpose a Seq", "user" : "dbyrne" }, 40 | { "_id" : 41, "description" : "Write a function which drops every Nth item from a sequence.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])", "(= (__ [:a :b :c :d :e :f] 2) [:a :c :e])", "(= (__ [1 2 3 4 5 6] 4) [1 2 3 5 6])" ], "times-solved" : 276, "title" : "Drop Every Nth Item", "user" : "dbyrne" }, 41 | { "_id" : 42, "description" : "Write a function which calculates factorials.", "difficulty" : "Easy", "tags" : [ "math" ], "tests" : [ "(= (__ 1) 1)", "(= (__ 3) 6)", "(= (__ 5) 120)", "(= (__ 8) 40320)" ], "times-solved" : 335, "title" : "Factorial Fun", "user" : "amalloy" }, 42 | { "_id" : 43, "description" : "Write a function which reverses the interleave process into x number of subsequences.", "difficulty" : "Medium", "tags" : [ "seqs" ], "tests" : [ "(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6)))", "(= (__ (range 9) 3) '((0 3 6) (1 4 7) (2 5 8)))", "(= (__ (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9)))" ], "times-solved" : 216, "title" : "Reverse Interleave", "user" : "amalloy" }, 43 | { "_id" : 44, "description" : "Write a function which can rotate a sequence in either direction.", "difficulty" : "Medium", "tags" : [ "seqs" ], "tests" : [ "(= (__ 2 [1 2 3 4 5]) '(3 4 5 1 2))", "(= (__ -2 [1 2 3 4 5]) '(4 5 1 2 3))", "(= (__ 6 [1 2 3 4 5]) '(2 3 4 5 1))", "(= (__ 1 '(:a :b :c)) '(:b :c :a))", "(= (__ -4 '(:a :b :c)) '(:c :a :b))" ], "times-solved" : 233, "title" : "Rotate Sequence", "user" : "dbyrne" }, 44 | { "_id" : 45, "description" : "The iterate function can be used to produce an infinite lazy sequence.", "difficulty" : "Easy", "tags" : [ "seqs" ], "tests" : [ "(= __ (take 5 (iterate #(+ 3 %) 1)))" ], "times-solved" : 342, "title" : "Intro to Iterate", "user" : "dbyrne" }, 45 | { "_id" : 46, "description" : "Write a higher-order function which flips the order of the arguments of an input function.", "difficulty" : "Medium", "tags" : [ "higher-order-functions" ], "tests" : [ "(= 3 ((__ nth) 2 [1 2 3 4 5]))", "(= true ((__ >) 7 8))", "(= 4 ((__ quot) 2 8))", "(= [1 2 3] ((__ take) [1 2 3 4 5] 3))" ], "times-solved" : 277, "title" : "Flipping out", "user" : "dbyrne" }, 46 | { "_id" : 47, "description" : "The contains? function checks if a KEY is present in a given collection. This often leads beginner clojurians to use it incorrectly with numerically indexed collections like vectors and lists.", "difficulty" : "Easy", "tags" : null, "tests" : [ "(contains? #{4 5 6} __)", "(contains? [1 1 1 1 1] __)", "(contains? {4 :a 2 :b} __)", "(not (contains? [1 2 4] __))" ], "times-solved" : 349, "title" : "Contain Yourself", "user" : "dbyrne", "restricted" : null }, 47 | { "_id" : 48, "description" : "The some function takes a predicate function and a collection. It returns the first logical true value of (predicate x) where x is an item in the collection.", "difficulty" : "Easy", "tags" : [], "tests" : [ "(= __ (some #{2 7 6} [5 6 7 8]))", "(= __ (some #(when (even? %) %) [5 6 7 8]))" ], "times-solved" : 384, "title" : "Intro to some", "user" : "dbyrne" }, 48 | { "_id" : 49, "description" : "Write a function which will split a sequence into two parts.", "difficulty" : "Easy", "restricted" : [ "split-at" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ 3 [1 2 3 4 5 6]) [[1 2 3] [4 5 6]])", "(= (__ 1 [:a :b :c :d]) [[:a] [:b :c :d]])", "(= (__ 2 [[1 2] [3 4] [5 6]]) [[[1 2] [3 4]] [[5 6]]])" ], "times-solved" : 266, "title" : "Split a sequence", "user" : "dbyrne" }, 49 | { "_id" : 50, "description" : "Write a function which takes a sequence consisting of items with different types and splits them up into a set of homogeneous sub-sequences. The internal order of each sub-sequence should be maintained, but the sub-sequences themselves can be returned in any order (this is why 'set' is used in the test cases).", "difficulty" : "Medium", "tags" : [ "seqs" ], "tests" : [ "(= (set (__ [1 :a 2 :b 3 :c])) #{[1 2 3] [:a :b :c]})", "(= (set (__ [:a \"foo\" \"bar\" :b])) #{[:a :b] [\"foo\" \"bar\"]})", "(= (set (__ [[1 2] :a [3 4] 5 6 :b])) #{[[1 2] [3 4]] [:a :b] [5 6]})" ], "times-solved" : 207, "title" : "Split by Type", "user" : "dbyrne" }, 50 | { "_id" : 51, "description" : "Here is an example of some more sophisticated destructuring.", "difficulty" : "Easy", "tags" : [ "destructuring" ], "tests" : [ "(= [1 2 [3 4 5] [1 2 3 4 5]] (let [[a b & c :as d] __] [a b c d]))" ], "times-solved" : 279, "title" : "Advanced Destructuring", "user" : "dbyrne" }, 51 | { "_id" : 52, "description" : "Let bindings and function parameter lists support destructuring.", "difficulty" : "Elementary", "tags" : [ "destructuring" ], "tests" : [ "(= [2 4] (let [[a b c d e] [0 1 2 3 4]] __))" ], "times-solved" : 301, "title" : "Intro to Destructuring", "user" : "amalloy", "restricted" : null }, 52 | { "_id" : 53, "description" : "Given a vector of integers, find the longest consecutive sub-sequence of increasing numbers. If two sub-sequences have the same length, use the one that occurs first. An increasing sub-sequence must have a length of 2 or greater to qualify.", "difficulty" : "Hard", "tags" : [ "seqs" ], "tests" : [ "(= (__ [1 0 1 2 3 0 4 5]) [0 1 2 3])", "(= (__ [5 6 1 3 2 7]) [5 6])", "(= (__ [2 3 3 4 5]) [3 4 5])", "(= (__ [7 6 5 4]) [])" ], "times-solved" : 156, "title" : "Longest Increasing Sub-Seq", "user" : "dbyrne" }, 53 | { "_id" : 54, "description" : "Write a function which returns a sequence of lists of x items each. Lists of less than x items should not be returned.", "difficulty" : "Medium", "restricted" : [ "partition", "partition-all" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ 3 (range 9)) '((0 1 2) (3 4 5) (6 7 8)))", "(= (__ 2 (range 8)) '((0 1) (2 3) (4 5) (6 7)))", "(= (__ 3 (range 8)) '((0 1 2) (3 4 5)))" ], "times-solved" : 170, "title" : "Partition a Sequence", "user" : "dbyrne" }, 54 | { "_id" : 55, "description" : "Write a function which returns a map containing the number of occurences of each distinct item in a sequence.", "difficulty" : "Medium", "restricted" : [ "frequencies" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ [1 1 2 3 2 1 1]) {1 4, 2 2, 3 1})", "(= (__ [:b :a :b :a :b]) {:a 2, :b 3})", "(= (__ '([1 2] [1 3] [1 3])) {[1 2] 1, [1 3] 2})" ], "times-solved" : 193, "title" : "Count Occurrences", "user" : "dbyrne" }, 55 | { "_id" : 56, "description" : "Write a function which removes the duplicates from a sequence. Order of the items must be maintained.", "difficulty" : "Medium", "restricted" : [ "distinct" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (__ [1 2 1 3 1 2 4]) [1 2 3 4])", "(= (__ [:a :a :b :b :c :c]) [:a :b :c])", "(= (__ '([2 4] [1 2] [1 3] [1 3])) '([2 4] [1 2] [1 3]))", "(= (__ (range 50)) (range 50))" ], "times-solved" : 170, "title" : "Find Distinct Items", "user" : "dbyrne" }, 56 | { "_id" : 57, "description" : "A recursive function is a function which calls itself. This is one of the fundamental techniques used in functional programming.", "difficulty" : "Elementary", "tags" : [ "recursion" ], "tests" : [ "(= __ ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5))" ], "times-solved" : 326, "title" : "Simple Recursion", "user" : "dbyrne" }, 57 | { "_id" : 58, "description" : "Write a function which allows you to create function compositions. The parameter list should take a variable number of functions, and create a function that applies them from right-to-left.", "difficulty" : "Medium", "restricted" : [ "comp" ], "tags" : [ "higher-order-functions", "core-functions" ], "tests" : [ "(= [3 2 1] ((__ rest reverse) [1 2 3 4]))", "(= 5 ((__ (partial + 3) second) [1 2 3 4]))", "(= true ((__ zero? #(mod % 8) +) 3 5 7 9))", "(= \"HELLO\" ((__ #(.toUpperCase %) #(apply str %) take) 5 \"hello world\"))" ], "times-solved" : 167, "title" : "Function Composition", "user" : "dbyrne" }, 58 | { "_id" : 59, "description" : "Take a set of functions and return a new function that takes a variable number of arguments and returns a sequence containing the result of applying each function left-to-right to the argument list.", "difficulty" : "Medium", "restricted" : [ "juxt" ], "tags" : [ "higher-order-functions", "core-functions" ], "tests" : [ "(= [21 6 1] ((__ + max min) 2 3 5 1 6 4))", "(= [\"HELLO\" 5] ((__ #(.toUpperCase %) count) \"hello\"))", "(= [2 6 4] ((__ :a :c :b) {:a 2, :b 4, :c 6, :d 8 :e 10}))" ], "times-solved" : 169, "title" : "Juxtaposition", "user" : "dbyrne" }, 59 | { "_id" : 60, "description" : "Write a function which behaves like reduce, but returns each intermediate value of the reduction. Your function must accept either two or three arguments, and the return sequence must be lazy.", "difficulty" : "Medium", "restricted" : [ "reductions" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (take 5 (__ + (range))) [0 1 3 6 10])", "(= (__ conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]])", "(= (last (__ * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120)" ], "times-solved" : 119, "title" : "Sequence Reductions", "user" : "dbyrne" }, 60 | { "_id" : 61, "description" : "Write a function which takes a vector of keys and a vector of values and constructs a map from them.", "difficulty" : "Easy", "restricted" : [ "zipmap" ], "tags" : [ "core-functions" ], "tests" : [ "(= (__ [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})", "(= (__ [1 2 3 4] [\"one\" \"two\" \"three\"]) {1 \"one\", 2 \"two\", 3 \"three\"})", "(= (__ [:foo :bar] [\"foo\" \"bar\" \"baz\"]) {:foo \"foo\", :bar \"bar\"})" ], "times-solved" : 197, "title" : "Map Construction", "user" : "dbyrne" }, 61 | { "_id" : 62, "description" : "Given a side-effect free function f and an initial value x write a function which returns an infinite lazy sequence of x, (f x), (f (f x)), (f (f (f x))), etc.", "difficulty" : "Easy", "restricted" : [ "iterate" ], "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= (take 5 (__ #(* 2 %) 1)) [1 2 4 8 16])", "(= (take 100 (__ inc 0)) (take 100 (range)))", "(= (take 9 (__ #(inc (mod % 3)) 1)) (take 9 (cycle [1 2 3])))" ], "times-solved" : 163, "title" : "Re-implement Iterate", "user" : "amalloy" }, 62 | { "_id" : 63, "description" : "Given a function f and a sequence s, write a function which returns a map. The keys should be the values of f applied to each item in s. The value at each key should be a vector of corresponding items in the order they appear in s.", "difficulty" : "Easy", "restricted" : [ "group-by" ], "tags" : [ "core-functions" ], "tests" : [ "(= (__ #(> % 5) [1 3 6 8]) {false [1 3], true [6 8]})", "(= (__ #(apply / %) [[1 2] [2 4] [4 6] [3 6]])\n {1/2 [[1 2] [2 4] [3 6]], 2/3 [[4 6]]})", "(= (__ count [[1] [1 2] [3] [1 2 3] [2 3]])\n {1 [[1] [3]], 2 [[1 2] [2 3]], 3 [[1 2 3]]})" ], "times-solved" : 154, "title" : "Group a Sequence", "user" : "dbyrne" }, 63 | { "_id" : 64, "description" : "Reduce takes a 2 argument function and an optional starting value. It then applies the function to the first 2 items in the sequence (or the starting value and the first element of the sequence). In the next iteration the function will be called on the previous return value and the next item from the sequence, thus reducing the entire collection to one value. Don't worry, it's not as complicated as it sounds.", "difficulty" : "Elementary", "tags" : [ "seqs" ], "tests" : [ "(= 15 (reduce __ [1 2 3 4 5]))", "(= 0 (reduce __ []))", "(= 6 (reduce __ 1 [2 3]))" ], "times-solved" : 339, "title" : "Intro to Reduce", "user" : "citizen428" }, 64 | { "_id" : 65, "description" : "Clojure has many sequence types, which act in subtly different ways. The core functions typically convert them into a uniform \"sequence\" type and work with them that way, but it can be important to understand the behavioral and performance differences so that you know which kind is appropriate for your application.

Write a function which takes a collection and returns one of :map, :set, :list, or :vector - describing the type of collection it was given.
You won't be allowed to inspect their class or use the built-in predicates like list? - the point is to poke at them and understand their behavior.", "difficulty" : "Medium", "restricted" : [ "class", "type", "Class", "vector?", "sequential?", "list?", "seq?", "map?", "set?", "instance?", "getClass" ], "tags" : [ "seqs", "testing" ], "tests" : [ "(= :map (__ {:a 1, :b 2}))", "(= :list (__ (range (rand-int 20))))", "(= :vector (__ [1 2 3 4 5 6]))", "(= :set (__ #{10 (rand-int 5)}))", "(= [:map :set :vector :list] (map __ [{} #{} [] ()]))" ], "times-solved" : 107, "title" : "Black Box Testing", "user" : "amalloy" }, 65 | { "_id" : 66, "description" : "Given two integers, write a function which\nreturns the greatest common divisor.", "difficulty" : "Easy", "tags" : [], "tests" : [ "(= (__ 2 4) 2)", "(= (__ 10 5) 5)", "(= (__ 5 7) 1)", "(= (__ 1023 858) 33)" ], "times-solved" : 187, "title" : "Greatest Common Divisor", "user" : "dbyrne" }, 66 | { "_id" : 67, "description" : "Write a function which returns the first x\nnumber of prime numbers.", "difficulty" : "Medium", "tags" : [ "primes" ], "tests" : [ "(= (__ 2) [2 3])", "(= (__ 5) [2 3 5 7 11])", "(= (last (__ 100)) 541)" ], "times-solved" : 122, "title" : "Prime Numbers", "user" : "dbyrne" }, 67 | { "_id" : 68, "description" : "Clojure only has one non-stack-consuming looping construct: recur. Either a function or a loop can be used as the recursion point. Either way, recur rebinds the bindings of the recursion point to the values it is passed. Recur must be called from the tail-position, and calling it elsewhere will result in an error.", "difficulty" : "Elementary", "tags" : [ "recursion" ], "tests" : [ "(= __\n (loop [x 5\n result []]\n (if (> x 0)\n (recur (dec x) (conj result (+ 2 x)))\n result)))" ], "times-solved" : 224, "title" : "Recurring Theme", "user" : "dbyrne" }, 68 | { "_id" : 69, "description" : "Write a function which takes a function f and a variable number of maps. Your function should return a map that consists of the rest of the maps conj-ed onto the first. If a key occurs in more than one map, the mapping(s) from the latter (left-to-right) should be combined with the mapping in the result by calling (f val-in-result val-in-latter)", "difficulty" : "Medium", "restricted" : [ "merge-with" ], "tags" : [ "core-functions" ], "tests" : [ "(= (__ * {:a 2, :b 3, :c 4} {:a 2} {:b 2} {:c 5})\n {:a 4, :b 6, :c 20})", "(= (__ - {1 10, 2 20} {1 3, 2 10, 3 15})\n {1 7, 2 10, 3 15})", "(= (__ concat {:a [3], :b [6]} {:a [4 5], :c [8 9]} {:b [7]})\n {:a [3 4 5], :b [6 7], :c [8 9]})" ], "times-solved" : 109, "title" : "Merge with a Function", "user" : "dbyrne" }, 69 | { "_id" : 70, "description" : "Write a function that splits a sentence up into a sorted list of words. Capitalization should not affect sort order and punctuation should be ignored.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "sorting" ], "tests" : [ "(= (__ \"Have a nice day.\")\r\n [\"a\" \"day\" \"Have\" \"nice\"])", "(= (__ \"Clojure is a fun language!\")\r\n [\"a\" \"Clojure\" \"fun\" \"is\" \"language\"])", "(= (__ \"Fools fall for foolish follies.\")\r\n [\"fall\" \"follies\" \"foolish\" \"Fools\" \"for\"])" ], "times-solved" : 138, "title" : "Word Sorting", "user" : "fotland" }, 70 | { "_id" : 71, "description" : "The -> macro threads an expression x through a variable number of forms. First, x is inserted as the second item in the first form, making a list of it if it is not a list already. Then the first form is inserted as the second item in the second form, making a list of that form if necessary. This process continues for all the forms. Using -> can sometimes make your code more readable.", "difficulty" : "Elementary", "restricted" : null, "tags" : null, "tests" : [ "(= (__ (sort (rest (reverse [2 5 4 1 3 6]))))\r\n (-> [2 5 4 1 3 6] (reverse) (rest) (sort) (__))\r\n 5)" ], "times-solved" : 246, "title" : "Rearranging Code: ->", "user" : "amalloy" }, 71 | { "_id" : 72, "description" : "The ->> macro threads an expression x through a variable number of forms. First, x is inserted as the last item in the first form, making a list of it if it is not a list already. Then the first form is inserted as the last item in the second form, making a list of that form if necessary. This process continues for all the forms. Using ->> can sometimes make your code more readable.", "difficulty" : "Elementary", "tags" : [], "tests" : [ "(= (__ (map inc (take 3 (drop 2 [2 5 4 1 3 6]))))\n (->> [2 5 4 1 3 6] (drop 2) (take 3) (map inc) (__))\n 11)" ], "times-solved" : 230, "title" : "Rearranging Code: ->>", "user" : "amalloy" }, 72 | { "_id" : 73, "description" : "A tic-tac-toe board is represented by a two dimensional vector. X is represented by :x, O is represented by :o, and empty is represented by :e. A player wins by placing three Xs or three Os in a horizontal, vertical, or diagonal row. Write a function which analyzes a tic-tac-toe board and returns :x if X has won, :o if O has won, and nil if neither player has won.", "difficulty" : "Hard", "tags" : [ "game" ], "tests" : [ "(= nil (__ [[:e :e :e]\n [:e :e :e]\n [:e :e :e]]))", "(= :x (__ [[:x :e :o]\n [:x :e :e]\n [:x :e :o]]))", "(= :o (__ [[:e :x :e]\n [:o :o :o]\n [:x :e :x]]))", "(= nil (__ [[:x :e :o]\n [:x :x :e]\n [:o :x :o]]))", "(= :x (__ [[:x :e :e]\n [:o :x :e]\n [:o :e :x]]))", "(= :o (__ [[:x :e :o]\n [:x :o :e]\n [:o :e :x]]))", "(= nil (__ [[:x :o :x]\n [:x :o :x]\n [:o :x :o]]))" ], "times-solved" : 100, "title" : "Analyze a Tic-Tac-Toe Board", "user" : "fotland" }, 73 | { "_id" : 74, "description" : "Given a string of comma separated integers, write a function which returns a new comma separated string that only contains the numbers which are perfect squares.", "difficulty" : "Medium", "tags" : [], "tests" : [ "(= (__ \"4,5,6,7,8,9\") \"4,9\")", "(= (__ \"15,16,25,36,37\") \"16,25,36\")" ], "times-solved" : 126, "title" : "Filter Perfect Squares", "user" : "dbyrne" }, 74 | { "_id" : 75, "description" : "Two numbers are coprime if their greatest common divisor equals 1. Euler's totient function f(x) is defined as the number of positive integers less than x which are coprime to x. The special case f(1) equals 1. Write a function which calculates Euler's totient function.", "difficulty" : "Medium", "tags" : [], "tests" : [ "(= (__ 1) 1)", "(= (__ 10) (count '(1 3 7 9)) 4)", "(= (__ 40) 16)", "(= (__ 99) 60)" ], "times-solved" : 92, "title" : "Euler's Totient Function", "user" : "dbyrne" }, 75 | { "_id" : 76, "description" : "The trampoline function takes a function f and a variable number of parameters. Trampoline calls f with any parameters that were supplied. If f returns a function, trampoline calls that function with no arguments. This is repeated, until the return value is not a function, and then trampoline returns that non-function value. This is useful for implementing mutually recursive algorithms in a way that won't consume the stack.", "difficulty" : "Medium", "tags" : [ "recursion" ], "tests" : [ "(= __\n (letfn\n [(foo [x y] #(bar (conj x y) y))\n (bar [x y] (if (> (last x) 10)\n x\n #(foo x (+ 2 y))))]\n (trampoline foo [] 1)))" ], "times-solved" : 117, "title" : "Intro to Trampoline", "user" : "dbyrne" }, 76 | { "_id" : 77, "description" : "Write a function which finds all the anagrams in a vector of words. A word x is an anagram of word y if all the letters in x can be rearranged in a different order to form y. Your function should return a set of sets, where each sub-set is a group of words which are anagrams of each other. Each sub-set should have at least two words. Words without any anagrams should not be included in the result.", "difficulty" : "Medium", "tags" : [], "tests" : [ "(= (__ [\"meat\" \"mat\" \"team\" \"mate\" \"eat\"])\n #{#{\"meat\" \"team\" \"mate\"}})", "(= (__ [\"veer\" \"lake\" \"item\" \"kale\" \"mite\" \"ever\"])\n #{#{\"veer\" \"ever\"} #{\"lake\" \"kale\"} #{\"mite\" \"item\"}})" ], "times-solved" : 96, "title" : "Anagram Finder", "user" : "dbyrne" }, 77 | { "_id" : 78, "description" : "Reimplement the function described in \"Intro to Trampoline\".", "difficulty" : "Medium", "restricted" : [ "trampoline" ], "tags" : [ "core-functions" ], "tests" : [ "(= (letfn [(triple [x] #(sub-two (* 3 x)))\n (sub-two [x] #(stop?(- x 2)))\n (stop? [x] (if (> x 50) x #(triple x)))]\n (__ triple 2))\n 82)", "(= (letfn [(my-even? [x] (if (zero? x) true #(my-odd? (dec x))))\n (my-odd? [x] (if (zero? x) false #(my-even? (dec x))))]\n (map (partial __ my-even?) (range 6)))\n [true false true false true false])" ], "times-solved" : 86, "title" : "Reimplement Trampoline", "user" : "dbyrne" }, 78 | { "_id" : 79, "description" : "Write a function which calculates the sum of the minimal path through a triangle. The triangle is represented as a collection of vectors. The path should start at the top of the triangle and move to an adjacent number on the next row until the bottom of the triangle is reached.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "graph-theory" ], "tests" : [ "(= 7 (__ '([1]\r\n [2 4]\r\n [5 1 4]\r\n [2 3 4 5]))) ; 1->2->1->3", "(= 20 (__ '([3]\r\n [2 4]\r\n [1 9 3]\r\n [9 9 2 4]\r\n [4 6 6 7 8]\r\n [5 7 3 5 1 4]))) ; 3->4->3->2->7->1" ], "times-solved" : 72, "title" : "Triangle Minimal Path", "user" : "dbyrne" }, 79 | { "_id" : 80, "description" : "A number is \"perfect\" if the sum of its divisors equal the number itself. 6 is a perfect number because 1+2+3=6. Write a function which returns true for perfect numbers and false otherwise.", "difficulty" : "Medium", "tags" : [], "tests" : [ "(= (__ 6) true)", "(= (__ 7) false)", "(= (__ 496) true)", "(= (__ 500) false)", "(= (__ 8128) true)" ], "times-solved" : 118, "title" : "Perfect Numbers", "user" : "dbyrne" }, 80 | { "_id" : 81, "description" : "Write a function which returns the intersection of two sets. The intersection is the sub-set of items that each set has in common.", "difficulty" : "Easy", "restricted" : [ "intersection" ], "tags" : [ "set-theory" ], "tests" : [ "(= (__ #{0 1 2 3} #{2 3 4 5}) #{2 3})", "(= (__ #{0 1 2} #{3 4 5}) #{})", "(= (__ #{:a :b :c :d} #{:c :e :a :f :d}) #{:a :c :d})" ], "times-solved" : 150, "title" : "Set Intersection", "user" : "dbyrne" }, 81 | { "_id" : 82, "description" : "A word chain consists of a set of words ordered so that each word differs by only one letter from the words directly before and after it. The one letter difference can be either an insertion, a deletion, or a substitution. Here is an example word chain:

cat -> cot -> coat -> oat -> hat -> hot -> hog -> dog

Write a function which takes a sequence of words, and returns true if they can be arranged into one continous word chain, and false if they cannot.", "difficulty" : "Hard", "tags" : [ "seqs" ], "tests" : [ "(= true (__ #{\"hat\" \"coat\" \"dog\" \"cat\" \"oat\" \"cot\" \"hot\" \"hog\"}))", "(= false (__ #{\"cot\" \"hot\" \"bat\" \"fat\"}))", "(= false (__ #{\"to\" \"top\" \"stop\" \"tops\" \"toss\"}))", "(= true (__ #{\"spout\" \"do\" \"pot\" \"pout\" \"spot\" \"dot\"}))", "(= true (__ #{\"share\" \"hares\" \"shares\" \"hare\" \"are\"}))", "(= false (__ #{\"share\" \"hares\" \"hare\" \"are\"}))" ], "times-solved" : 48, "title" : "Word Chains", "user" : "dbyrne" }, 82 | { "_id" : 83, "description" : "Write a function which takes a variable number of booleans. Your function should return true if some of the parameters are true, but not all of the parameters are true. Otherwise your function should return false.", "difficulty" : "Easy", "tags" : [], "tests" : [ "(= false (__ false false))", "(= true (__ true false))", "(= false (__ true))", "(= true (__ false true false))", "(= false (__ true true true))", "(= true (__ true true true false))" ], "times-solved" : 184, "title" : "A Half-Truth", "user" : "cmeier" }, 83 | { "_id" : 84, "description" : "Write a function which generates the transitive closure of a binary relation. The relation will be represented as a set of 2 item vectors.", "difficulty" : "Hard", "tags" : [ "set-theory" ], "tests" : [ "(let [divides #{[8 4] [9 3] [4 2] [27 9]}]\n (= (__ divides) #{[4 2] [8 4] [8 2] [9 3] [27 9] [27 3]}))", "(let [more-legs\n #{[\"cat\" \"man\"] [\"man\" \"snake\"] [\"spider\" \"cat\"]}]\n (= (__ more-legs)\n #{[\"cat\" \"man\"] [\"cat\" \"snake\"] [\"man\" \"snake\"]\n [\"spider\" \"cat\"] [\"spider\" \"man\"] [\"spider\" \"snake\"]}))", "(let [progeny\n #{[\"father\" \"son\"] [\"uncle\" \"cousin\"] [\"son\" \"grandson\"]}]\n (= (__ progeny)\n #{[\"father\" \"son\"] [\"father\" \"grandson\"]\n [\"uncle\" \"cousin\"] [\"son\" \"grandson\"]}))" ], "times-solved" : 57, "title" : "Transitive Closure", "user" : "dbyrne" }, 84 | { "_id" : 85, "description" : "Write a function which generates the power set of a given set. The power set of a set x is the set of all subsets of x, including the empty set and x itself.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "set-theory" ], "tests" : [ "(= (__ #{1 :a}) #{#{1 :a} #{:a} #{} #{1}})", "(= (__ #{}) #{#{}})", "(= (__ #{1 2 3})\r\n #{#{} #{1} #{2} #{3} #{1 2} #{1 3} #{2 3} #{1 2 3}})", "(= (count (__ (into #{} (range 10)))) 1024)" ], "times-solved" : 64, "title" : "Power Set", "user" : "peteris" }, 85 | { "_id" : 86, "description" : "Happy numbers are positive integers that follow a particular formula: take each individual digit, square it, and then sum the squares to get a new number. Repeat with the new number and eventually, you might get to a number whose squared sum is 1. This is a happy number. An unhappy number (or sad number) is one that loops endlessly. Write a function that determines if a number is happy or not.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "math" ], "tests" : [ "(= (__ 7) true)", "(= (__ 986543210) true)", "(= (__ 2) false)", "(= (__ 3) false)" ], "times-solved" : 94, "title" : "Happy numbers", "user" : "daviddavis" }, 86 | { "_id" : 88, "description" : "Write a function which returns the symmetric difference of two sets. The symmetric difference is the set of items belonging to one but not both of the two sets.", "difficulty" : "Easy", "tags" : [ "set-theory" ], "tests" : [ "(= (__ #{1 2 3 4 5 6} #{1 3 5 7}) #{2 4 6 7})", "(= (__ #{:a :b :c} #{}) #{:a :b :c})", "(= (__ #{} #{4 5 6}) #{4 5 6})", "(= (__ #{[1 2] [2 3]} #{[2 3] [3 4]}) #{[1 2] [3 4]})" ], "times-solved" : 107, "title" : "Symmetric Difference", "user" : "dbyrne" }, 87 | { "_id" : 89, "description" : "Starting with a graph you must write a function that returns true if it is possible to make a tour of the graph in which every edge is visited exactly once.

The graph is represented by a vector of tuples, where each tuple represents a single edge.

The rules are:

- You can start at any node.
- You must visit each edge exactly once.
- All edges are undirected.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "graph-theory" ], "tests" : [ "(= true (__ [[:a :b]]))", "(= false (__ [[:a :a] [:b :b]]))", "(= false (__ [[:a :b] [:a :b] [:a :c] [:c :a]\r\n [:a :d] [:b :d] [:c :d]]))", "(= true (__ [[1 2] [2 3] [3 4] [4 1]]))", "(= true (__ [[:a :b] [:a :c] [:c :b] [:a :e]\r\n [:b :e] [:a :d] [:b :d] [:c :e]\r\n [:d :e] [:c :f] [:d :f]]))", "(= false (__ [[1 2] [2 3] [2 4] [2 5]]))" ], "times-solved" : 31, "title" : "Graph Tour", "user" : "lucas1000001" }, 88 | { "_id" : 90, "description" : "Write a function which calculates the Cartesian product of two sets.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "set-theory" ], "tests" : [ "(= (__ #{\"ace\" \"king\" \"queen\"} #{\"♠\" \"♥\" \"♦\" \"♣\"})\r\n #{[\"ace\" \"♠\"] [\"ace\" \"♥\"] [\"ace\" \"♦\"] [\"ace\" \"♣\"]\r\n [\"king\" \"♠\"] [\"king\" \"♥\"] [\"king\" \"♦\"] [\"king\" \"♣\"]\r\n [\"queen\" \"♠\"] [\"queen\" \"♥\"] [\"queen\" \"♦\"] [\"queen\" \"♣\"]})", "(= (__ #{1 2 3} #{4 5})\r\n #{[1 4] [2 4] [3 4] [1 5] [2 5] [3 5]})", "(= 300 (count (__ (into #{} (range 10))\r\n (into #{} (range 30)))))" ], "times-solved" : 120, "title" : "Cartesian Product", "user" : "dbyrne" }, 89 | { "_id" : 91, "description" : "Given a graph, determine whether the graph is connected. A connected graph is such that a path exists between any two given nodes.

-Your function must return true if the graph is connected and false otherwise.

-You will be given a set of tuples representing the edges of a graph. Each member of a tuple being a vertex/node in the graph.

-Each edge is undirected (can be traversed either direction).\r\n", "difficulty" : "Hard", "restricted" : null, "tags" : [ "graph-theory" ], "tests" : [ "(= true (__ #{[:a :a]}))", "(= true (__ #{[:a :b]}))", "(= false (__ #{[1 2] [2 3] [3 1]\r\n [4 5] [5 6] [6 4]}))", "(= true (__ #{[1 2] [2 3] [3 1]\r\n [4 5] [5 6] [6 4] [3 4]}))", "(= false (__ #{[:a :b] [:b :c] [:c :d]\r\n [:x :y] [:d :a] [:b :e]}))", "(= true (__ #{[:a :b] [:b :c] [:c :d]\r\n [:x :y] [:d :a] [:b :e] [:x :a]}))" ], "times-solved" : 41, "title" : "Graph Connectivity", "user" : "lucas1000001" }, 90 | { "_id" : 92, "description" : "Roman numerals are easy to recognize, but not everyone knows all the rules necessary to work with them. Write a function to parse a Roman-numeral string and return the number it represents.\r\n

\r\nYou can assume that the input will be well-formed, in upper-case, and follow the subtractive principle. You don't need to handle any numbers greater than MMMCMXCIX (3999), the largest number representable with ordinary letters.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "strings", "math" ], "tests" : [ "(= 14 (__ \"XIV\"))", "(= 827 (__ \"DCCCXXVII\"))", "(= 3999 (__ \"MMMCMXCIX\"))", "(= 48 (__ \"XLVIII\"))\r\n" ], "times-solved" : 58, "title" : "Read Roman numerals", "user" : "amalloy" }, 91 | { "_id" : 93, "description" : "Write a function which flattens any nested combination of sequential things (lists, vectors, etc.), but maintains the lowest level sequential items. The result should be a sequence of sequences with only one level of nesting.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs" ], "tests" : [ "(= (__ [[\"Do\"] [\"Nothing\"]])\r\n [[\"Do\"] [\"Nothing\"]])", "(= (__ [[[[:a :b]]] [[:c :d]] [:e :f]])\r\n [[:a :b] [:c :d] [:e :f]])", "(= (__ '((1 2)((3 4)((((5 6)))))))\r\n '((1 2)(3 4)(5 6)))" ], "times-solved" : 54, "title" : "Partially Flatten a Sequence", "user" : "dbyrne" }, 92 | { "_id" : 94, "description" : "The game of life is a cellular automaton devised by mathematician John Conway.

The 'board' consists of both live (#) and dead ( ) cells. Each cell interacts with its eight neighbours (horizontal, vertical, diagonal), and its next state is dependent on the following rules:

1) Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2) Any live cell with two or three live neighbours lives on to the next generation.
3) Any live cell with more than three live neighbours dies, as if by overcrowding.
4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Write a function that accepts a board, and returns a board representing the next generation of cells.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "game" ], "tests" : [ "(= (__ [\" \" \r\n \" ## \"\r\n \" ## \"\r\n \" ## \"\r\n \" ## \"\r\n \" \"])\r\n [\" \" \r\n \" ## \"\r\n \" # \"\r\n \" # \"\r\n \" ## \"\r\n \" \"])", "(= (__ [\" \"\r\n \" \"\r\n \" ### \"\r\n \" \"\r\n \" \"])\r\n [\" \"\r\n \" # \"\r\n \" # \"\r\n \" # \"\r\n \" \"])", "(= (__ [\" \"\r\n \" \"\r\n \" ### \"\r\n \" ### \"\r\n \" \"\r\n \" \"])\r\n [\" \"\r\n \" # \"\r\n \" # # \"\r\n \" # # \"\r\n \" # \"\r\n \" \"])" ], "times-solved" : 34, "title" : "Game of Life", "user" : "lucas1000001" }, 93 | { "_id" : 95, "description" : "Write a predicate which checks whether or not a given sequence represents a binary tree. Each node in the tree must have a value, a left child, and a right child.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "trees" ], "tests" : [ "(= (__ '(:a (:b nil nil) nil))\r\n true)", "(= (__ '(:a (:b nil nil)))\r\n false)", "(= (__ [1 nil [2 [3 nil nil] [4 nil nil]]])\r\n true)", "(= (__ [1 [2 nil nil] [3 nil nil] [4 nil nil]])\r\n false)", "(= (__ [1 [2 [3 [4 nil nil] nil] nil] nil])\r\n true)", "(= (__ [1 [2 [3 [4 false nil] nil] nil] nil])\r\n false)", "(= (__ '(:a nil ()))\r\n false)\r\n" ], "times-solved" : 81, "title" : "To Tree, or not to Tree", "user" : "dbyrne" }, 94 | { "_id" : 96, "description" : "Let us define a binary tree as \"symmetric\" if the left half of the tree is the mirror image of the right half of the tree. Write a predicate to determine whether or not a given binary tree is symmetric. (see To Tree, or not to Tree for a reminder on the tree representation we're using).", "difficulty" : "Easy", "restricted" : null, "tags" : [ "trees" ], "tests" : [ "(= (__ '(:a (:b nil nil) (:b nil nil))) true)", "(= (__ '(:a (:b nil nil) nil)) false)", "(= (__ '(:a (:b nil nil) (:c nil nil))) false)", "(= (__ [1 [2 nil [3 [4 [5 nil nil] [6 nil nil]] nil]]\r\n [2 [3 nil [4 [6 nil nil] [5 nil nil]]] nil]])\r\n true)", "(= (__ [1 [2 nil [3 [4 [5 nil nil] [6 nil nil]] nil]]\r\n [2 [3 nil [4 [5 nil nil] [6 nil nil]]] nil]])\r\n false)", "(= (__ [1 [2 nil [3 [4 [5 nil nil] [6 nil nil]] nil]]\r\n [2 [3 nil [4 [6 nil nil] nil]] nil]])\r\n false)" ], "times-solved" : 61, "title" : "Beauty is Symmetry", "user" : "dbyrne" }, 95 | { "_id" : 97, "description" : "Pascal's triangle is a triangle of numbers computed using the following rules:

- The first row is 1.
- Each successive row is computed by adding together adjacent numbers in the row above, and adding a 1 to the beginning and end of the row.

Write a function which returns the nth row of Pascal's Triangle.\r\n\r\n\r\n", "difficulty" : "Easy", "restricted" : null, "tags" : [], "tests" : [ "(= (__ 1) [1])", "(= (map __ (range 1 6))\r\n [ [1]\r\n [1 1]\r\n [1 2 1]\r\n [1 3 3 1]\r\n [1 4 6 4 1]])", "(= (__ 11)\r\n [1 10 45 120 210 252 210 120 45 10 1])" ], "times-solved" : 82, "title" : "Pascal's Triangle", "user" : "dbyrne" }, 96 | { "_id" : 98, "description" : "A function f defined on a domain D induces an equivalence relation on D, as follows: a is equivalent to b with respect to f if and only if (f a) is equal to (f b). Write a function with arguments f and D that computes the equivalence classes of D with respect to f.", "difficulty" : "Medium", "restricted" : null, "tags" : [], "tests" : [ "(= (__ #(* % %) #{-2 -1 0 1 2})\r\n #{#{0} #{1 -1} #{2 -2}})", "(= (__ #(rem % 3) #{0 1 2 3 4 5 })\r\n #{#{0 3} #{1 4} #{2 5}})", "(= (__ identity #{0 1 2 3 4})\r\n #{#{0} #{1} #{2} #{3} #{4}})", "(= (__ (constantly true) #{0 1 2 3 4})\r\n #{#{0 1 2 3 4}})\r\n" ], "times-solved" : 59, "title" : "Equivalence Classes", "user" : "drcabana" }, 97 | { "_id" : 99, "description" : "Write a function which multiplies two numbers and returns the result as a sequence of its digits.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "math", "seqs" ], "tests" : [ "(= (__ 1 1) [1])", "(= (__ 99 9) [8 9 1])", "(= (__ 999 99) [9 8 9 0 1])" ], "times-solved" : 105, "title" : "Product Digits", "user" : "jneira" }, 98 | { "_id" : 100, "description" : "Write a function which calculates the least common multiple. Your function should accept a variable number of positive integers or ratios. ", "difficulty" : "Easy", "restricted" : null, "tags" : [ "math" ], "tests" : [ "(== (__ 2 3) 6)", "(== (__ 5 3 7) 105)", "(== (__ 1/3 2/5) 2)", "(== (__ 3/4 1/6) 3/2)", "(== (__ 7 5/7 2 3/5) 210)" ], "times-solved" : 66, "title" : "Least Common Multiple", "user" : "dbyrne" }, 99 | { "_id" : 101, "description" : "Given two sequences x and y, calculate the Levenshtein distance of x and y, i. e. the minimum number of edits needed to transform x into y. The allowed edits are:

- insert a single item
- delete a single item
- replace a single item with another item

WARNING: Some of the test cases may timeout if you write an inefficient solution!", "difficulty" : "Hard", "restricted" : null, "tags" : [ "seqs" ], "tests" : [ "(= (__ \"kitten\" \"sitting\") 3)", "(= (__ \"closure\" \"clojure\") (__ \"clojure\" \"closure\") 1)", "(= (__ \"xyx\" \"xyyyx\") 2)", "(= (__ \"\" \"123456\") 6)", "(= (__ \"Clojure\" \"Clojure\") (__ \"\" \"\") (__ [] []) 0)", "(= (__ [1 2 3 4] [0 2 3 4 5]) 2)", "(= (__ '(:a :b :c :d) '(:a :d)) 2)", "(= (__ \"ttttattttctg\" \"tcaaccctaccat\") 10)", "(= (__ \"gaattctaatctc\" \"caaacaaaaaattt\") 9)" ], "times-solved" : 31, "title" : "Levenshtein Distance", "user" : "patsp" }, 100 | { "_id" : 102, "description" : "When working with java, you often need to create an object with fieldsLikeThis, but you'd rather work with a hashmap that has :keys-like-this until it's time to convert. Write a function which takes lower-case hyphen-separated strings and converts them to camel-case strings.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "strings" ], "tests" : [ "(= (__ \"something\") \"something\")", "(= (__ \"multi-word-key\") \"multiWordKey\")", "(= (__ \"leaveMeAlone\") \"leaveMeAlone\")" ], "times-solved" : 65, "title" : "intoCamelCase", "user" : "amalloy" }, 101 | { "_id" : 103, "description" : "Given a sequence S consisting of n elements generate all k-combinations of S, i. e. generate all possible sets consisting of k distinct elements taken from S.\r\n\r\nThe number of k-combinations for a sequence is equal to the binomial coefficient.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs", "combinatorics" ], "tests" : [ "(= (__ 1 #{4 5 6}) #{#{4} #{5} #{6}})", "(= (__ 10 #{4 5 6}) #{})", "(= (__ 2 #{0 1 2}) #{#{0 1} #{0 2} #{1 2}})", "(= (__ 3 #{0 1 2 3 4}) #{#{0 1 2} #{0 1 3} #{0 1 4} #{0 2 3} #{0 2 4}\r\n #{0 3 4} #{1 2 3} #{1 2 4} #{1 3 4} #{2 3 4}})", "(= (__ 4 #{[1 2 3] :a \"abc\" \"efg\"}) #{#{[1 2 3] :a \"abc\" \"efg\"}})", "(= (__ 2 #{[1 2 3] :a \"abc\" \"efg\"}) #{#{[1 2 3] :a} #{[1 2 3] \"abc\"} #{[1 2 3] \"efg\"}\r\n #{:a \"abc\"} #{:a \"efg\"} #{\"abc\" \"efg\"}})" ], "times-solved" : 30, "title" : "Generating k-combinations", "user" : "patsp" }, 102 | { "_id" : 104, "description" : "This is the inverse of Problem 92, but much easier. Given an integer smaller than 4000, return the corresponding roman numeral in uppercase, adhering to the subtractive principle.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "strings", "math" ], "tests" : [ "(= \"I\" (__ 1))", "(= \"XXX\" (__ 30))", "(= \"IV\" (__ 4))", "(= \"CXL\" (__ 140))", "(= \"DCCCXXVII\" (__ 827))", "(= \"MMMCMXCIX\" (__ 3999))", "(= \"XLVIII\" (__ 48))" ], "times-solved" : 33, "title" : "Write Roman Numerals", "user" : "0x89" }, 103 | { "_id" : 105, "description" : "Given an input sequence of keywords and numbers, create a map such that each key in the map is a keyword, and the value is a sequence of all the numbers (if any) between it and the next keyword in the sequence.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "maps", "seqs" ], "tests" : [ "(= {} (__ []))", "(= {:a [1]} (__ [:a 1]))", "(= {:a [1], :b [2]} (__ [:a 1, :b 2]))", "(= {:a [1 2 3], :b [], :c [4]} (__ [:a 1 2 3 :b :c 4]))" ], "times-solved" : 40, "title" : "Identify keys and values", "user" : "amalloy" }, 104 | { "_id" : 106, "description" : "Given a pair of numbers, the start and end point, find a path between the two using only three possible operations:\r\n\r\nFind the shortest path through the \"maze\". Because there are multiple shortest paths, you must return the length of the shortest path, not the path itself.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "numbers" ], "tests" : [ "(= 1 (__ 1 1)) ; 1", "(= 3 (__ 3 12)) ; 3 6 12", "(= 3 (__ 12 3)) ; 12 6 3", "(= 3 (__ 5 9)) ; 5 7 9", "(= 9 (__ 9 2)) ; 9 18 20 10 12 6 8 4 2", "(= 5 (__ 9 12)) ; 9 11 22 24 12\r\n" ], "times-solved" : 33, "title" : "Number Maze", "user" : "lucas1000001" }, 105 | { "_id" : 107, "description" : "

Lexical scope and first-class functions are two of the most basic building blocks of a functional language like Clojure. When you combine the two together, you get something very powerful called lexical closures. With these, you can exercise a great deal of control over the lifetime of your local bindings, saving their values for use later, long after the code you're running now has finished.

\r\n\r\n

It can be hard to follow in the abstract, so let's build a simple closure. Given a positive integer n, return a function (f x) which computes xn. Observe that the effect of this is to preserve the value of n for use outside the scope in which it is defined.

", "difficulty" : "Easy", "restricted" : null, "tags" : [ "higher-order-functions", "math" ], "tests" : [ "(= 256 ((__ 2) 16),\r\n ((__ 8) 2))", "(= [1 8 27 64] (map (__ 3) [1 2 3 4]))", "(= [1 2 4 8 16] (map #((__ %) 2) [0 1 2 3 4]))" ], "times-solved" : 68, "title" : "Simple closures", "user" : "amalloy" }, 106 | { "_id" : 122, "description" : "Convert a binary number, provided in the form of a string, to its numerical value.", "difficulty" : "Easy", "restricted" : null, "tags" : null, "tests" : [ "(= 0 (__ \"0\"))", "(= 7 (__ \"111\"))", "(= 8 (__ \"1000\"))", "(= 9 (__ \"1001\"))", "(= 255 (__ \"11111111\"))", "(= 1365 (__ \"10101010101\"))", "(= 65535 (__ \"1111111111111111\"))" ], "title" : "Read a binary number", "user" : "mikera" }, 107 | { "_id" : 108, "description" : "

Given any number of sequences, each sorted from smallest to largest, find the smallest single number which appears in all of the sequences. The sequences may be infinite, so be careful to search lazily.

", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs", "sorting" ], "tests" : [ "(= 3 (__ [3 4 5]))", "(= 4 (__ [1 2 3 4 5 6 7] [0.5 3/2 4 19]))", "(= 7 (__ (range) (range 0 100 7/6) [2 3 5 7 11 13]))", "(= 64 (__ (map #(* % % %) (range)) ;; perfect cubes\r\n (filter #(zero? (bit-and % (dec %))) (range)) ;; powers of 2\r\n (iterate inc 20))) ;; at least as large as 20" ], "times-solved" : 34, "title" : "Lazy Searching", "user" : "amalloy" }, 108 | { "_id" : 110, "description" : "

Write a function that returns a lazy sequence of \"pronunciations\" of a sequence of numbers. A pronunciation of each element in the sequence consists of the number of repeating identical numbers and the number itself. For example, [1 1] is pronounced as [2 1] (\"two ones\"), which in turn is pronounced as [1 2 1 1] (\"one two, one one\").

Your function should accept an initial sequence of numbers, and return an infinite lazy sequence of pronunciations, each element being a pronunciation of the previous element.

", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs" ], "tests" : [ "(= [[1 1] [2 1] [1 2 1 1]] (take 3 (__ [1])))", "(= [3 1 2 4] (first (__ [1 1 1 4 4])))", "(= [1 1 1 3 2 1 3 2 1 1] (nth (__ [1]) 6))", "(= 338 (count (nth (__ [3 2]) 15)))\r\n" ], "times-solved" : 34, "title" : "Sequence of pronunciations", "user" : "mlni" }, 109 | { "_id" : 111, "description" : "Write a function that takes a string and a partially-filled crossword puzzle board, and determines if the input string can be legally placed onto the board.\r\n

\r\n\r\nThe crossword puzzle board consists of a collection of partially-filled rows. Empty spaces are denoted with an underscore (_), unusable spaces are denoted with a hash symbol (#), and pre-filled spaces have a character in place; the whitespace characters are for legibility and should be ignored.\r\n

\r\nFor a word to be legally placed on the board:\r\n
\r\n- It may use empty spaces (underscores)\r\n
\r\n- It may use but must not conflict with any pre-filled characters.\r\n
\r\n- It must not use any unusable spaces (hashes).\r\n
\r\n- There must be no empty spaces (underscores) or extra characters before or after the word (the word may be bound by unusable spaces though).\r\n
\r\n- Characters are not case-sensitive. \r\n
\r\n- Words may be placed vertically (proceeding top-down only), or horizontally (proceeding left-right only).", "difficulty" : "Hard", "restricted" : null, "tags" : [ "game" ], "tests" : [ "(= true (__ \"the\" [\"_ # _ _ e\"]))", "(= false (__ \"the\" [\"c _ _ _\"\r\n \"d _ # e\"\r\n \"r y _ _\"]))", "(= true (__ \"joy\" [\"c _ _ _\"\r\n \"d _ # e\"\r\n \"r y _ _\"]))", "(= false (__ \"joy\" [\"c o n j\"\r\n \"_ _ y _\"\r\n \"r _ _ #\"]))", "(= true (__ \"clojure\" [\"_ _ _ # j o y\"\r\n \"_ _ o _ _ _ _\"\r\n \"_ _ f _ # _ _\"]))\r\n" ], "times-solved" : 15, "title" : "Crossword puzzle", "user" : "mlni" }, 110 | { "_id" : 112, "description" : "Create a function which takes an integer and a nested collection of integers as arguments. Analyze the elements of the input collection and return a sequence which maintains the nested structure, and which includes all elements starting from the head whose sum is less than or equal to the input integer.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs" ], "tests" : [ "(= (__ 10 [1 2 [3 [4 5] 6] 7])\r\n '(1 2 (3 (4))))", "(= (__ 30 [1 2 [3 [4 [5 [6 [7 8]] 9]] 10] 11])\r\n '(1 2 (3 (4 (5 (6 (7)))))))", "(= (__ 9 (range))\r\n '(0 1 2 3))", "(= (__ 1 [[[[[1]]]]])\r\n '(((((1))))))", "(= (__ 0 [1 2 [3 [4 5] 6] 7])\r\n '())", "(= (__ 0 [0 0 [0 [0]]])\r\n '(0 0 (0 (0))))", "(= (__ 1 [-10 [1 [2 3 [4 5 [6 7 [8]]]]]])\r\n '(-10 (1 (2 3 (4)))))" ], "times-solved" : 17, "title" : "Sequs Horribilis", "user" : "ghoseb" }, 111 | { "_id" : 113, "description" : "Write a function that takes a variable number of integer arguments. If the output is coerced into a string, it should return a comma (and space) separated list of the inputs sorted smallest to largest. If the output is coerced into a sequence, it should return a seq of unique input elements in the same order as they were entered.", "difficulty" : "Hard", "restricted" : [ "proxy" ], "tags" : [ "types" ], "tests" : [ "(= \"1, 2, 3\" (str (__ 2 1 3)))", "(= '(2 1 3) (seq (__ 2 1 3)))", "(= '(2 1 3) (seq (__ 2 1 3 3 1 2)))", "(= '(1) (seq (apply __ (repeat 5 1))))", "(= \"1, 1, 1, 1, 1\" (str (apply __ (repeat 5 1))))", "(and (= nil (seq (__)))\r\n (= \"\" (str (__))))" ], "times-solved" : 20, "title" : "Making Data Dance", "user" : "amcnamara" }, 112 | { "_id" : 114, "description" : "

take-while\r\nis great for filtering sequences, but it limited: you can only examine\r\na single item of the sequence at a time. What if you need to keep\r\ntrack of some state as you go over the sequence?

\r\n\r\n

Write a function which accepts an integer n, a predicate p, and a sequence. It should return a lazy sequence of items in the list up to, but not including, the nth item that satisfies the predicate.

", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs", "higher-order-functions" ], "tests" : [ "(= [2 3 5 7 11 13]\r\n (__ 4 #(= 2 (mod % 3))\r\n [2 3 5 7 11 13 17 19 23]))", "(= [\"this\" \"is\" \"a\" \"sentence\"]\r\n (__ 3 #(some #{\\i} %)\r\n [\"this\" \"is\" \"a\" \"sentence\" \"i\" \"wrote\"]))", "(= [\"this\" \"is\"]\r\n (__ 1 #{\"a\"}\r\n [\"this\" \"is\" \"a\" \"sentence\" \"i\" \"wrote\"]))" ], "times-solved" : 24, "title" : "Global take-while", "user" : "amalloy" }, 113 | { "_id" : 115, "description" : "A balanced number is one whose component digits have the same sum on the left and right halves of the number. Write a function which accepts an integer n, and returns true iff n is balanced.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "math" ], "tests" : [ "(= true (__ 11))", "(= true (__ 121))", "(= false (__ 123))", "(= true (__ 0))", "(= false (__ 88099))", "(= true (__ 89098))", "(= true (__ 89089))", "(= (take 20 (filter __ (range)))\r\n [0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101]) " ], "times-solved" : 20, "title" : "The Balance of N", "user" : "amcnamara" }, 114 | { "_id" : 116, "restricted" : null, "title" : "Prime Sandwich", "times-solved" : 0, "difficulty" : "Medium", "tests" : [ "(= false (__ 4))", "(= true (__ 563))", "(= 1103 (nth (filter __ (range)) 15))" ], "user" : "amcnamara", "description" : "A balanced prime is a prime number which is also the mean of the primes directly before and after it in the sequence of valid primes. Create a function which takes an integer n, and returns true iff it is a balanced prime.", "tags" : [ "math" ] }, 115 | { "_id" : 117, "description" : "A mad scientist with tenure has created an experiment tracking mice in a maze. Several mazes have been randomly generated, and you've been tasked with writing a program to determine the mazes in which it's possible for the mouse to reach the cheesy endpoint. Write a function which accepts a maze in the form of a collection of rows, each row is a string where:\r\n\r\nThe mouse is not allowed to travel diagonally in the maze (only up/down/left/right), nor can he escape the edge of the maze. Your function must return true iff the maze is solvable by the mouse.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "game" ], "tests" : [ "(= true (__ [\"M C\"]))", "(= false (__ [\"M # C\"]))", "(= true (__ [\"#######\"\r\n \"# #\"\r\n \"# # #\"\r\n \"#M # C#\"\r\n \"#######\"]))", "(= false (__ [\"########\"\r\n \"#M # #\"\r\n \"# # #\"\r\n \"# # # #\"\r\n \"# # #\"\r\n \"# # #\"\r\n \"# # # #\"\r\n \"# # #\"\r\n \"# # C#\"\r\n \"########\"]))", "(= false (__ [\"M \"\r\n \" \"\r\n \" \"\r\n \" \"\r\n \" ##\"\r\n \" #C\"]))", "(= true (__ [\"C######\"\r\n \" # \"\r\n \" # # \"\r\n \" # #M\"\r\n \" # \"]))", "(= true (__ [\"C# # # #\"\r\n \" \"\r\n \"# # # # \"\r\n \" \"\r\n \" # # # #\"\r\n \" \"\r\n \"# # # #M\"]))" ], "times-solved" : 0, "title" : "For Science!", "user" : "amcnamara" }, 116 | { "_id" : 118, "description" : "

Map is one of the core elements of a functional programming language. Given a function f and an input sequence s, return a lazy sequence of (f x) for each element x in s.", "difficulty" : "Easy", "restricted" : [ "map", "map-indexed", "mapcat", "for" ], "tags" : [ "core-seqs" ], "tests" : [ "(= [3 4 5 6 7]\r\n (__ inc [2 3 4 5 6]))", "(= (repeat 10 nil)\r\n (__ (fn [_] nil) (range 10)))", "(= [1000000 1000001]\r\n (->> (__ inc (range))\r\n (drop (dec 1000000))\r\n (take 2)))" ], "times-solved" : 0, "title" : "Re-implement Map", "user" : "semisight" }, 117 | { "_id" : 119, "description" : "

As in Problem 73, a tic-tac-toe board is represented by a two dimensional vector. X is represented by :x, O is represented by :o, and empty is represented by :e. Create a function that accepts a game piece and board as arguments, and returns a set (possibly empty) of all valid board placements of the game piece which would result in an immediate win.

\r\n\r\n

Board coordinates should be as in calls to get-in. For example, [0 1] is the topmost row, center position.

", "difficulty" : "Hard", "restricted" : null, "tags" : [ "game" ], "tests" : [ "(= (__ :x [[:o :e :e] \r\n [:o :x :o] \r\n [:x :x :e]])\r\n #{[2 2] [0 1] [0 2]})", "(= (__ :x [[:x :o :o] \r\n [:x :x :e] \r\n [:e :o :e]])\r\n #{[2 2] [1 2] [2 0]})", "(= (__ :x [[:x :e :x] \r\n [:o :x :o] \r\n [:e :o :e]])\r\n #{[2 2] [0 1] [2 0]})", "(= (__ :x [[:x :x :o] \r\n [:e :e :e] \r\n [:e :e :e]])\r\n #{})", "(= (__ :o [[:x :x :o] \r\n [:o :e :o] \r\n [:x :e :e]])\r\n #{[2 2] [1 1]})" ], "times-solved" : 8, "title" : "Win at Tic-Tac-Toe", "user" : "shockbob" }, 118 | { "_id" : 120, "description" : "Write a function which takes a collection of integers as an argument. Return the count of how many elements are smaller than the sum of their squared component digits. For example: 10 is larger than 1 squared plus 0 squared; whereas 15 is smaller than 1 squared plus 5 squared.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "math" ], "tests" : [ "(= 8 (__ (range 10)))", "(= 19 (__ (range 30)))", "(= 50 (__ (range 100)))", "(= 50 (__ (range 1000)))" ], "times-solved" : 0, "title" : "Sum of square of digits", "user" : "danilo" }, 119 | { "_id" : 121, "description" : "\t Given a mathematical formula in prefix notation, return a function that calculates\r\n\t the value of the formula.\r\n\t The formula can contain nested calculations using the four basic\r\n\t mathematical operators, numeric constants, and symbols representing variables.\r\n\t The returned function has to accept a single parameter containing the map\r\n\t of variable names to their values.\r\n", "difficulty" : "Medium", "restricted" : [ "eval", "resolve" ], "tags" : [ "functions" ], "tests" : [ "(= 2 ((__ '(/ a b))\r\n '{b 8 a 16}))", "(= 8 ((__ '(+ a b 2))\r\n '{a 2 b 4}))", "(= [6 0 -4]\r\n (map (__ '(* (+ 2 a)\r\n \t (- 10 b)))\r\n\t '[{a 1 b 8}\r\n\t {b 5 a -2}\r\n\t {a 2 b 11}]))", "(= 1 ((__ '(/ (+ x 2)\r\n (* 3 (+ y 1))))\r\n '{x 4 y 1}))\r\n" ], "title" : "Universal Computation Engine", "user" : "mlni" }, 120 | { "_id" : 124, "description" : "

Reversi is normally played on an 8 by 8 board. In this problem, a 4 by 4 board is represented as a two-dimensional vector with black, white, and empty pieces represented by 'b, 'w, and 'e, respectively. Create a function that accepts a game board and color as arguments, and returns a map of legal moves for that color. Each key should be the coordinates of a legal move, and its value a set of the coordinates of the pieces flipped by that move.

\r\n\r\n

Board coordinates should be as in calls to get-in. For example, [0 1] is the topmost row, second column from the left.

", "difficulty" : "Hard", "restricted" : null, "tags" : [ "game" ], "tests" : [ "(= {[1 3] #{[1 2]}, [0 2] #{[1 2]}, [3 1] #{[2 1]}, [2 0] #{[2 1]}}\r\n (__ '[[e e e e]\r\n [e w b e]\r\n [e b w e]\r\n [e e e e]] 'w))", "(= {[3 2] #{[2 2]}, [3 0] #{[2 1]}, [1 0] #{[1 1]}}\r\n (__ '[[e e e e]\r\n [e w b e]\r\n [w w w e]\r\n [e e e e]] 'b))", "(= {[0 3] #{[1 2]}, [1 3] #{[1 2]}, [3 3] #{[2 2]}, [2 3] #{[2 2]}}\r\n (__ '[[e e e e]\r\n [e w b e]\r\n [w w b e]\r\n [e e b e]] 'w))", "(= {[0 3] #{[2 1] [1 2]}, [1 3] #{[1 2]}, [2 3] #{[2 1] [2 2]}}\r\n (__ '[[e e w e]\r\n [b b w e]\r\n [b w w e]\r\n [b w w w]] 'b))\r\n" ], "title" : "Analyze Reversi", "user" : "shockbob" }, 121 | { "_id" : 125, "description" : "Create a function of no arguments which returns a string that is an exact copy of the function itself.\r\n

\r\nHint: read this if you get stuck (this question is harder than it first appears); but it's worth the effort to solve it independently if you can!\r\n

\r\nFun fact: Gus is the name of the 4Clojure dragon.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "logic", "fun", "brain-teaser" ], "tests" : [ "(= (str '__) (__))" ], "title" : "Gus' Quinundrum", "user" : "amcnamara" }, 122 | { "_id" : 126, "description" : "Enter a value which satisfies the following:", "difficulty" : "Easy", "restricted" : null, "tags" : [ "fun", "brain-teaser" ], "tests" : [ "(let [x __]\r\n (and (= (class x) x) x))" ], "title" : "Through the Looking Class", "user" : "amcnamara" }, 123 | { "_id" : 127, "description" : "Everyone loves triangles, and it's easy to understand why—they're so wonderfully symmetric (except scalenes, they suck).\r\n

\r\nYour passion for triangles has led you to become a miner (and part-time Clojure programmer) where you work all day to chip out isosceles-shaped minerals from rocks gathered in a nearby open-pit mine. There are too many rocks coming from the mine to harvest them all so you've been tasked with writing a program to analyze the mineral patterns of each rock, and determine which rocks have the biggest minerals.\r\n

\r\nSomeone has already written a computer-vision system for the mine. It images each rock as it comes into the processing centre and creates a cross-sectional bitmap of mineral (1) and rock (0) concentrations for each one.\r\n

\r\nYou must now create a function which accepts a collection of integers, each integer when read in base-2 gives the bit-representation of the rock (again, 1s are mineral and 0s are worthless scalene-like rock). You must return the cross-sectional area of the largest harvestable mineral from the input rock, as follows:\r\n
\r\n", "difficulty" : "Hard", "restricted" : null, "tags" : [ "search", "data-juggling" ], "tests" : [ "(= 10 (__ [15 15 15 15 15]))\r\n; 1111 1111\r\n; 1111 *111\r\n; 1111 -> **11\r\n; 1111 ***1\r\n; 1111 ****", "(= 15 (__ [1 3 7 15 31]))\r\n; 00001 0000*\r\n; 00011 000**\r\n; 00111 -> 00***\r\n; 01111 0****\r\n; 11111 *****", "(= 3 (__ [3 3]))\r\n; 11 *1\r\n; 11 -> **", "(= 4 (__ [7 3]))\r\n; 111 ***\r\n; 011 -> 0*1", "(= 6 (__ [17 22 6 14 22]))\r\n; 10001 10001\r\n; 10110 101*0\r\n; 00110 -> 00**0\r\n; 01110 0***0\r\n; 10110 10110", "(= 9 (__ [18 7 14 14 6 3]))\r\n; 10010 10010\r\n; 00111 001*0\r\n; 01110 01**0\r\n; 01110 -> 0***0\r\n; 00110 00**0\r\n; 00011 000*1", "(= nil (__ [21 10 21 10]))\r\n; 10101 10101\r\n; 01010 01010\r\n; 10101 -> 10101\r\n; 01010 01010", "(= nil (__ [0 31 0 31 0]))\r\n; 00000 00000\r\n; 11111 11111\r\n; 00000 -> 00000\r\n; 11111 11111\r\n; 00000 00000" ], "title" : "Love Triangle", "user" : "amcnamara" }, 124 | { "_id" : 128, "description" : "

A standard American deck of playing cards has four suits - spades, hearts, diamonds, and clubs - and thirteen cards in each suit. Two is the lowest rank, followed by other integers up to ten; then the jack, queen, king, and ace.

\r\n\r\n

It's convenient for humans to represent these cards as suit/rank pairs, such as H5 or DQ: the heart five and diamond queen respectively. But these forms are not convenient for programmers, so to write a card game you need some way to parse an input string into meaningful components. For purposes of determining rank, we will define the cards to be valued from 0 (the two) to 12 (the ace)

\r\n\r\n

Write a function which converts (for example) the string \"SJ\" into a map of {:suit :spade, :rank 9}. A ten will always be represented with the single character \"T\", rather than the two characters \"10\".

", "difficulty" : "Easy", "restricted" : null, "tags" : [ "strings", "game" ], "tests" : [ "(= {:suit :diamond :rank 10} (__ \"DQ\"))", "(= {:suit :heart :rank 3} (__ \"H5\"))", "(= {:suit :club :rank 12} (__ \"CA\"))", "(= (range 13) (map (comp :rank __ str)\r\n '[S2 S3 S4 S5 S6 S7\r\n S8 S9 ST SJ SQ SK SA]))" ], "title" : "Recognize Playing Cards", "user" : "amalloy" }, 125 | { "_id" : 130, "description" : "Every node of a tree is connected to each of its children as\r\nwell as its parent. One can imagine grabbing one node of\r\na tree and dragging it up to the root position, leaving all\r\nconnections intact. For example, below on the left is\r\na binary tree. By pulling the \"c\" node up to the root, we\r\nobtain the tree on the right.\r\n
\r\n\r\n
\r\nNote it is no longer binary as \"c\" had three connections\r\ntotal -- two children and one parent.\r\n\r\nEach node is represented as a vector, which always has at\r\nleast one element giving the name of the node as a symbol.\r\nSubsequent items in the vector represent the children of the\r\nnode. Because the children are ordered it's important that\r\nthe tree you return keeps the children of each node in order\r\nand that the old parent node, if any, is appended on the\r\nright.\r\n\r\nYour function will be given two args -- the name of the node\r\nthat should become the new root, and the tree to transform.\r\n", "difficulty" : "Hard", "restricted" : null, "tags" : [ "tree" ], "tests" : [ "(= '(n)\r\n (__ 'n '(n)))", "(= '(a (t (e)))\r\n (__ 'a '(t (e) (a))))", "(= '(e (t (a)))\r\n (__ 'e '(a (t (e)))))", "(= '(a (b (c)))\r\n (__ 'a '(c (b (a)))))", "(= '(d \r\n (b\r\n (c)\r\n (e)\r\n (a \r\n (f \r\n (g) \r\n (h)))))\r\n (__ 'd '(a\r\n (b \r\n (c) \r\n (d) \r\n (e))\r\n (f \r\n (g)\r\n (h)))))", "(= '(c \r\n (d) \r\n (e) \r\n (b\r\n (f \r\n (g) \r\n (h))\r\n (a\r\n (i\r\n (j\r\n (k)\r\n (l))\r\n (m\r\n (n)\r\n (o))))))\r\n (__ 'c '(a\r\n (b\r\n (c\r\n (d)\r\n (e))\r\n (f\r\n (g)\r\n (h)))\r\n (i\r\n (j\r\n (k)\r\n (l))\r\n (m\r\n (n)\r\n (o))))))\r\n" ], "title" : "Tree reparenting", "user" : "chouser" }, 126 | { "_id" : 131, "description" : "Given a variable number of sets of integers, create a function which returns true iff all of the sets have a non-empty subset with an equivalent summation.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "math" ], "tests" : [ "(= true (__ #{-1 1 99} \r\n #{-2 2 888}\r\n #{-3 3 7777})) ; ex. all sets have a subset which sums to zero", "(= false (__ #{1}\r\n #{2}\r\n #{3}\r\n #{4}))", "(= true (__ #{1}))", "(= false (__ #{1 -3 51 9} \r\n #{0} \r\n #{9 2 81 33}))", "(= true (__ #{1 3 5}\r\n #{9 11 4}\r\n #{-3 12 3}\r\n #{-3 4 -2 10}))", "(= false (__ #{-1 -2 -3 -4 -5 -6}\r\n #{1 2 3 4 5 6 7 8 9}))", "(= true (__ #{1 3 5 7}\r\n #{2 4 6 8}))", "(= true (__ #{-1 3 -5 7 -9 11 -13 15}\r\n #{1 -3 5 -7 9 -11 13 -15}\r\n #{1 -1 2 -2 4 -4 8 -8}))", "(= true (__ #{-10 9 -8 7 -6 5 -4 3 -2 1}\r\n #{10 -9 8 -7 6 -5 4 -3 2 -1}))" ], "title" : "Sum Some Set Subsets", "user" : "amcnamara" }, 127 | { "_id" : 132, "description" : "Write a function that takes a two-argument predicate, a value, and a collection; and returns a new collection where the value is inserted between every two items that satisfy the predicate.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs", "core-functions" ], "tests" : [ "(= '(1 :less 6 :less 7 4 3) (__ < :less [1 6 7 4 3]))", "(= '(2) (__ > :more [2]))", "(= [0 1 :x 2 :x 3 :x 4] (__ #(and (pos? %) (< % %2)) :x (range 5)))", "(empty? (__ > :more ()))", "(= [0 1 :same 1 2 3 :same 5 8 13 :same 21]\r\n (take 12 (->> [0 1]\r\n (iterate (fn [[a b]] [b (+ a b)]))\r\n (map first) ; fibonacci numbers\r\n (__ (fn [a b] ; both even or both odd\r\n (= (mod a 2) (mod b 2)))\r\n :same))))" ], "title" : "Insert between two items", "user" : "srid" }, 128 | { "_id" : 134, "description" : "Write a function which, given a key and map, returns true iff the map contains an entry with that key and its value is nil.", "difficulty" : "Elementary", "restricted" : null, "tags" : [ "maps" ], "tests" : [ "(true? (__ :a {:a nil :b 2}))", "(false? (__ :b {:a nil :b 2}))", "(false? (__ :c {:a nil :b 2}))" ], "title" : "A nil key", "user" : "goranjovic" }, 129 | { "_id" : 135, "description" : "Your friend Joe is always whining about Lisps using the prefix notation for math. Show him how you could easily write a function that does math using the infix notation. Is your favorite language that flexible, Joe?\r\n\r\nWrite a function that accepts a variable length mathematical expression consisting of numbers and the operations +, -, *, and /. Assume a simple calculator that does not do precedence and instead just calculates left to right.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "higher-order-functions", "math" ], "tests" : [ "(= 7 (__ 2 + 5))", "(= 42 (__ 38 + 48 - 2 / 2))", "(= 8 (__ 10 / 2 - 1 * 2))", "(= 72 (__ 20 / 2 + 2 + 4 + 8 - 6 - 10 * 9))" ], "title" : "Infix Calculator", "user" : "fdaoud" }, 130 | { "_id" : 137, "description" : "Write a function which returns a sequence of digits of a non-negative number (first argument) in numerical system with an arbitrary base (second argument). Digits should be represented with their integer values, e.g. 15 would be [1 5] in base 10, [1 1 1 1] in base 2 and [15] in base 16. ", "difficulty" : "Medium", "restricted" : null, "tags" : [ "math" ], "tests" : [ "(= [1 2 3 4 5 0 1] (__ 1234501 10))", "(= [0] (__ 0 11))", "(= [1 0 0 1] (__ 9 2))", "(= [1 0] (let [n (rand-int 100000)](__ n n)))", "(= [16 18 5 24 15 1] (__ Integer/MAX_VALUE 42))" ], "title" : "Digits and bases", "user" : "goranjovic" }, 131 | { "_id" : 138, "description" : "Create a function of two integer arguments: the start and end, respectively. You must create a vector of strings which renders a 45° rotated square of integers which are successive squares from the start point up to and including the end point. If a number comprises multiple digits, wrap them around the shape individually. If there are not enough digits to complete the shape, fill in the rest with asterisk characters. The direction of the drawing should be clockwise, starting from the center of the shape and working outwards, with the initial direction being down and to the right.", "difficulty" : "Hard", "restricted" : null, "tags" : [ "data-juggling" ], "tests" : [ "(= (__ 2 2) [\"2\"])", "(= (__ 2 4) [\" 2 \"\r\n \"* 4\"\r\n \" * \"])", "(= (__ 3 81) [\" 3 \"\r\n \"1 9\"\r\n \" 8 \"])", "(= (__ 4 20) [\" 4 \"\r\n \"* 1\"\r\n \" 6 \"])", "(= (__ 2 256) [\" 6 \"\r\n \" 5 * \"\r\n \"2 2 *\"\r\n \" 6 4 \"\r\n \" 1 \"])", "(= (__ 10 10000) [\" 0 \"\r\n \" 1 0 \"\r\n \" 0 1 0 \"\r\n \"* 0 0 0\"\r\n \" * 1 * \"\r\n \" * * \"\r\n \" * \"])" ], "title" : "Squares Squared", "user" : "amcnamara" }, 132 | { "_id" : 140, "description" : "Create a function which accepts as input a boolean algebra function in the form of a set of sets, where the inner sets are collections of symbols corresponding to the input boolean variables which satisfy the function (the inputs of the inner sets are conjoint, and the sets themselves are disjoint... also known as canonical minterms). Note: capitalized symbols represent truth, and lower-case symbols represent negation of the inputs. Your function must return the minimal function which is logically equivalent to the input.\r\n

\r\nPS — You may want to give this a read before proceeding: K-Maps", "difficulty" : "Hard", "restricted" : null, "tags" : [ "math", "circuit-design" ], "tests" : [ "(= (__ #{#{'a 'B 'C 'd}\r\n #{'A 'b 'c 'd}\r\n #{'A 'b 'c 'D}\r\n #{'A 'b 'C 'd}\r\n #{'A 'b 'C 'D}\r\n #{'A 'B 'c 'd}\r\n #{'A 'B 'c 'D}\r\n #{'A 'B 'C 'd}})\r\n #{#{'A 'c} \r\n #{'A 'b}\r\n #{'B 'C 'd}})", "(= (__ #{#{'A 'B 'C 'D}\r\n #{'A 'B 'C 'd}})\r\n #{#{'A 'B 'C}})", "(= (__ #{#{'a 'b 'c 'd}\r\n #{'a 'B 'c 'd}\r\n #{'a 'b 'c 'D}\r\n #{'a 'B 'c 'D}\r\n #{'A 'B 'C 'd}\r\n #{'A 'B 'C 'D}\r\n #{'A 'b 'C 'd}\r\n #{'A 'b 'C 'D}})\r\n #{#{'a 'c}\r\n #{'A 'C}})", "(= (__ #{#{'a 'b 'c} \r\n #{'a 'B 'c}\r\n #{'a 'b 'C}\r\n #{'a 'B 'C}})\r\n #{#{'a}})", "(= (__ #{#{'a 'B 'c 'd}\r\n #{'A 'B 'c 'D}\r\n #{'A 'b 'C 'D}\r\n #{'a 'b 'c 'D}\r\n #{'a 'B 'C 'D}\r\n #{'A 'B 'C 'd}})\r\n #{#{'a 'B 'c 'd}\r\n #{'A 'B 'c 'D}\r\n #{'A 'b 'C 'D}\r\n #{'a 'b 'c 'D}\r\n #{'a 'B 'C 'D}\r\n #{'A 'B 'C 'd}})", "(= (__ #{#{'a 'b 'c 'd}\r\n #{'a 'B 'c 'd}\r\n #{'A 'B 'c 'd}\r\n #{'a 'b 'c 'D}\r\n #{'a 'B 'c 'D}\r\n #{'A 'B 'c 'D}})\r\n #{#{'a 'c}\r\n #{'B 'c}})", "(= (__ #{#{'a 'B 'c 'd}\r\n #{'A 'B 'c 'd}\r\n #{'a 'b 'c 'D}\r\n #{'a 'b 'C 'D}\r\n #{'A 'b 'c 'D}\r\n #{'A 'b 'C 'D}\r\n #{'a 'B 'C 'd}\r\n #{'A 'B 'C 'd}})\r\n #{#{'B 'd}\r\n #{'b 'D}})", "(= (__ #{#{'a 'b 'c 'd}\r\n #{'A 'b 'c 'd}\r\n #{'a 'B 'c 'D}\r\n #{'A 'B 'c 'D}\r\n #{'a 'B 'C 'D}\r\n #{'A 'B 'C 'D}\r\n #{'a 'b 'C 'd}\r\n #{'A 'b 'C 'd}})\r\n #{#{'B 'D}\r\n #{'b 'd}})" ], "title" : "Veitch, Please!", "user" : "amcnamara" }, 133 | { "_id" : 141, "description" : "

\r\n In trick-taking\r\n card games such as bridge, spades, or hearts, cards are played\r\n in groups known as \"tricks\" - each player plays a single card, in\r\n order; the first player is said to \"lead\" to the trick. After all\r\n players have played, one card is said to have \"won\" the trick. How\r\n the winner is determined will vary by game, but generally the winner\r\n is the highest card played in the suit that was\r\n led. Sometimes (again varying by game), a particular suit will\r\n be designated \"trump\", meaning that its cards are more powerful than\r\n any others: if there is a trump suit, and any trumps are played,\r\n then the highest trump wins regardless of what was led.\r\n

\r\n

\r\n Your goal is to devise a function that can determine which of a\r\n number of cards has won a trick. You should accept a trump suit, and\r\n return a function winner. Winner will be called on a\r\n sequence of cards, and should return the one which wins the\r\n trick. Cards will be represented in the format returned\r\n by Problem 128, Recognize Playing Cards:\r\n a hash-map of :suit and a\r\n numeric :rank. Cards with a larger rank are stronger.\r\n

", "difficulty" : "Medium", "restricted" : null, "tags" : [ "game", "cards" ], "tests" : [ "(let [notrump (__ nil)]\r\n (and (= {:suit :club :rank 9} (notrump [{:suit :club :rank 4}\r\n {:suit :club :rank 9}]))\r\n (= {:suit :spade :rank 2} (notrump [{:suit :spade :rank 2}\r\n {:suit :club :rank 10}]))))", "(= {:suit :club :rank 10} ((__ :club) [{:suit :spade :rank 2}\r\n {:suit :club :rank 10}]))", "(= {:suit :heart :rank 8}\r\n ((__ :heart) [{:suit :heart :rank 6} {:suit :heart :rank 8}\r\n {:suit :diamond :rank 10} {:suit :heart :rank 4}]))" ], "title" : "Tricky card games", "user" : "amalloy" }, 134 | { "_id" : 143, "description" : "Create a function that computes the dot product of two sequences. You may assume that the vectors will have the same length.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "seqs", "math" ], "tests" : [ "(= 0 (__ [0 1 0] [1 0 0]))", "(= 3 (__ [1 1 1] [1 1 1]))", "(= 32 (__ [1 2 3] [4 5 6]))", "(= 256 (__ [2 5 6] [100 10 1]))" ], "title" : "dot product", "user" : "bloop" }, 135 | { "_id" : 144, "description" : "Write an oscillating iterate: a function that takes an initial value and a variable number of functions. It should return a lazy sequence of the functions applied to the value in order, restarting from the first function after it hits the end.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "sequences" ], "tests" : [ "(= (take 3 (__ 3.14 int double)) [3.14 3 3.0])", "(= (take 5 (__ 3 #(- % 3) #(+ 5 %))) [3 0 5 2 7])", "(= (take 12 (__ 0 inc dec inc dec inc)) [0 1 0 1 0 1 2 1 2 1 2 3])\r\n" ], "title" : "Oscilrate", "user" : "bloop" }, 136 | { "_id" : 145, "description" : "Clojure's for macro is a tremendously versatile mechanism for producing a sequence based on some other sequence(s). It can take some time to understand how to use it properly, but that investment will be paid back with clear, concise sequence-wrangling later. With that in mind, read over these for expressions and try to see how each of them produces the same result.", "difficulty" : "Elementary", "restricted" : null, "tags" : [ "core-functions", "seqs" ], "tests" : [ "(= __ (for [x (range 40)\r\n :when (= 1 (rem x 4))]\r\n x))", "(= __ (for [x (iterate #(+ 4 %) 0)\r\n :let [z (inc x)]\r\n :while (< z 40)]\r\n z))", "(= __ (for [[x y] (partition 2 (range 20))]\r\n (+ x y)))" ], "title" : "For the win", "user" : "amalloy" }, 137 | { "_id" : 146, "description" : "

Because Clojure's for macro allows you to \"walk\" over multiple sequences in a nested fashion, it is excellent for transforming all sorts of sequences. If you don't want a sequence as your final output (say you want a map), you are often still best-off using for, because you can produce a sequence and feed it into a map, for example.

\r\n\r\n

For this problem, your goal is to \"flatten\" a map of hashmaps. Each key in your output map should be the \"path\"1 that you would have to take in the original map to get to a value, so for example {1 {2 3}} should result in {[1 2] 3}. You only need to flatten one level of maps: if one of the values is a map, just leave it alone.

\r\n\r\n

1 That is, (get-in original [k1 k2]) should be the same as (get result [k1 k2])

", "difficulty" : "Easy", "restricted" : null, "tags" : [ "seqs", "maps" ], "tests" : [ "(= (__ '{a {p 1, q 2}\r\n b {m 3, n 4}})\r\n '{[a p] 1, [a q] 2\r\n [b m] 3, [b n] 4})", "(= (__ '{[1] {a b c d}\r\n [2] {q r s t u v w x}})\r\n '{[[1] a] b, [[1] c] d,\r\n [[2] q] r, [[2] s] t,\r\n [[2] u] v, [[2] w] x})", "(= (__ '{m {1 [a b c] 3 nil}})\r\n '{[m 1] [a b c], [m 3] nil})" ], "title" : "Trees into tables", "user" : "amalloy" }, 138 | { "_id" : 147, "description" : "

Write a function that, for any given input vector of numbers, returns an infinite lazy sequence of vectors, where each next one is constructed from the previous following the rules used in Pascal's Triangle. For example, for [3 1 2], the next row is [3 4 3 2].

\r\n

Beware of arithmetic overflow! In clojure (since version 1.3 in 2011), if you use an arithmetic operator like + and the result is too large to fit into a 64-bit integer, an exception is thrown. You can use +' to indicate that you would rather overflow into Clojure's slower, arbitrary-precision bigint.

", "difficulty" : "Easy", "restricted" : null, "tags" : [ "seqs" ], "tests" : [ "(= (second (__ [2 3 2])) [2 5 5 2])", "(= (take 5 (__ [1])) [[1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1]])", "(= (take 2 (__ [3 1 2])) [[3 1 2] [3 4 3 2]])", "(= (take 100 (__ [2 4 2])) (rest (take 101 (__ [2 2]))))" ], "title" : "Pascal's Trapezoid", "user" : "narvius" }, 139 | { "_id" : 148, "description" : "

Write a function which calculates the sum of all natural numbers under n (first argument) which are evenly divisible by at least one of a and b (second and third argument). Numbers a and b are guaranteed to be coprimes.

\r\n\r\n

Note: Some test cases have a very large n, so the most obvious solution will exceed the time limit.

", "difficulty" : "Medium", "restricted" : null, "tags" : [ "math" ], "tests" : [ "(= 0 (__ 3 17 11))", "(= 23 (__ 10 3 5))", "(= 233168 (__ 1000 3 5))", "(= \"2333333316666668\" (str (__ 100000000 3 5)))", "(= \"110389610389889610389610\"\r\n (str (__ (* 10000 10000 10000) 7 11)))", "(= \"1277732511922987429116\"\r\n (str (__ (* 10000 10000 10000) 757 809)))", "(= \"4530161696788274281\"\r\n (str (__ (* 10000 10000 1000) 1597 3571)))" ], "title" : "The Big Divide", "user" : "goranjovic" }, 140 | { "_id" : 150, "description" : "

A palindromic number is a number that is the same when written forwards or backwards (e.g., 3, 99, 14341).

\r\n\r\n

Write a function which takes an integer n, as its only argument, and returns an increasing lazy sequence of all palindromic numbers that are not less than n.

\r\n\r\n

The most simple solution will exceed the time limit!

", "difficulty" : "Medium", "restricted" : null, "tags" : [ "seqs", "math" ], "tests" : [ "(= (take 26 (__ 0))\r\n [0 1 2 3 4 5 6 7 8 9 \r\n 11 22 33 44 55 66 77 88 99 \r\n 101 111 121 131 141 151 161])", "(= (take 16 (__ 162))\r\n [171 181 191 202 \r\n 212 222 232 242 \r\n 252 262 272 282 \r\n 292 303 313 323])", "(= (take 6 (__ 1234550000))\r\n [1234554321 1234664321 1234774321 \r\n 1234884321 1234994321 1235005321])", "(= (first (__ (* 111111111 111111111)))\r\n (* 111111111 111111111))", "(= (set (take 199 (__ 0)))\r\n (set (map #(first (__ %)) (range 0 10000))))", "(= true \r\n (apply < (take 6666 (__ 9999999))))", "(= (nth (__ 0) 10101)\r\n 9102019)" ], "title" : "Palindromic Numbers", "user" : "maximental" }, 141 | { "_id" : 152, "description" : "

\r\nA Latin square of order n \r\nis an n x n array that contains n different elements, \r\neach occurring exactly once in each row, and exactly once in each column. \r\nFor example, among the following arrays only the first one forms a Latin square:\r\n

\r\nA B C    A B C    A B C\r\nB C A    B C A    B D A\r\nC A B    C A C    C A B\r\n
\r\n

\r\n

\r\nLet V be a vector of such vectors1 that they may differ in length2.\r\nWe will say that an arrangement of vectors of V in consecutive rows \r\nis an alignment (of vectors) of V\r\nif the following conditions are satisfied:\r\n

\r\nLet L denote a Latin square of order 2 or greater.\r\nWe will say that L is included in V or \r\nthat V includes L\r\niff there exists an alignment of V \r\nsuch that contains a subsquare that is equal to L.\r\n

\r\n

\r\nFor example, if V equals [[1 2 3][2 3 1 2 1][3 1 2]] \r\nthen there are nine alignments of V (brackets omitted):\r\n

 \r\n        1              2              3\r\n\r\n      1 2 3          1 2 3          1 2 3\r\n  A   2 3 1 2 1    2 3 1 2 1    2 3 1 2 1\r\n      3 1 2        3 1 2        3 1 2\r\n\r\n      1 2 3          1 2 3          1 2 3\r\n  B   2 3 1 2 1    2 3 1 2 1    2 3 1 2 1\r\n        3 1 2        3 1 2        3 1 2\r\n\r\n      1 2 3          1 2 3          1 2 3\r\n  C   2 3 1 2 1    2 3 1 2 1    2 3 1 2 1\r\n          3 1 2        3 1 2        3 1 2\r\n
\r\nAlignment A1 contains Latin square [[1 2 3][2 3 1][3 1 2]], \r\nalignments A2, A3, B1, B2, B3 contain no Latin squares, and alignments\r\nC1, C2, C3 contain [[2 1][1 2]].\r\nThus in this case V includes one Latin square of order 3 \r\nand one of order 2 which is included three times.\r\n

\r\n

\r\nOur aim is to implement a function which accepts a vector of vectors V as an argument, \r\nand returns a map which keys and values are integers. \r\nEach key should be the order of a Latin square included in V, \r\nand its value a count of different Latin squares of that order included in V. \r\nIf V does not include any Latin squares an empty map should be returned.\r\nIn the previous example the correct output of such a function is {3 1, 2 1} and not {3 1, 2 3}.\r\n

\r\n

\r\n1 Of course, we can consider sequences instead of vectors. \r\n
\r\n2 Length of a vector is the number of elements in the vector.\r\n

", "difficulty" : "Hard", "restricted" : null, "tags" : [ "data-analysis", "math" ], "tests" : [ "(= (__ '[[A B C D]\r\n [A C D B]\r\n [B A D C]\r\n [D C A B]])\r\n {})", "(= (__ '[[A B C D E F]\r\n [B C D E F A]\r\n [C D E F A B]\r\n [D E F A B C]\r\n [E F A B C D]\r\n [F A B C D E]])\r\n {6 1})", "(= (__ '[[A B C D]\r\n [B A D C]\r\n [D C B A]\r\n [C D A B]])\r\n {4 1, 2 4})", "(= (__ '[[B D A C B]\r\n [D A B C A]\r\n [A B C A B]\r\n [B C A B C]\r\n [A D B C A]])\r\n {3 3})", "(= (__ [ [2 4 6 3]\r\n [3 4 6 2]\r\n [6 2 4] ])\r\n {})", "(= (__ [[1]\r\n [1 2 1 2]\r\n [2 1 2 1]\r\n [1 2 1 2]\r\n [] ])\r\n {2 2})", "(= (__ [[3 1 2]\r\n [1 2 3 1 3 4]\r\n [2 3 1 3] ])\r\n {3 1, 2 2})", "(= (__ [[8 6 7 3 2 5 1 4]\r\n [6 8 3 7]\r\n [7 3 8 6]\r\n [3 7 6 8 1 4 5 2]\r\n [1 8 5 2 4]\r\n [8 1 2 4 5]])\r\n {4 1, 3 1, 2 7})" ], "title" : "Latin Square Slicing", "user" : "maximental" }, 142 | { "_id" : 153, "description" : "\r\n

\r\nGiven a set of sets, create a function which returns true \r\nif no two of those sets have any elements in common1 and false otherwise. \r\nSome of the test cases are a bit tricky, so pay a little more attention to them. \r\n

\r\n\r\n

\r\n1Such sets are usually called pairwise disjoint or mutually disjoint.\r\n

\r\n", "difficulty" : "Easy", "restricted" : null, "tags" : [ "set-theory" ], "tests" : [ "(= (__ #{#{\\U} #{\\s} #{\\e \\R \\E} #{\\P \\L} #{\\.}})\r\n true)", "(= (__ #{#{:a :b :c :d :e}\r\n #{:a :b :c :d}\r\n #{:a :b :c}\r\n #{:a :b}\r\n #{:a}})\r\n false)", "(= (__ #{#{[1 2 3] [4 5]}\r\n #{[1 2] [3 4 5]}\r\n #{[1] [2] 3 4 5}\r\n #{1 2 [3 4] [5]}})\r\n true)", "(= (__ #{#{'a 'b}\r\n #{'c 'd 'e}\r\n #{'f 'g 'h 'i}\r\n #{''a ''c ''f}})\r\n true)", "(= (__ #{#{'(:x :y :z) '(:x :y) '(:z) '()}\r\n #{#{:x :y :z} #{:x :y} #{:z} #{}}\r\n #{'[:x :y :z] [:x :y] [:z] [] {}}})\r\n false)", "(= (__ #{#{(= \"true\") false}\r\n #{:yes :no}\r\n #{(class 1) 0}\r\n #{(symbol \"true\") 'false}\r\n #{(keyword \"yes\") ::no}\r\n #{(class '1) (int \\0)}})\r\n false)", "(= (__ #{#{distinct?}\r\n #{#(-> %) #(-> %)}\r\n #{#(-> %) #(-> %) #(-> %)}\r\n #{#(-> %) #(-> %) #(-> %)}})\r\n true)", "(= (__ #{#{(#(-> *)) + (quote mapcat) #_ nil}\r\n #{'+ '* mapcat (comment mapcat)}\r\n #{(do) set contains? nil?}\r\n #{, , , #_, , empty?}})\r\n false)" ], "title" : "Pairwise Disjoint Sets", "user" : "maximental" }, 143 | { "_id" : 156, "description" : "When retrieving values from a map, you can specify default values in case the key is not found:

(= 2 (:foo {:bar 0, :baz 1} 2))

However, what if you want the map itself to contain the default values? Write a function which takes a default value and a sequence of keys and constructs a map.", "difficulty" : "Elementary", "restricted" : null, "tags" : [ "seqs" ], "tests" : [ "(= (__ 0 [:a :b :c]) {:a 0 :b 0 :c 0})", "(= (__ \"x\" [1 2 3]) {1 \"x\" 2 \"x\" 3 \"x\"})", "(= (__ [:a :b] [:foo :bar]) {:foo [:a :b] :bar [:a :b]})" ], "title" : "Map Defaults", "user" : "jaycfields" }, 144 | { "_id" : 157, "description" : "Transform a sequence into a sequence of pairs containing the original elements along with their index.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "seqs" ], "tests" : [ "(= (__ [:a :b :c]) [[:a 0] [:b 1] [:c 2]])", "(= (__ [0 1 3]) '((0 0) (1 1) (3 2)))", "(= (__ [[:foo] {:bar :baz}]) [[[:foo] 0] [{:bar :baz} 1]])" ], "title" : "Indexing Sequences", "user" : "jaycfields" }, 145 | { "_id" : 158, "description" : "Write a function that accepts a curried function of unknown arity n. Return an equivalent function of n arguments.\r\n
\r\nYou may wish to read this.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "partial-functions" ], "tests" : [ "(= 10 ((__ (fn [a]\r\n (fn [b]\r\n (fn [c]\r\n (fn [d]\r\n (+ a b c d))))))\r\n 1 2 3 4))", "(= 24 ((__ (fn [a]\r\n (fn [b]\r\n (fn [c]\r\n (fn [d]\r\n (* a b c d))))))\r\n 1 2 3 4))", "(= 25 ((__ (fn [a]\r\n (fn [b]\r\n (* a b))))\r\n 5 5))\r\n" ], "title" : "Decurry", "user" : "amcnamara" }, 146 | { "_id" : 161, "description" : "Set A is a subset of set B, or equivalently B is a superset of A, if A is \"contained\" inside B. A and B may coincide.", "difficulty" : "Elementary", "restricted" : null, "tags" : [ "set-theory" ], "tests" : [ "(clojure.set/superset? __ #{2})", "(clojure.set/subset? #{1} __)", "(clojure.set/superset? __ #{1 2})", "(clojure.set/subset? #{1 2} __)" ], "title" : "Subset and Superset", "user" : "hangkous" }, 147 | { "_id" : 162, "description" : "In Clojure, only nil and false represent the values of logical falsity in conditional tests - anything else is logical truth.", "difficulty" : "Elementary", "restricted" : null, "tags" : [ "logic" ], "tests" : [ "(= __ (if-not false 1 0))", "(= __ (if-not nil 1 0))", "(= __ (if true 1 0))", "(= __ (if [] 1 0))", "(= __ (if [0] 1 0))", "(= __ (if 0 1 0))", "(= __ (if 1 1 0))" ], "title" : "Logical falsity and truth", "user" : "hangkous" }, 148 | { "_id" : 164, "description" : "A deterministic finite automaton (DFA) is an abstract machine that recognizes a regular language. Usually a DFA is defined by a 5-tuple, but instead we'll use a map with 5 keys:\r\n\r\n\r\n\r\nWrite a function that takes as input a DFA definition (as described above) and returns a sequence enumerating all strings in the language recognized by the DFA.\r\n\r\nNote: Although the DFA itself is finite and only recognizes finite-length strings it can still recognize an infinite set of finite-length strings. And because stack space is finite, make sure you don't get stuck in an infinite loop that's not producing results every so often!", "difficulty" : "Hard", "restricted" : null, "tags" : [ "automata", "seqs" ], "tests" : [ "(= #{\"a\" \"ab\" \"abc\"}\r\n (set (__ '{:states #{q0 q1 q2 q3}\r\n :alphabet #{a b c}\r\n :start q0\r\n :accepts #{q1 q2 q3}\r\n :transitions {q0 {a q1}\r\n q1 {b q2}\r\n q2 {c q3}}})))", "\r\n(= #{\"hi\" \"hey\" \"hello\"}\r\n (set (__ '{:states #{q0 q1 q2 q3 q4 q5 q6 q7}\r\n :alphabet #{e h i l o y}\r\n :start q0\r\n :accepts #{q2 q4 q7}\r\n :transitions {q0 {h q1}\r\n q1 {i q2, e q3}\r\n q3 {l q5, y q4}\r\n q5 {l q6}\r\n q6 {o q7}}})))", "(= (set (let [ss \"vwxyz\"] (for [i ss, j ss, k ss, l ss] (str i j k l))))\r\n (set (__ '{:states #{q0 q1 q2 q3 q4}\r\n :alphabet #{v w x y z}\r\n :start q0\r\n :accepts #{q4}\r\n :transitions {q0 {v q1, w q1, x q1, y q1, z q1}\r\n q1 {v q2, w q2, x q2, y q2, z q2}\r\n q2 {v q3, w q3, x q3, y q3, z q3}\r\n q3 {v q4, w q4, x q4, y q4, z q4}}})))", "(let [res (take 2000 (__ '{:states #{q0 q1}\r\n :alphabet #{0 1}\r\n :start q0\r\n :accepts #{q0}\r\n :transitions {q0 {0 q0, 1 q1}\r\n q1 {0 q1, 1 q0}}}))]\r\n (and (every? (partial re-matches #\"0*(?:10*10*)*\") res)\r\n (= res (distinct res))))", "(let [res (take 2000 (__ '{:states #{q0 q1}\r\n :alphabet #{n m}\r\n :start q0\r\n :accepts #{q1}\r\n :transitions {q0 {n q0, m q1}}}))]\r\n (and (every? (partial re-matches #\"n*m\") res)\r\n (= res (distinct res))))", "(let [res (take 2000 (__ '{:states #{q0 q1 q2 q3 q4 q5 q6 q7 q8 q9}\r\n :alphabet #{i l o m p t}\r\n :start q0\r\n :accepts #{q5 q8}\r\n :transitions {q0 {l q1}\r\n q1 {i q2, o q6}\r\n q2 {m q3}\r\n q3 {i q4}\r\n q4 {t q5}\r\n q6 {o q7}\r\n q7 {p q8}\r\n q8 {l q9}\r\n q9 {o q6}}}))]\r\n (and (every? (partial re-matches #\"limit|(?:loop)+\") res)\r\n (= res (distinct res))))\r\n" ], "title" : "Language of a DFA", "user" : "daowen" }, 149 | { "_id" : 166, "description" : "For any orderable data type it's possible to derive all of the basic comparison operations (<, ≤, =, ≠, ≥, and >) from a single operation (any operator but = or ≠ will work). Write a function that takes three arguments, a less than operator for the data and two items to compare. The function should return a keyword describing the relationship between the two items. The keywords for the relationship between x and y are as follows:\r\n\r\n", "difficulty" : "Easy", "restricted" : null, "tags" : null, "tests" : [ "(= :gt (__ < 5 1))", "(= :eq (__ (fn [x y] (< (count x) (count y))) \"pear\" \"plum\"))", "(= :lt (__ (fn [x y] (< (mod x 5) (mod y 5))) 21 3))", "(= :gt (__ > 0 2))\r\n" ], "title" : "Comparisons", "user" : "daowen" }, 150 | { "_id" : 168, "description" : "

\r\nIn what follows, m, n, s, t \r\ndenote nonnegative integers, f denotes a function that accepts two \r\narguments and is defined for all nonnegative integers in both arguments.\r\n

\r\n\r\n

\r\nIn mathematics, the function f can be interpreted as an infinite \r\nmatrix\r\nwith infinitely many rows and columns that, when written, looks like an ordinary \r\nmatrix but its rows and columns cannot be written down completely, so are terminated \r\nwith ellipses. In Clojure, such infinite matrix can be represented \r\nas an infinite lazy sequence of infinite lazy sequences, \r\nwhere the inner sequences represent rows.\r\n

\r\n\r\n

\r\nWrite a function that accepts 1, 3 and 5 arguments\r\n

\r\n

", "difficulty" : "Medium", "restricted" : [ "for", "range", "iterate", "repeat", "cycle", "drop" ], "tags" : [ "seqs", "recursion", "math" ], "tests" : [ "(= (take 5 (map #(take 6 %) (__ str)))\r\n [[\"00\" \"01\" \"02\" \"03\" \"04\" \"05\"]\r\n [\"10\" \"11\" \"12\" \"13\" \"14\" \"15\"]\r\n [\"20\" \"21\" \"22\" \"23\" \"24\" \"25\"]\r\n [\"30\" \"31\" \"32\" \"33\" \"34\" \"35\"]\r\n [\"40\" \"41\" \"42\" \"43\" \"44\" \"45\"]])", "(= (take 6 (map #(take 5 %) (__ str 3 2)))\r\n [[\"32\" \"33\" \"34\" \"35\" \"36\"]\r\n [\"42\" \"43\" \"44\" \"45\" \"46\"]\r\n [\"52\" \"53\" \"54\" \"55\" \"56\"]\r\n [\"62\" \"63\" \"64\" \"65\" \"66\"]\r\n [\"72\" \"73\" \"74\" \"75\" \"76\"]\r\n [\"82\" \"83\" \"84\" \"85\" \"86\"]])", "(= (__ * 3 5 5 7)\r\n [[15 18 21 24 27 30 33]\r\n [20 24 28 32 36 40 44]\r\n [25 30 35 40 45 50 55]\r\n [30 36 42 48 54 60 66]\r\n [35 42 49 56 63 70 77]])", "(= (__ #(/ % (inc %2)) 1 0 6 4)\r\n [[1/1 1/2 1/3 1/4]\r\n [2/1 2/2 2/3 1/2]\r\n [3/1 3/2 3/3 3/4]\r\n [4/1 4/2 4/3 4/4]\r\n [5/1 5/2 5/3 5/4]\r\n [6/1 6/2 6/3 6/4]])", "(= (class (__ (juxt bit-or bit-xor)))\r\n (class (__ (juxt quot mod) 13 21))\r\n (class (lazy-seq)))", "(= (class (nth (__ (constantly 10946)) 34))\r\n (class (nth (__ (constantly 0) 5 8) 55))\r\n (class (lazy-seq)))", "(= (let [m 377 n 610 w 987\r\n check (fn [f s] (every? true? (map-indexed f s)))\r\n row (take w (nth (__ vector) m))\r\n column (take w (map first (__ vector m n)))\r\n diagonal (map-indexed #(nth %2 %) (__ vector m n w w))]\r\n (and (check #(= %2 [m %]) row)\r\n (check #(= %2 [(+ m %) n]) column)\r\n (check #(= %2 [(+ m %) (+ n %)]) diagonal)))\r\n true)" ], "title" : "Infinite Matrix", "user" : "maximental" }, 151 | { "_id" : 171, "description" : "Write a function that takes a sequence of integers and returns a sequence of \"intervals\". Each interval is a a vector of two integers, start and end, such that all integers between start and end (inclusive) are contained in the input sequence.", "difficulty" : "Medium", "restricted" : null, "tags" : null, "tests" : [ "(= (__ [1 2 3]) [[1 3]])", "(= (__ [10 9 8 1 2 3]) [[1 3] [8 10]])", "(= (__ [1 1 1 1 1 1 1]) [[1 1]])", "(= (__ []) [])", "(= (__ [19 4 17 1 3 10 2 13 13 2 16 4 2 15 13 9 6 14 2 11])\r\n [[1 4] [6 6] [9 11] [13 17] [19 19]])\r\n" ], "title" : "Intervals", "user" : "aiba" }, 152 | { "_id" : 173, "description" : "Sequential destructuring allows you to bind symbols to parts of sequential things (vectors, lists, seqs, etc.): (let [bindings* ] exprs*)\r\n\r\nComplete the bindings so all let-parts evaluate to 3.", "difficulty" : "Easy", "restricted" : null, "tags" : [ "Destructuring" ], "tests" : [ "(= 3\r\n (let [[__] [+ (range 3)]] (apply __))\r\n (let [[[__] b] [[+ 1] 2]] (__ b))\r\n (let [[__] [inc 2]] (__)))" ], "title" : "Intro to Destructuring 2", "user" : "hangkous" }, 153 | { "_id" : 177, "description" : "When parsing a snippet of code it's often a good idea to do a sanity check to see if all the brackets match up. Write a function that takes in a string and returns truthy if all square [ ] round ( ) and curly { } brackets are properly paired and legally nested, or returns falsey otherwise.", "difficulty" : "Medium", "restricted" : null, "tags" : [ "parsing" ], "tests" : [ "(__ \"This string has no brackets.\")", "(__ \"class Test {\r\n public static void main(String[] args) {\r\n System.out.println(\\\"Hello world.\\\");\r\n }\r\n }\")", "(not (__ \"(start, end]\"))", "(not (__ \"())\"))", "(not (__ \"[ { ] } \"))", "(__ \"([]([(()){()}(()(()))(([[]]({}()))())]((((()()))))))\")", "(not (__ \"([]([(()){()}(()(()))(([[]]({}([)))())]((((()()))))))\"))", "(not (__ \"[\"))" ], "title" : "Balancing Brackets", "user" : "daowen" }, 154 | { "_id" : 178, "description" : "

Following on from Recognize Playing Cards, determine the best poker hand that can be made with five cards. The hand rankings are listed below for your convenience.

\r\n\r\n
    \r\n
  1. Straight flush: All cards in the same suit, and in sequence
  2. \r\n
  3. Four of a kind: Four of the cards have the same rank
  4. \r\n
  5. Full House: Three cards of one rank, the other two of another rank
  6. \r\n
  7. Flush: All cards in the same suit
  8. \r\n
  9. Straight: All cards in sequence (aces can be high or low, but not both at once)
  10. \r\n
  11. Three of a kind: Three of the cards have the same rank
  12. \r\n
  13. Two pair: Two pairs of cards have the same rank
  14. \r\n
  15. Pair: Two cards have the same rank
  16. \r\n
  17. High card: None of the above conditions are met
  18. \r\n
", "difficulty" : "Hard", "restricted" : null, "tags" : [ "strings", "game" ], "tests" : [ "(= :high-card (__ [\"HA\" \"D2\" \"H3\" \"C9\" \"DJ\"]))", "(= :pair (__ [\"HA\" \"HQ\" \"SJ\" \"DA\" \"HT\"]))", "(= :two-pair (__ [\"HA\" \"DA\" \"HQ\" \"SQ\" \"HT\"]))", "(= :three-of-a-kind (__ [\"HA\" \"DA\" \"CA\" \"HJ\" \"HT\"]))", "(= :straight (__ [\"HA\" \"DK\" \"HQ\" \"HJ\" \"HT\"]))", "(= :straight (__ [\"HA\" \"H2\" \"S3\" \"D4\" \"C5\"]))", "(= :flush (__ [\"HA\" \"HK\" \"H2\" \"H4\" \"HT\"]))", "(= :full-house (__ [\"HA\" \"DA\" \"CA\" \"HJ\" \"DJ\"]))", "(= :four-of-a-kind (__ [\"HA\" \"DA\" \"CA\" \"SA\" \"DJ\"]))", "(= :straight-flush (__ [\"HA\" \"HK\" \"HQ\" \"HJ\" \"HT\"]))\r\n" ], "title" : "Best Hand", "user" : "toolkit" }, 155 | { "_id" : 195, "title" : "Parentheses... Again", "difficulty" : "Medium", "description" : "

In a family of languages like Lisp, having balanced parentheses is a defining feature of the language. Luckily, Lisp has almost no syntax, except for these \"delimiters\" -- and that hardly qualifies as \"syntax\", at least in any useful computer programming sense.

\r\n\r\n

It is not a difficult exercise to find all the combinations of well-formed parentheses if we only have N pairs to work with. For instance, if we only have 2 pairs, we only have two possible combinations: \"()()\" and \"(())\". Any other combination of length 4 is ill-formed. Can you see why?

\r\n\r\n

Generate all possible combinations of well-formed parentheses of length 2n (n pairs of parentheses).\r\nFor this problem, we only consider '(' and ')', but the answer is similar if you work with only {} or only [].

\r\n\r\n

There is an interesting pattern in the numbers!

", "tags" : [ "math", "combinatorics" ], "restricted" : null, "tests" : [ "(= [#{\"\"} #{\"()\"} #{\"()()\" \"(())\"}] (map (fn [n] (__ n)) [0 1 2]))", "(= #{\"((()))\" \"()()()\" \"()(())\" \"(())()\" \"(()())\"} (__ 3))", "(= 16796 (count (__ 10)))", "(= (nth (sort (filter #(.contains ^String % \"(()()()())\") (__ 9))) 6) \"(((()()()())(())))\")", "(= (nth (sort (__ 12)) 5000) \"(((((()()()()()))))(()))\")" ], "user" : "djtrack16" }, 156 | { "_id" : 5, "description" : "

When operating on a list, the conj function will return a new list with one or more items \"added\" to the front.

\r\n

Note that there are two test cases, but you are expected to supply only one answer, which will cause all the tests to pass.

", "difficulty" : "Elementary", "tags" : null, "tests" : [ "(= __ (conj '(2 3 4) 1))", "(= __ (conj '(3 4) 2 1))" ], "times-solved" : 1260, "title" : "Lists: conj", "user" : "dbyrne", "restricted" : null }] 157 | -------------------------------------------------------------------------------- /public/index.template.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 4clojure 4ever 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /shadow-cljs.edn: -------------------------------------------------------------------------------- 1 | ;; shadow-cljs configuration 2 | {:deps {:aliases [:dev]} 3 | :dev-http {8000 "public"} 4 | :builds 5 | {:my-build {:target :browser 6 | :output-dir "public/js" 7 | :asset-path "/js" 8 | :module-hash-names true 9 | :modules {:my-main {:entries [app.core]}} 10 | :build-hooks [(shadow.cljs.build-report/hook)]}}} 11 | -------------------------------------------------------------------------------- /src/app/core.cljs: -------------------------------------------------------------------------------- 1 | (ns app.core 2 | (:require [app.routes :as routes] 3 | [reagent.dom :as rdom] 4 | [reitit.frontend.easy :as rfe])) 5 | 6 | (def notification 7 | [:div {:style {:background-color "#D6D0FD" 8 | :position "absolute" 9 | :top 0 10 | :left 0 11 | :width "100%" 12 | :text-align "center"}} 13 | [:div {:style {:padding "0.5rem"}} 14 | [:small 15 | "Solutions archive browsable from each problem page 🎉 Huge thanks to Alan!!"]]]) 16 | 17 | (defn header [] 18 | [:header 19 | [:h1 "4ever-clojure"] 20 | [:p 21 | [:small 22 | [:a {:href (rfe/href :home) 23 | :data-reitit-handle-click false} "home"] 24 | " | " 25 | [:a {:href "https://github.com/oxalorg/4ever-clojure"} "github"] 26 | " | " 27 | "Built with ❤ by " 28 | [:a {:href "https://twitter.com/oxalorg"} "@oxalorg"] 29 | " and " 30 | [:a {:href "https://twitter.com/borkdude"} "@borkdude"]]] 31 | notification]) 32 | 33 | (defn main [] 34 | [:div 35 | [header] 36 | (when-let [match @routes/match] 37 | (let [view (:view (:data match))] 38 | [view match]))]) 39 | 40 | (defn ^:dev/after-load mount [] 41 | (rdom/render 42 | [main] 43 | (js/document.getElementById "app"))) 44 | 45 | (defn init! [] 46 | (routes/init!) 47 | (mount)) 48 | 49 | (init!) 50 | -------------------------------------------------------------------------------- /src/app/editor.cljs: -------------------------------------------------------------------------------- 1 | (ns app.editor 2 | (:require ["@codemirror/fold" :as fold] 3 | ["@codemirror/gutter" :refer [lineNumbers]] 4 | ["@codemirror/highlight" :as highlight] 5 | ["@codemirror/history" :refer [history #_historyKeymap]] 6 | ["@codemirror/state" :refer [EditorState]] 7 | ["@codemirror/view" :as view :refer [EditorView]] 8 | [app.editor-ex :as editor-ex] 9 | [app.sci :as sci] 10 | [applied-science.js-interop :as j] 11 | [nextjournal.clojure-mode :as cm-clj] 12 | [nextjournal.clojure-mode.live-grammar :as live-grammar] 13 | [reagent.core :as r])) 14 | 15 | (def theme 16 | (.theme 17 | EditorView 18 | (j/lit {".cm-content" {:white-space "pre-wrap", :padding "10px 0"}, 19 | "&.cm-focused" {:outline "none"}, 20 | ".cm-line" {:padding "0 9px", 21 | :line-height "1.6", 22 | :font-size "16px", 23 | :font-family "var(--code-font)"}, 24 | ".cm-matchingBracket" {:border-bottom "1px solid var(--teal-color)", 25 | :color "inherit"}, 26 | ".cm-gutters" {:background "transparent", :border "none"}, 27 | ".cm-gutterElement" {:margin-left "5px"}, 28 | ;; only show cursor when focused 29 | ".cm-cursor" {:visibility "hidden"}, 30 | "&.cm-focused .cm-cursor" {:visibility "visible"}}))) 31 | 32 | (def mk-extensions 33 | (memoize 34 | (fn [extension-mode] 35 | #js 36 | [theme 37 | (history) 38 | highlight/defaultHighlightStyle 39 | (view/drawSelection) 40 | (lineNumbers) 41 | (fold/foldGutter) 42 | (.. EditorState -allowMultipleSelections (of true)) 43 | (let [editor-extensions-to-load (case extension-mode 44 | :basic editor-ex/clojure-mode-extensions-basic 45 | :extended cm-clj/default-extensions 46 | (do (js/console.info (str "Unknown Editor extensions mode: " 47 | (name extension-mode) 48 | ". Defaulting to basic mode.")) 49 | editor-ex/clojure-mode-extensions-basic))] 50 | (if false 51 | ;; use live-reloading grammar 52 | #js [(cm-clj/syntax live-grammar/parser) 53 | (.slice editor-extensions-to-load 1)] 54 | editor-extensions-to-load)) 55 | ;; enable formatting by Tab key 56 | (.of view/keymap (.filter cm-clj/complete-keymap #(= "Tab" (aget % "key")))) 57 | #_(.of view/keymap cm-clj/complete-keymap) 58 | #_(.of view/keymap historyKeymap)]))) 59 | 60 | (defn- make-state [extensions doc] 61 | (.create EditorState 62 | #js{:doc doc 63 | :extensions (cond-> #js[(.. EditorState -allowMultipleSelections (of true))] 64 | extensions 65 | (j/push! extensions))})) 66 | 67 | (defn to-readable-output [result] 68 | (or (::sci/error-str result) 69 | (::sci/result result))) 70 | 71 | (defn editor 72 | [source !view outside-error-str {:keys [eval? extension-mode]}] 73 | (r/with-let 74 | [last-result (when eval? (r/atom (->> source 75 | sci/eval-string 76 | to-readable-output))) 77 | mount! (fn [el] 78 | (when el 79 | (reset! !view 80 | (new EditorView 81 | (j/obj :state (make-state 82 | (cond-> #js [(mk-extensions (or extension-mode :basic))] 83 | eval? (.concat 84 | #js 85 | [(sci/extension 86 | {:modifier "Alt", 87 | :on-result 88 | (fn [result] 89 | (reset! last-result 90 | (to-readable-output result)))})])) 91 | source) 92 | 93 | :parent el)))))] 94 | [:div 95 | [:div 96 | {:ref mount!, 97 | :style {:background-color "#e3e3e3"}}] 98 | (when eval? 99 | [:div 100 | {:style {:white-space "pre-wrap" 101 | :margin-top "0.5rem" 102 | :font-size "0.9em" 103 | :color "#333333" 104 | :font-family "var(--code-font)"}} 105 | [:pre {:style {:margin-bottom "0.5rem"}} 106 | [:span "user=> "] 107 | (try [:code {:style {:white-space "pre-wrap" 108 | :word-break "break-all"}} 109 | (binding [*print-length* 20] 110 | (cond 111 | outside-error-str outside-error-str 112 | (string? @last-result) @last-result 113 | :else (pr-str @last-result)))] 114 | 115 | (catch :default e (str e)))]])] 116 | (finally (j/call @!view :destroy)))) 117 | -------------------------------------------------------------------------------- /src/app/editor_ex.cljs: -------------------------------------------------------------------------------- 1 | (ns app.editor-ex 2 | (:require 3 | ["@codemirror/view" :as view :refer [EditorView]] 4 | ["lezer-clojure" :as lezer-clj] 5 | [nextjournal.clojure-mode :as cm-clj] 6 | [nextjournal.clojure-mode.extensions.eval-region :as eval-region] 7 | [nextjournal.clojure-mode.extensions.formatting :as format] 8 | [nextjournal.clojure-mode.extensions.match-brackets :as match-brackets] 9 | [nextjournal.clojure-mode.extensions.selection-history :as sel-history])) 10 | 11 | (def clojure-mode-extensions-basic 12 | #js[(cm-clj/syntax lezer-clj/parser) 13 | (match-brackets/extension) 14 | (sel-history/extension) 15 | (format/ext-format-changed-lines) 16 | (.-lineWrapping EditorView) 17 | (eval-region/extension {:modifier "Alt"})]) 18 | -------------------------------------------------------------------------------- /src/app/editor_settings.cljs: -------------------------------------------------------------------------------- 1 | (ns app.editor-settings 2 | (:require 3 | [app.state :refer [db]] 4 | [reagent.core :as r])) 5 | 6 | (def editor-settings (r/cursor db [:editor-settings])) 7 | 8 | (defn mk-editor-settings-state [extension-mode] 9 | {:extension-mode extension-mode}) 10 | 11 | (defn save-editor-settings! [new-state] 12 | (reset! editor-settings new-state)) 13 | 14 | (defn modal [on-change-settings] 15 | (let [editor-extension-mode (r/atom (:extension-mode @editor-settings))] 16 | [:div ^{:key "header"} 17 | [:h4 "Editor settings"] 18 | ^{:key "settings"} 19 | [:div 20 | [:div {:style {:display "flex" :align-items "center" :gap 6}} 21 | [:input {:type "checkbox" 22 | :id "checkbox-editor-is-extended-mode" 23 | :checked (= :extended @editor-extension-mode) 24 | :on-change (fn [e] 25 | (let [new-mode (if (-> e 26 | .-target 27 | .-checked) 28 | :extended 29 | :basic)] 30 | (let [new-state (mk-editor-settings-state 31 | new-mode)] 32 | (save-editor-settings! new-state) 33 | (on-change-settings new-state))))}] 34 | [:label {:for "checkbox-editor-is-extended-mode"} "Use extended input mode (can be somewhat intrusive)"]]]])) 35 | -------------------------------------------------------------------------------- /src/app/error.cljs: -------------------------------------------------------------------------------- 1 | (ns app.error 2 | (:require [clojure.string :as str] 3 | [sci.core :as sci])) 4 | 5 | (defn ruler [title] 6 | (println (apply str "----- " title " " (repeat (- 50 7 (count title)) \-)))) 7 | 8 | (defn split-stacktrace [stacktrace verbose?] 9 | (if verbose? [stacktrace] 10 | (let [stack-count (count stacktrace)] 11 | (if (<= stack-count 10) 12 | [stacktrace] 13 | [(take 5 stacktrace) 14 | (drop (- stack-count 5) stacktrace)])))) 15 | 16 | (defn print-stacktrace 17 | [stacktrace {:keys [:verbose?]}] 18 | (let [stacktrace (sci/format-stacktrace stacktrace) 19 | segments (split-stacktrace stacktrace verbose?) 20 | [fst snd] segments] 21 | (run! #(print % "\n") fst) 22 | (when snd 23 | (print "...\n") 24 | (run! #(print % "\n") snd)))) 25 | 26 | (defn error-context [source ex] 27 | (let [{:keys [:line :column]} (ex-data ex)] 28 | (when line 29 | (when-let [content source] 30 | (let [matching-line (dec line) 31 | start-line (max (- matching-line 4) 0) 32 | end-line (+ matching-line 6) 33 | [before after] (->> 34 | (str/split-lines content) 35 | (map-indexed list) 36 | (drop start-line) 37 | (take (- end-line start-line)) 38 | (split-at (inc (- matching-line start-line)))) 39 | snippet-lines (concat before 40 | [[nil (str (str/join "" (repeat (dec column) " ")) 41 | (str "^--- " (ex-message ex)))]] 42 | after) 43 | indices (map first snippet-lines) 44 | max-size (reduce max 0 (map (comp count str) indices)) 45 | snippet-lines (map (fn [[idx line]] 46 | (if idx 47 | (let [line-number (inc idx)] 48 | (str (.padStart (str line-number ":") max-size "0") " " line)) 49 | (str (str/join (repeat (+ 2 max-size) " ")) line))) 50 | snippet-lines)] 51 | (str/join "\n" snippet-lines)))))) 52 | 53 | (defn right-pad [s n] 54 | (let [n (- n (count s))] 55 | (str s (str/join (repeat n " "))))) 56 | 57 | (defn print-locals [locals] 58 | (let [max-name-length (reduce max 0 (map (comp count str) 59 | (keys locals))) 60 | max-name-length (+ max-name-length 2)] 61 | (println 62 | (with-out-str (binding [*print-length* 10 63 | *print-level* 2] 64 | (doseq [[k v] locals] 65 | (print (str (right-pad (str k ": ") max-name-length))) 66 | ;; print nil as nil 67 | (prn v))))))) 68 | 69 | (defn error-handler [source ex] 70 | (let [stacktrace (sci/stacktrace ex) 71 | d (ex-data ex) 72 | sci-error? (isa? (:type d) :sci/error)] 73 | (ruler "Error") 74 | (when-let [name (.-name ex)] 75 | (when-not (= "Error" name) 76 | (println "Type: " name))) 77 | (when-let [m (.-message ex)] 78 | (println (str "Message: " m))) 79 | (let [{:keys [:file :line :column]} d] 80 | (when line 81 | (println (str "Location: " 82 | (when file (str file ":")) 83 | line ":" column"")))) 84 | (when-let [phase (:phase d)] 85 | (println "Phase: " phase)) 86 | (when-let [ec (when sci-error? 87 | (error-context source ex))] 88 | (println) 89 | (ruler "Context") 90 | (println ec)) 91 | (when sci-error? 92 | (when-let 93 | [st (let [st (with-out-str 94 | (when stacktrace 95 | (print-stacktrace stacktrace nil)))] 96 | (when-not (str/blank? st) st))] 97 | (println) 98 | (ruler "Stack trace") 99 | (println st))))) 100 | -------------------------------------------------------------------------------- /src/app/home.cljs: -------------------------------------------------------------------------------- 1 | (ns app.home 2 | (:require [app.data :as data] 3 | [app.state :as state :refer [db]] 4 | [reagent.core :as r])) 5 | 6 | (def user-data (r/cursor db [:solutions])) 7 | 8 | (def sort-by-solved (r/cursor db [:sort-by-solved])) 9 | 10 | (defn sorted-problems [] 11 | (let [data-state 12 | (map #(assoc % :solution (get @user-data (:id %))) 13 | data/problems) 14 | sorted (if (nil? @sort-by-solved) 15 | (sort-by :id data-state) 16 | (sort-by :solution #(not (nil? %)) data-state))] 17 | (if (false? @sort-by-solved) (reverse sorted) sorted))) 18 | 19 | (defn get-problem-status [id] 20 | (let [{:keys [passed failed]} 21 | (get @user-data (js/parseInt id)) 22 | progress (str passed "/" (+ passed failed))] 23 | (cond 24 | (and passed (zero? failed)) 25 | [:span {:style {:color "green"}} (str progress " Passed!")] 26 | (not (nil? passed)) progress 27 | :else "-"))) 28 | 29 | (defn problem-list-item [{:keys [id title _tags difficulty]}] 30 | [:tr 31 | [:td id] 32 | [:td 33 | [:a {:href (state/href :problem/item {:id id})} 34 | title]] 35 | [:td difficulty] 36 | [:td (get-problem-status id)]]) 37 | 38 | (defn problem-list [] 39 | [:<> 40 | [:h3 "Problems " 41 | [:small (str "(" (count data/problems) ")")]] 42 | (into [:table 43 | [:thead 44 | [:tr 45 | [:th {:on-click #(swap! sort-by-solved (fn [] nil))} "No."] 46 | [:th "Name"] 47 | [:th "Difficulty"] 48 | [:th 49 | {:on-click #(swap! sort-by-solved not)} 50 | (str "Status " (case @sort-by-solved 51 | true "🠕" false "🠗" nil ""))]]] 52 | [:tbody 53 | (for [problem (sorted-problems)] 54 | ^{:key (:id problem)} 55 | [problem-list-item problem])]])]) 56 | 57 | (defn view [] 58 | [:div 59 | [:p 60 | "Keeping 4clojure alive forever! This website is completely static and evals 61 | code using sci. Suggestions / PRs welcome at " 62 | [:a {:href "https://github.com/oxalorg/4ever-clojure"} 63 | "github.com/oxalorg/4ever-clojure"]] 64 | [:p 65 | "Please note that 4ever-clojure is evaluated completely in the browser. So 66 | not all Java interop works, but some of it is the same in JS if you're 67 | lucky. Check " 68 | [:a {:href "https://cljs.info/cheatsheet/"} "cljs-cheatsheet"] 69 | " for more info!"] 70 | [problem-list]]) 71 | -------------------------------------------------------------------------------- /src/app/modal.cljs: -------------------------------------------------------------------------------- 1 | (ns app.modal 2 | (:require [goog.string :refer [unescapeEntities]])) 3 | 4 | (def modal-style {:position "fixed", 5 | :width "700px" 6 | :max-width "100%" 7 | :top "50%", 8 | :left "50%", 9 | :transform "translate(-50%, -50%)", 10 | :background "white", 11 | :borderRadius "5px", 12 | :boxShadow "0 0 0 100vw rgba(0, 0, 0, 0.5)", 13 | :zIndex "9999"}) 14 | 15 | (defn close-button [on-close] 16 | [:button {:aria-label "Close Dialog" 17 | :style {:font-size "2.5rem" 18 | :padding "unset" 19 | :background "unset" 20 | :border "unset" 21 | :color "#000" 22 | :margin-top "1rem" 23 | :margin-bottom "-3rem"} 24 | :on-click on-close} 25 | (unescapeEntities "×")]) 26 | 27 | (defn box [{:keys [is-open on-close]} & children] 28 | (when @is-open 29 | [:div {:style modal-style} 30 | [:div {:style {:display "flex" :flex-direction "row" :justify-content "flex-end" :padding-right "2rem"}} 31 | [close-button on-close]] 32 | [:div {:style {:padding "0px 30px 30px 30px"}} children]])) 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/app/problem.cljs: -------------------------------------------------------------------------------- 1 | (ns app.problem 2 | (:require 3 | [app.data :as data] 4 | [app.editor :as editor] 5 | [app.editor-settings :as editor-settings] 6 | [app.modal :as modal] 7 | [app.sci :refer [eval-string] :as evaluator] 8 | [app.state :as state :refer [db]] 9 | [clojure.string :as str] 10 | [reagent.core :as r])) 11 | 12 | (def user-data (r/cursor db [:solutions])) 13 | 14 | (defn get-problem [id] 15 | (first 16 | (filter #(= (:id %) id) data/problems))) 17 | 18 | (defn next-problem [id] 19 | (some #(when (> (:id %) id) %) data/problems)) 20 | 21 | (defn passed? [attempt] 22 | (true? (::evaluator/result attempt))) 23 | 24 | (defn result-type [attempt] 25 | (cond 26 | (::evaluator/error-str attempt) ::error 27 | (passed? attempt) ::passed 28 | :else ::failed)) 29 | 30 | (defn check-solution [problem user-solution] 31 | (let [replaced (mapv #(str/replace % "__" user-solution) (:tests problem)) 32 | results (map eval-string replaced) 33 | grouped-results (group-by result-type results)] 34 | (swap! user-data assoc (:id problem) {:code user-solution 35 | :passed (count (grouped-results ::passed)) 36 | :failed (+ (count (grouped-results ::failed)) 37 | (count (grouped-results ::error)))}) 38 | (if-let [[first-error] (seq (grouped-results ::error))] 39 | {:error first-error} 40 | {:results results}))) 41 | 42 | (def results-style {:display "flex" 43 | :flex-direction "row" 44 | :flex-wrap "wrap" 45 | :font-family "monospace"}) 46 | 47 | (defn render-result [test-src maybe-result] 48 | [:li 49 | [:pre 50 | [:code {:style results-style} 51 | [:span test-src] 52 | (when maybe-result maybe-result)]]]) 53 | 54 | (defn result-list [items] 55 | [:ul {:style {:list-style "none" :padding 0}} 56 | items]) 57 | 58 | (defn test-list-section [tests] 59 | [result-list 60 | (doall 61 | (for [[i test-src] (map-indexed vector tests)] 62 | ^{:key i} 63 | [render-result test-src]))]) 64 | 65 | (defn result-info-item [color text] 66 | [:span {:style {:color color 67 | :align-self "center" 68 | :width "4.5em" 69 | :margin-left "auto"}} 70 | text]) 71 | 72 | (defn test-results-section [attempts tests] 73 | [result-list 74 | (let [test-attempts (map vector tests attempts)] 75 | (for [[i [test-src attempt]] (map-indexed vector test-attempts)] 76 | ^{:key i} 77 | [render-result 78 | test-src 79 | (if (passed? attempt) 80 | [result-info-item "green" "🟢 pass"] 81 | [result-info-item "red" "🔴 uh-oh"])]))]) 82 | 83 | (def run-button-style {:margin-top "1rem"}) 84 | 85 | (defn restricted-alert [problem] 86 | [:p 87 | {:style {:color "#FF0000", 88 | :border-color "darkred", 89 | :border-style "dashed", 90 | :padding "10px"}} "Special Restrictions : " 91 | (str/join "," (:restricted problem))]) 92 | 93 | (defn user-code-section [id problem solution] 94 | (r/with-let [code (r/atom (if-let [code (:code solution "")] 95 | ;; can sometimes be {:code nil} 96 | code "")) 97 | !editor-view (r/atom nil) 98 | editor-extension-mode (r/atom 99 | (:extension-mode 100 | @editor-settings/editor-settings)) 101 | get-editor-value #(some-> @!editor-view .-state .-doc str) 102 | attempts-atom (r/atom '()) 103 | attempt-error-str (r/atom nil) 104 | success-modal-is-open (r/atom false) 105 | success-modal-on-close #(reset! success-modal-is-open false) 106 | settings-modal-is-open (r/atom false) 107 | settings-modal-on-close #(reset! settings-modal-is-open false) 108 | solution-attempted (r/atom false) 109 | tests (:tests problem)] 110 | (let [next-prob (next-problem id) 111 | next-prob-href (state/href :problem/item {:id (:id next-prob)}) 112 | on-run (fn [] 113 | (let [editor-value (get-editor-value) 114 | _ (reset! code editor-value) 115 | {:keys [results error]} (check-solution problem editor-value)] 116 | (if error 117 | (reset! attempt-error-str (::evaluator/error-str error)) 118 | (do 119 | (reset! attempt-error-str nil) 120 | (reset! attempts-atom results) 121 | (reset! solution-attempted true) 122 | (when (every? passed? results) 123 | (reset! success-modal-is-open true))))))] 124 | [:div 125 | (if @solution-attempted 126 | [test-results-section @attempts-atom tests] 127 | [test-list-section tests]) 128 | (when (:restricted problem) 129 | [restricted-alert problem]) 130 | [:p "Write code which will fill in the above blanks:"] 131 | 132 | ;; Force resetting editor state when input source code changed 133 | ;; e.g., when manually trigger run 134 | ^{:key [@code @editor-extension-mode]} 135 | [editor/editor 136 | @code 137 | !editor-view 138 | @attempt-error-str 139 | {:eval? true 140 | :extension-mode @editor-extension-mode}] 141 | [:div {:style {:display "flex" 142 | :justify-content "space-between"}} 143 | [:button {:on-click on-run 144 | :style run-button-style} 145 | "Run"] 146 | [:button {:on-click #(reset! settings-modal-is-open true) 147 | :style run-button-style} 148 | "Settings"]] 149 | [modal/box {:is-open settings-modal-is-open 150 | :on-close settings-modal-on-close} 151 | [editor-settings/modal 152 | (fn [{:keys [extension-mode] :as _editor-settings}] 153 | (reset! code (get-editor-value)) 154 | (reset! editor-extension-mode extension-mode))]] 155 | [:p {:style {:margin-top "1rem"}} 156 | [:small 157 | "Alt+Enter will eval the local form in the editor box above, and Tab will format the code. There are 158 | lots of nifty such features and keybindings. More docs coming soon! (Try 159 | playing with alt + arrows / ctrl + enter / tab) in the meanwhile. 160 | For documentation try e.g. (doc map)."]] 161 | [modal/box {:is-open success-modal-is-open 162 | :on-close success-modal-on-close} 163 | [:h4 (str "Congratulations on solving problem " "#" id "!")] 164 | [:div 165 | [:p {:on-click #(reset! success-modal-is-open false)} 166 | "Next problem " 167 | [:a {:href next-prob-href} 168 | (str "#" (:id next-prob) " " (:title next-prob))]]] 169 | [:button {:on-click #(set! js/window.location next-prob-href)} "Next Problem"]]]))) 170 | 171 | (defn view [_] 172 | (fn [{:keys [path-params] :as _props}] 173 | (let [id (js/parseInt (:id path-params)) 174 | solution (get @user-data id) 175 | {:keys [title description difficulty] :as problem} (get-problem id)] 176 | [:div 177 | [:h3 "Problem " id ", " title] 178 | [:div {:style {:margin-top "0.5rem" :margin-bottom "2rem"}} 179 | [:b "Difficulty: "] difficulty] 180 | [:p description] 181 | ^{:key (str "problem-" id)} 182 | [user-code-section id problem solution] 183 | [:hr] 184 | [:p 185 | "Want to see how others have solved this? " 186 | [:a {:href (state/href :solution/list {:id id})} 187 | "View problem #" id " solutions archive"] 188 | " No cheating please! :)"]]))) 189 | -------------------------------------------------------------------------------- /src/app/routes.cljs: -------------------------------------------------------------------------------- 1 | (ns app.routes 2 | (:require [app.home :as home] 3 | [app.problem :as problem] 4 | [app.solution :as solution] 5 | [reagent.core :as r] 6 | [reitit.frontend :as rf] 7 | [reitit.frontend.controllers :as rfc] 8 | [reitit.frontend.easy :as rfe])) 9 | 10 | (defonce match (r/atom nil)) 11 | 12 | (def routes 13 | [["/" {:name :home 14 | :view home/view}] 15 | ["/problem" {:name :problem/list 16 | :view home/view}] 17 | ["/problem/:id" {:name :problem/item 18 | :view problem/view}] 19 | ["/problem/:id/solutions" {:name :solution/list 20 | :view solution/list-view 21 | :controllers [{:parameters {:path [:id]} 22 | :start (fn [{:keys [path]}] 23 | (solution/state-init! (:id path)))}]}]]) 24 | 25 | (defn route-handler [new-match] 26 | (swap! match 27 | (fn [old-match] 28 | (when new-match 29 | (assoc new-match 30 | :controllers (rfc/apply-controllers (:controllers old-match) new-match)))))) 31 | 32 | (defn init! [] 33 | (rfe/start! 34 | (rf/router routes) 35 | route-handler 36 | {:use-fragment true})) 37 | -------------------------------------------------------------------------------- /src/app/sci.cljs: -------------------------------------------------------------------------------- 1 | (ns app.sci 2 | (:require ["@codemirror/view" :as view] 3 | [app.error :refer [error-handler]] 4 | [applied-science.js-interop :as j] 5 | [goog.string] 6 | [goog.string.format] 7 | [nextjournal.clojure-mode.extensions.eval-region :as eval-region] 8 | [sci.core :as sci] 9 | [sci.impl.evaluator])) 10 | 11 | (def doc-str-src " 12 | (defmacro doc [sym] 13 | `(str \"\n\" (with-out-str (clojure.repl/doc ~sym))))") 14 | 15 | (defonce context 16 | (doto 17 | (sci/init {:classes {'js goog/global 18 | :allow :all} 19 | :namespaces {'clojure.core {'format goog.string/format}}}) 20 | (sci/eval-string* doc-str-src))) 21 | 22 | 23 | (defn eval-string [source] 24 | (try {::result (sci/eval-string* context source)} 25 | (catch :default e 26 | {::error-str (with-out-str (error-handler source e))}))) 27 | 28 | (j/defn eval-at-cursor [on-result ^:js {:keys [state]}] 29 | (some->> (eval-region/cursor-node-string state) 30 | (eval-string) 31 | (on-result)) 32 | true) 33 | 34 | (j/defn eval-top-level [on-result ^:js {:keys [state]}] 35 | (some->> (eval-region/top-level-string state) 36 | (eval-string) 37 | (on-result)) 38 | true) 39 | 40 | (j/defn eval-cell [on-result ^:js {:keys [state]}] 41 | (-> (str "(do " (.-doc state) " )") 42 | (eval-string) 43 | (on-result)) 44 | true) 45 | 46 | (defn keymap* [modifier] 47 | {:eval-cell 48 | [{:key "Mod-Enter" 49 | :doc "Evaluate cell"}] 50 | :eval-at-cursor 51 | [{:key (str modifier "-Enter") 52 | :doc "Evaluates form at cursor"}] 53 | :eval-top-level 54 | [{:key (str modifier "-Shift-Enter") 55 | :doc "Evaluates top-level form at cursor"}]}) 56 | 57 | (defn extension [{:keys [modifier 58 | on-result]}] 59 | (.of view/keymap 60 | (j/lit 61 | [{:key "Mod-Enter" 62 | :run (partial eval-cell on-result)} 63 | {:key (str modifier "-Enter") 64 | :shift (partial eval-top-level on-result) 65 | :run (partial eval-at-cursor on-result)}]))) 66 | -------------------------------------------------------------------------------- /src/app/solution.cljs: -------------------------------------------------------------------------------- 1 | (ns app.solution 2 | (:require 3 | [reagent.core :as r])) 4 | 5 | (defn solutions-url [id] 6 | (str "https://solutions.4clojure.oxal.org/deduped/solutions/" id ".json")) 7 | 8 | (def solutions (r/atom [])) 9 | 10 | (defn state-init! [id] 11 | (reset! solutions []) 12 | (let [id (js/parseInt id) 13 | data (js/fetch (solutions-url id))] 14 | (-> data 15 | (.then (fn [resp] 16 | (.json resp))) 17 | (.then (fn [data] 18 | (reset! solutions (take 1000 (js->clj data)))))))) 19 | 20 | (defn list-view [{:keys [path-params]}] 21 | (let [id (js/parseInt (:id path-params))] 22 | [:div 23 | [:h2 "Solutions for problem #" id] 24 | [:p "Entire archive available at: " 25 | [:a {:href "https://github.com/oxalorg/4clojure-solutions-archive/"} 26 | "github.com/oxalorg/4clojure-solutions-archive/"]] 27 | (if @solutions 28 | [:div 29 | (for [solution @solutions] 30 | ^{:key (get solution "user")} 31 | [:div 32 | [:pre 33 | [:code (get solution "code")]]] 34 | )] 35 | "Loading...")])) 36 | -------------------------------------------------------------------------------- /src/app/state.cljs: -------------------------------------------------------------------------------- 1 | (ns app.state 2 | (:require [alandipert.storage-atom :as lstore] 3 | [reagent.core :as r] 4 | [reitit.frontend.easy :as rfe])) 5 | 6 | (def navigate rfe/push-state) 7 | 8 | (def href rfe/href) 9 | 10 | (def default-db 11 | "default db example: 12 | {:solutions {12 {:code \"(fn [x] (apply + x))\" 13 | :passed 2 14 | :failed 3} 15 | 44 {...}} 16 | :sort-by-solved nil|false|true}" 17 | {:solutions {} 18 | :sort-by-solved nil}) 19 | 20 | (defonce db 21 | (lstore/local-storage (r/atom {}) 22 | :4ever-clojure)) 23 | --------------------------------------------------------------------------------