├── .github └── FUNDING.yml ├── .gitpod.dockerfile ├── doc ├── images │ ├── rich4clojure-header.png │ ├── status-bar-nrepl-button.png │ └── clojure-getting-started-repl.png └── INSTRUCTIONS.md ├── src ├── user.clj └── rich4clojure │ ├── elementary │ ├── problem_013.clj │ ├── problem_015.clj │ ├── problem_009.clj │ ├── problem_037.clj │ ├── problem_011.clj │ ├── problem_016.clj │ ├── problem_052.clj │ ├── problem_003.clj │ ├── problem_008.clj │ ├── problem_007.clj │ ├── problem_014.clj │ ├── problem_035.clj │ ├── problem_012.clj │ ├── problem_057.clj │ ├── problem_161.clj │ ├── problem_004.clj │ ├── problem_018.clj │ ├── problem_134.clj │ ├── problem_010.clj │ ├── problem_002.clj │ ├── problem_017.clj │ ├── problem_006.clj │ ├── problem_036.clj │ ├── problem_005.clj │ ├── problem_162.clj │ ├── problem_001.clj │ ├── problem_068.clj │ ├── problem_156.clj │ ├── problem_071.clj │ ├── problem_072.clj │ ├── problem_064.clj │ └── problem_145.clj │ ├── easy │ ├── problem_126.clj │ ├── problem_045.clj │ ├── problem_042.clj │ ├── problem_051.clj │ ├── problem_066.clj │ ├── problem_026.clj │ ├── problem_020.clj │ ├── problem_099.clj │ ├── problem_019.clj │ ├── problem_029.clj │ ├── problem_024.clj │ ├── problem_025.clj │ ├── problem_041.clj │ ├── problem_034.clj │ ├── problem_028.clj │ ├── problem_038.clj │ ├── problem_023.clj │ ├── problem_048.clj │ ├── problem_030.clj │ ├── problem_021.clj │ ├── problem_031.clj │ ├── problem_143.clj │ ├── problem_032.clj │ ├── problem_049.clj │ ├── problem_157.clj │ ├── problem_022.clj │ ├── problem_100.clj │ ├── problem_122.clj │ ├── problem_047.clj │ ├── problem_040.clj │ ├── problem_033.clj │ ├── problem_081.clj │ ├── problem_027.clj │ ├── problem_061.clj │ ├── problem_173.clj │ ├── problem_088.clj │ ├── problem_039.clj │ ├── problem_062.clj │ ├── problem_083.clj │ ├── problem_120.clj │ ├── problem_118.clj │ ├── problem_063.clj │ ├── problem_095.clj │ ├── problem_097.clj │ ├── problem_090.clj │ ├── problem_135.clj │ ├── problem_166.clj │ ├── problem_096.clj │ ├── problem_147.clj │ ├── problem_107.clj │ ├── problem_146.clj │ ├── problem_128.clj │ └── problem_153.clj │ ├── medium │ ├── problem_067.clj │ ├── problem_046.clj │ ├── problem_074.clj │ ├── problem_043.clj │ ├── problem_044.clj │ ├── problem_080.clj │ ├── problem_055.clj │ ├── problem_116.clj │ ├── problem_056.clj │ ├── problem_054.clj │ ├── problem_085.clj │ ├── problem_075.clj │ ├── problem_104.clj │ ├── problem_105.clj │ ├── problem_070.clj │ ├── problem_060.clj │ ├── problem_102.clj │ ├── problem_144.clj │ ├── problem_059.clj │ ├── problem_093.clj │ ├── problem_115.clj │ ├── problem_171.clj │ ├── problem_058.clj │ ├── problem_137.clj │ ├── problem_108.clj │ ├── problem_078.clj │ ├── problem_050.clj │ ├── problem_086.clj │ ├── problem_098.clj │ ├── problem_158.clj │ ├── problem_077.clj │ ├── problem_076.clj │ ├── problem_069.clj │ ├── problem_177.clj │ ├── problem_132.clj │ ├── problem_112.clj │ ├── problem_148.clj │ ├── problem_110.clj │ ├── problem_114.clj │ ├── problem_121.clj │ ├── problem_103.clj │ ├── problem_131.clj │ ├── problem_065.clj │ ├── problem_150.clj │ ├── problem_195.clj │ ├── problem_141.clj │ └── problem_168.clj │ └── hard │ ├── problem_053.clj │ ├── problem_125.clj │ ├── problem_106.clj │ ├── problem_079.clj │ ├── problem_092.clj │ ├── problem_113.clj │ ├── problem_084.clj │ ├── problem_101.clj │ ├── problem_082.clj │ ├── problem_089.clj │ ├── problem_091.clj │ ├── problem_073.clj │ ├── problem_119.clj │ ├── problem_138.clj │ ├── problem_178.clj │ ├── problem_124.clj │ ├── problem_094.clj │ ├── problem_111.clj │ ├── problem_117.clj │ ├── problem_130.clj │ ├── problem_127.clj │ ├── problem_140.clj │ ├── problem_152.clj │ └── problem_164.clj ├── deps.edn ├── .gitignore ├── .gitpod.yml ├── .vscode └── settings.json ├── CHANGELOG.md ├── README.md └── LICENSE /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [PEZ] 4 | -------------------------------------------------------------------------------- /.gitpod.dockerfile: -------------------------------------------------------------------------------- 1 | FROM gitpod/workspace-full 2 | 3 | RUN brew install clojure/tools/clojure 4 | RUN brew install leiningen -------------------------------------------------------------------------------- /doc/images/rich4clojure-header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PEZ/rich4clojure/HEAD/doc/images/rich4clojure-header.png -------------------------------------------------------------------------------- /doc/images/status-bar-nrepl-button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PEZ/rich4clojure/HEAD/doc/images/status-bar-nrepl-button.png -------------------------------------------------------------------------------- /src/user.clj: -------------------------------------------------------------------------------- 1 | (ns user ; user ns is loaded by REPL startup 2 | (:require [hyperfiddle.rcf])) 3 | 4 | (hyperfiddle.rcf/enable!) -------------------------------------------------------------------------------- /doc/images/clojure-getting-started-repl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PEZ/rich4clojure/HEAD/doc/images/clojure-getting-started-repl.png -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["src"] 2 | :deps {org.clojure/clojure {:mvn/version "1.11.1"} 3 | com.hyperfiddle/rcf {:mvn/version "20220926-202227"}} 4 | :aliases 5 | {}} 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | *.jar 5 | *.class 6 | /.cpcache 7 | /.lein-* 8 | /.nrepl-history 9 | /.nrepl-port 10 | .calva/**/*.calva-repl 11 | .lsp/sqlite*.db 12 | .DS_Store 13 | **/.cache -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | image: 2 | file: .gitpod.dockerfile 3 | 4 | vscode: 5 | extensions: 6 | - betterthantomorrow.calva 7 | 8 | tasks: 9 | - name: Prepare deps 10 | init: clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version,"0.8.3"},cider/cider-nrepl {:mvn/version,"0.26.0"}}}' -P 11 | - name: INSTRUCTIONS 12 | command: code doc/INSTRUCTIONS.md 13 | 14 | github: 15 | prebuilds: 16 | main: true -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "calva.replConnectSequences": [ 3 | { 4 | "name": "Rich 4Clojure", 5 | "projectType": "deps.edn", 6 | "menuSelections": { 7 | "cljAliases": [ 8 | "repl" 9 | ] 10 | }, 11 | "afterCLJReplJackInCode": "(require 'repl)", 12 | "cljsType": "none" 13 | } 14 | ], 15 | "workbench.editorAssociations": { 16 | "INSTRUCTIONS.md": "vscode.markdown.preview.editor" 17 | } 18 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changes to rich4clojure 2 | 3 | ## [Unreleased] 4 | 5 | ## [0.0.5] - 2021-08-18 6 | 7 | * Add Zero-install option using Gitpod 8 | 9 | ## [0.0.4] - 2021-07-26 10 | 11 | * Zero-pad the problem number in filenames to make it easier to find problems in numeric order 12 | 13 | ## [0.0.3] - 2021-07-25 14 | 15 | * Tests translated using `rewrite-clj` (instead of `clojure.tools.reader`) 16 | 17 | ## [0.0.2] - 2021-07-21 18 | 19 | * First version with all original problems (still WIP) 20 | 21 | ## [0.0.1] - 2021-07-20 22 | 23 | * Placeholder release -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_013.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-013 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sequences: rest = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; The rest function will return all the items of a 9 | ;; sequence except the first. 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | __ := (rest [10 20 30 40])) 19 | 20 | ;; Share your solution, and/or check how others did it: 21 | ;; https://gist.github.com/e6a11835e69f77aaeae5bc7a3ef2a529 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_015.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-015 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Double Down = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Write a function which doubles a number. 9 | 10 | (def __ :tests-will-fail) 11 | 12 | (comment 13 | 14 | ) 15 | 16 | (tests 17 | (__ 2) := 4 18 | (__ 3) := 6 19 | (__ 11) := 22 20 | (__ 7) := 14) 21 | 22 | ;; Share your solution, and/or check how others did it: 23 | ;; https://gist.github.com/b00355c5b51c4e29e27a09b6ed2f7a73 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_126.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-126 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Through the Looking Class = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Easy 7 | ;; Tags: [fun brain-teaser] 8 | ;; 9 | ;; Enter a value which satisfies the following: 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | [x __] := 19 | (and (= (class x) x) x)) 20 | 21 | ;; Share your solution, and/or check how others did it: 22 | ;; https://gist.github.com/8b24bd0cf40098ec3875dde7ecf3fc5c -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_045.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-045 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Iterate = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; The iterate function can be used to produce an infinite 10 | ;; lazy sequence. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | __ := (take 5 (iterate #(+ 3 %) 1))) 20 | 21 | ;; Share your solution, and/or check how others did it: 22 | ;; https://gist.github.com/f1da413cb1110daf97898dbf73113894 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_042.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-042 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Factorial Fun = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Easy 7 | ;; Tags: [math] 8 | ;; 9 | ;; Write a function which calculates factorials. 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | (__ 1) := 1 19 | (__ 3) := 6 20 | (__ 5) := 120 21 | (__ 8) := 40320) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/850039024c0d503cce2b2e98ca1803c5 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_009.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-009 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sets: conj = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; When operating on a set, the conj function returns a 9 | ;; new set with one or more keys "added". 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | #{1 2 3 4} := (conj #{1 4 3} __)) 19 | 20 | ;; Share your solution, and/or check how others did it: 21 | ;; https://gist.github.com/ad6c54ca6259cd67274a902f2d0be1ff -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_037.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-037 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Regular Expressions = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; Tags: [regex syntax] 8 | ;; 9 | ;; Regex patterns are supported with a special reader tag. 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | __ := (apply str (re-seq #"[A-Z]+" "bA1B3Ce "))) 19 | 20 | ;; Share your solution, and/or check how others did it: 21 | ;; https://gist.github.com/48ee58ab81946ddf8038f739536b7249 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_051.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-051 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Advanced Destructuring = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [destructuring] 8 | ;; 9 | ;; Here is an example of some more sophisticated 10 | ;; destructuring. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | [1 2 [3 4 5] [1 2 3 4 5]] := (let [[a b & c :as d] __] [a b c d])) 20 | 21 | ;; Share your solution, and/or check how others did it: 22 | ;; https://gist.github.com/d4eb7ceb4ae05468135c920a39be2bf8 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_011.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-011 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Maps: conj = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; When operating on a map, the conj function returns a 9 | ;; new map with one or more key-value pairs "added". 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | {:a 1, :b 2, :c 3} := (conj {:a 1} __ [:c 3])) 19 | 20 | ;; Share your solution, and/or check how others did it: 21 | ;; https://gist.github.com/3a6532504180b1b65534b75a804d6f82 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_016.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-016 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Hello World = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Write a function which returns a personalized greeting. 9 | 10 | (def __ :tests-will-fail) 11 | 12 | (comment 13 | 14 | ) 15 | 16 | (tests 17 | (__ "Dave") := "Hello, Dave!" 18 | (__ "Jenn") := "Hello, Jenn!" 19 | (__ "Rhea") := "Hello, Rhea!") 20 | 21 | ;; Share your solution, and/or check how others did it: 22 | ;; https://gist.github.com/60e1accc59d5c5783365ab46d47eb78b -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_052.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-052 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Destructuring = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Elementary 7 | ;; Tags: [destructuring] 8 | ;; 9 | ;; Let bindings and function parameter lists support 10 | ;; destructuring. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | [2 4] := (let [[a b c d e] [0 1 2 3 4]] __)) 20 | 21 | ;; Share your solution, and/or check how others did it: 22 | ;; https://gist.github.com/631d47dbf5b3f286f54a74fd58bb0e00 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_067.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-067 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Prime Numbers = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [primes] 8 | ;; 9 | ;; Write a function which returns the first x number of 10 | ;; prime numbers. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ 2) := [2 3] 20 | (__ 5) := [2 3 5 7 11] 21 | (last (__ 100)) := 541) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/024453db620369a2d32894f7b0940462 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_066.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-066 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Greatest Common Divisor = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; 8 | ;; Given two integers, write a function which returns the 9 | ;; greatest common divisor. 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | (__ 2 4) := 2 19 | (__ 10 5) := 5 20 | (__ 5 7) := 1 21 | (__ 1023 858) := 33) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/4aa85a5a1d6e6962548e6dc69a02a55e -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_003.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-003 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Strings = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Clojure strings are Java strings. This means that you 9 | ;; can use any of the Java string methods on Clojure 10 | ;; strings. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | __ := (.toUpperCase "hello world")) 20 | 21 | ;; Share your solution, and/or check how others did it: 22 | ;; https://gist.github.com/a9e56d5eec902a5e74c4261f3ed3b0ed -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_008.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-008 2 | (:require [hyperfiddle.rcf :refer [tests]] 3 | [clojure.set])) 4 | 5 | ;; = Intro to Sets = 6 | ;; By 4Clojure user: dbyrne 7 | ;; Difficulty: Elementary 8 | ;; 9 | ;; Sets are collections of unique values. 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | __ := (set '(:a :a :b :c :c :c :c :d :d)) 19 | __ := (clojure.set/union #{:a :b :c} #{:b :c :d})) 20 | 21 | ;; Share your solution, and/or check how others did it: 22 | ;; https://gist.github.com/d1f6098dff344875902737842e15543f -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_026.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-026 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Fibonacci Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [Fibonacci seqs] 8 | ;; 9 | ;; Write a function which returns the first X fibonacci 10 | ;; numbers. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ 3) := '(1 1 2) 20 | (__ 6) := '(1 1 2 3 5 8) 21 | (__ 8) := '(1 1 2 3 5 8 13 21)) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/87153a8e55b56058703e5bca6f8ba62a -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_007.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-007 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Vectors: conj = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; When operating on a Vector, the conj function will 9 | ;; return a new vector with one or more items "added" to 10 | ;; the end. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | __ := (conj [1 2 3] 4) 20 | __ := (conj [1 2] 3 4)) 21 | 22 | ;; Share your solution, and/or check how others did it: 23 | ;; https://gist.github.com/5863d15b78b837e8cdfeb5e97e23d28b -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_020.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-020 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Penultimate Element = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which returns the second to last 10 | ;; element from a sequence. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ (list 1 2 3 4 5)) := 4 20 | (__ ["a" "b" "c"]) := "b" 21 | (__ [[1 2] [3 4]]) := [1 2]) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/bb564e188dc4d73aa37b714b64003dfe -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_014.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-014 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Functions = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Clojure has many different ways to create functions. 9 | 10 | (def __ :tests-will-fail) 11 | 12 | (comment 13 | 14 | ) 15 | 16 | (tests 17 | __ := ((fn add-five [x] (+ x 5)) 3) 18 | __ := ((fn [x] (+ x 5)) 3) 19 | __ := (#(+ % 5) 3) 20 | __ := ((partial + 5) 3)) 21 | 22 | ;; Share your solution, and/or check how others did it: 23 | ;; https://gist.github.com/08da0019981a566de6550e692c959a24 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_099.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-099 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Product Digits = 5 | ;; By 4Clojure user: jneira 6 | ;; Difficulty: Easy 7 | ;; Tags: [math seqs] 8 | ;; 9 | ;; Write a function which multiplies two numbers and 10 | ;; returns the result as a sequence of its digits. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ 1 1) := [1] 20 | (__ 99 9) := [8 9 1] 21 | (__ 999 99) := [9 8 9 0 1]) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/2a889ddd7c667b5b738f9a61d14f3958 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_019.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-019 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Last Element = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which returns the last element in a 10 | ;; sequence. 11 | 12 | (def restricted [last]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ [1 2 3 4 5]) := 5 22 | (__ '(5 4 3)) := 3 23 | (__ ["b" "c" "d"]) := "d") 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/374c499f3dad0203503b7dae16bf86f4 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_029.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-029 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Get the Caps = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [strings] 8 | ;; 9 | ;; Write a function which takes a string and returns a new 10 | ;; string containing only the capital letters. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ "HeLlO, WoRlD!") := "HLOWRD" 20 | (__ "nothing") := 21 | (__ "$#A(*&987Zf") := "AZ") 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/bdbcf005bcae10b15531ebe3a7d0be9c -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_024.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-024 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sum It All Up = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which returns the sum of a sequence of 10 | ;; numbers. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ [1 2 3]) := 6 20 | (__ (list 0 -2 5 5)) := 8 21 | (__ #{4 2 1}) := 7 22 | (__ '(0 0 -1)) := -1 23 | (__ '(1 10 3)) := 14) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/feb88ed305b672c9897330561da6b331 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_025.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-025 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Find the odd numbers = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which returns only the odd numbers 10 | ;; from a sequence. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ #{1 2 3 4 5}) := '(1 3 5) 20 | (__ [4 2 1 6]) := '(1) 21 | (__ [2 2 4 6]) := '() 22 | (__ [1 1 1 3]) := '(1 1 1 3)) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/5e9cf292d770f5e9dfabe8f4a9e10be3 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_041.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-041 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Drop Every Nth Item = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which drops every Nth item from a 10 | ;; sequence. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ [1 2 3 4 5 6 7 8] 3) := [1 2 4 5 7 8] 20 | (__ [:a :b :c :d :e :f] 2) := [:a :c :e] 21 | (__ [1 2 3 4 5 6] 4) := [1 2 3 5 6]) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/03788b118a3d7923f7aae143e8ef1aee -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_034.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-034 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Implement range = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which creates a list of all integers 10 | ;; in a given range. 11 | 12 | (def restricted [range]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ 1 4) := '(1 2 3) 22 | (__ -2 2) := '(-2 -1 0 1) 23 | (__ 5 8) := '(5 6 7)) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/6ebd843c6422d507efa327bee4bf0b50 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_035.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-035 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Local bindings = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Elementary 7 | ;; Tags: [syntax] 8 | ;; 9 | ;; Clojure lets you give local names to values using the 10 | ;; special let-form. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | __ := (let [x 5] (+ 2 x)) 20 | __ := (let [x 3, y 10] (- y x)) 21 | __ := (let [x 21] (let [y 3] (/ x y)))) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/b42371c75b1ec96c39aecb3c3c1a539f -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_028.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-028 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Flatten a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which flattens a sequence. 10 | 11 | (def restricted [flatten]) 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ '((1 2) 3 [4 [5 6]])) := '(1 2 3 4 5 6) 21 | (__ ["a" ["b"] "c"]) := '("a" "b" "c") 22 | (__ '((((:a))))) := '(:a)) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/0c6e3c48cac7434882ca4b2c71ebfce1 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_038.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-038 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Maximum value = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [core-functions] 8 | ;; 9 | ;; Write a function which takes a variable number of 10 | ;; parameters and returns the maximum value. 11 | 12 | (def restricted [max max-key]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ 1 8 3 4) := 8 22 | (__ 30 20) := 30 23 | (__ 45 67 11) := 67) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/3d8cce63160543ce69b40bc041174b28 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_012.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-012 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Sequences = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; All Clojure collections support sequencing. You can 9 | ;; operate on sequences with functions like first, second, 10 | ;; and last. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | __ := (first '(3 2 1)) 20 | __ := (second [2 3 4]) 21 | __ := (last (list 1 2 3))) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/f5d00ce2e066fb1487e3c4ffeb9b8848 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_023.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-023 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Reverse a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which reverses a sequence. 10 | 11 | (def restricted [reverse rseq]) 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ [1 2 3 4 5]) := [5 4 3 2 1] 21 | (__ (sorted-set 5 7 2 7)) := '(7 5 2) 22 | (__ [[1 2][3 4][5 6]]) := [[5 6][3 4][1 2]]) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/904085bff870b46beca9c51605e1b3fc -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_048.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-048 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to some = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; 8 | ;; The some function takes a predicate function and a 9 | ;; collection. It returns the first logical true value of 10 | ;; (predicate x) where x is an item in the collection. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | __ := (some #{2 7 6} [5 6 7 8]) 20 | __ := (some #(when (even? %) %) [5 6 7 8])) 21 | 22 | ;; Share your solution, and/or check how others did it: 23 | ;; https://gist.github.com/e8d9e038324cff43641994d657d9ec93 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_057.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-057 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Simple Recursion = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; Tags: [recursion] 8 | ;; 9 | ;; A recursive function is a function which calls itself. 10 | ;; This is one of the fundamental techniques used in 11 | ;; functional programming. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | __ := ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5)) 21 | 22 | ;; Share your solution, and/or check how others did it: 23 | ;; https://gist.github.com/c9b7a459e83135bcb56a2a1148e87216 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_161.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-161 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Subset and Superset = 5 | ;; By 4Clojure user: hangkous 6 | ;; Difficulty: Elementary 7 | ;; Tags: [set-theory] 8 | ;; 9 | ;; Set A is a subset of set B, or equivalently B is a 10 | ;; superset of A, if A is "contained" inside B. A and B 11 | ;; may coincide. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | __ := #{2} 21 | #{1} := __ 22 | __ := #{1 2} 23 | #{1 2} := __) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/fb5992733dcee8d4e449957ed7ece7db -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_030.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-030 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Compress a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which removes consecutive duplicates 10 | ;; from a sequence. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (apply str (__ "Leeeeeerrroyyy")) := "Leroy" 20 | (__ [1 1 2 3 3 2 2 3]) := '(1 2 3 2 3) 21 | (__ [[1 2] [1 2] [3 4] [1 2]]) := '([1 2] [3 4] [1 2])) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/92a4bd13aaa6bffb80d7724de2c8e64d -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_004.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-004 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Lists = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Lists can be constructed with either a function or a 9 | ;; quoted form. 10 | ;; 11 | ;; Note: You can't redefine `__` to solve this problem. 12 | ;; You will need to replace `__` in the `(tests ...)` 13 | ;; form. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (list __) := '(:a :b :c)) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/447fba2735f190ec4786a4b2cf7d5b76 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_018.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-018 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sequences: filter = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; The filter function takes two arguments: a predicate 9 | ;; function (f) and a sequence (s). Filter returns a new 10 | ;; sequence consisting of all the items of s for which (f 11 | ;; item) returns true. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | __ := (filter #(> % 5) '(3 4 5 6 7))) 21 | 22 | ;; Share your solution, and/or check how others did it: 23 | ;; https://gist.github.com/5f4df173b9c2f1075262f0f821d83ab0 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_021.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-021 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Nth Element = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which returns the Nth element from a 10 | ;; sequence. 11 | 12 | (def restricted [nth]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ '(4 5 6 7) 2) := 6 22 | (__ [:a :b :c] 0) := :a 23 | (__ [1 2 3 4] 1) := 2 24 | (__ '([1 2] [3 4] [5 6]) 2) := [5 6]) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/327178a78d8d0b021a72c75b0876a225 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_031.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-031 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Pack a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which packs consecutive duplicates 10 | ;; into sub-lists. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ [1 1 2 1 1 1 3 3]) := '((1 1) (2) (1 1 1) (3 3)) 20 | (__ [:a :a :b :b :c]) := '((:a :a) (:b :b) (:c)) 21 | (__ [[1 2] [1 2] [3 4]]) := '(([1 2] [1 2]) ([3 4]))) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/ded02d5ee478eec231bacd671a08b606 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_134.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-134 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = A nil key = 5 | ;; By 4Clojure user: goranjovic 6 | ;; Difficulty: Elementary 7 | ;; Tags: [maps] 8 | ;; 9 | ;; Write a function which, given a key and map, returns 10 | ;; true iff the map contains an entry with that key and 11 | ;; its value is nil. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ :a {:a nil :b 2}) := 21 | (__ :b {:a nil :b 2}) := 22 | (__ :c {:a nil :b 2}) :=) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/70ba70e1e4e0f249a90bc99bda5f8aff -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_010.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-010 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Maps = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Maps store key-value pairs. Both maps and keywords can 9 | ;; be used as lookup functions. Commas can be used to make 10 | ;; maps more readable, but they are not required. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | __ := ((hash-map :a 10, :b 20, :c 30) :b) 20 | __ := (:b {:a 10, :b 20, :c 30})) 21 | 22 | ;; Share your solution, and/or check how others did it: 23 | ;; https://gist.github.com/cc20ba916a1f806dccb489d01ea6037b -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_046.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-046 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Flipping out = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [higher-order-functions] 8 | ;; 9 | ;; Write a higher-order function which flips the order of 10 | ;; the arguments of an input function. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | 3 := ((__ nth) 2 [1 2 3 4 5]) 20 | true := ((__ >) 7 8) 21 | 4 := ((__ quot) 2 8) 22 | [1 2 3] := ((__ take) [1 2 3 4 5] 3)) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/ab7a1d0a03feaaa8189a5ab386ad216c -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_074.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-074 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Filter Perfect Squares = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; 8 | ;; Given a string of comma separated integers, write a 9 | ;; function which returns a new comma separated string 10 | ;; that only contains the numbers which are perfect 11 | ;; squares. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ "4,5,6,7,8,9") := "4,9" 21 | (__ "15,16,25,36,37") := "16,25,36") 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/b90fcac09b35c74a07228dea603ddc73 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_002.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-002 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Simple Math = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; If you are not familiar with polish (prefix) notation, 9 | ;; simple arithmetic might seem confusing. 10 | ;; 11 | ;; Note: Enter only enough to fill in the blank (in this 12 | ;; case, a single number) - do not retype the whole 13 | ;; problem. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (- 10 (* 2 3)) := __) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/0431a32b7520fbb82264004059a32d90 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_043.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-043 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Reverse Interleave = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which reverses the interleave process 10 | ;; into x number of subsequences. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ [1 2 3 4 5 6] 2) := '((1 3 5) (2 4 6)) 20 | (__ (range 9) 3) := '((0 3 6) (1 4 7) (2 5 8)) 21 | (__ (range 10) 5) := '((0 5) (1 6) (2 7) (3 8) (4 9))) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/22a521dbd7918006dd82f9617e7a92a7 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_143.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-143 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = dot product = 5 | ;; By 4Clojure user: bloop 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs math] 8 | ;; 9 | ;; Create a function that computes the dot product of two 10 | ;; sequences. You may assume that the vectors will have 11 | ;; the same length. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | 0 := (__ [0 1 0] [1 0 0]) 21 | 3 := (__ [1 1 1] [1 1 1]) 22 | 32 := (__ [1 2 3] [4 5 6]) 23 | 256 := (__ [2 5 6] [100 10 1])) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/9b1601d03554162866074ac2fd15f4de -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_032.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-032 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Duplicate a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which duplicates each element of a 10 | ;; sequence. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ [1 2 3]) := '(1 1 2 2 3 3) 20 | (__ [:a :a :b :b]) := '(:a :a :a :a :b :b :b :b) 21 | (__ [[1 2] [3 4]]) := '([1 2] [1 2] [3 4] [3 4]) 22 | (__ [[1 2] [3 4]]) := '([1 2] [1 2] [3 4] [3 4])) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/9e936a6097fdb1fd7a8418a22e3e1170 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_049.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-049 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Split a sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which will split a sequence into two 10 | ;; parts. 11 | 12 | (def restricted [split-at]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ 3 [1 2 3 4 5 6]) := [[1 2 3] [4 5 6]] 22 | (__ 1 [:a :b :c :d]) := [[:a] [:b :c :d]] 23 | (__ 2 [[1 2] [3 4] [5 6]]) := [[[1 2] [3 4]] [[5 6]]]) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/27721e1df66462290127eb4a0775915a -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_157.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-157 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Indexing Sequences = 5 | ;; By 4Clojure user: jaycfields 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Transform a sequence into a sequence of pairs 10 | ;; containing the original elements along with their 11 | ;; index. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ [:a :b :c]) := [[:a 0] [:b 1] [:c 2]] 21 | (__ [0 1 3]) := '((0 0) (1 1) (3 2)) 22 | (__ [[:foo] {:bar :baz}]) := [[[:foo] 0] [{:bar :baz} 1]]) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/efa257369fd08ba1dcef6bec34d04ede -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_017.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-017 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sequences: map = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; The map function takes two arguments: a function (f) 9 | ;; and a sequence (s). Map returns a new sequence 10 | ;; consisting of the result of applying f to each item of 11 | ;; s. Do not confuse the map function with the map data 12 | ;; structure. 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | __ := (map #(+ % 5) '(1 2 3))) 22 | 23 | ;; Share your solution, and/or check how others did it: 24 | ;; https://gist.github.com/39980dc3c5893db22fe13359c3f8c72c -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_022.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-022 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Count a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which returns the total number of 10 | ;; elements in a sequence. 11 | 12 | (def restricted [count]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ '(1 2 3 3 1)) := 5 22 | (__ "Hello World") := 11 23 | (__ [[1 2] [3 4] [5 6]]) := 3 24 | (__ '(13)) := 1 25 | (__ '(:a :b :c)) := 3) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/d55eddc37d7a08a3440748ddb75c7ec4 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_100.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-100 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Least Common Multiple = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [math] 8 | ;; 9 | ;; Write a function which calculates the least common 10 | ;; multiple. Your function should accept a variable number 11 | ;; of positive integers or ratios. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ 2 3) := 6 21 | (__ 5 3 7) := 105 22 | (__ 1/3 2/5) := 2 23 | (__ 3/4 1/6) := 3/2 24 | (__ 7 5/7 2 3/5) := 210) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/f9876ddb08600e7c90e1647e14efeeae -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_122.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-122 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Read a binary number = 5 | ;; By 4Clojure user: mikera 6 | ;; Difficulty: Easy 7 | ;; 8 | ;; Convert a binary number, provided in the form of a 9 | ;; string, to its numerical value. 10 | 11 | (def __ :tests-will-fail) 12 | 13 | (comment 14 | 15 | ) 16 | 17 | (tests 18 | 0 := (__ "0") 19 | 7 := (__ "111") 20 | 8 := (__ "1000") 21 | 9 := (__ "1001") 22 | 255 := (__ "11111111") 23 | 1365 := (__ "10101010101") 24 | 65535 := (__ "1111111111111111")) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/561d7a007e4aa32a0c66655ef9e802c7 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_006.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-006 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Vectors = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Vectors can be constructed several ways. You can 9 | ;; compare them with lists. 10 | ;; 11 | ;; Note: You can't redefine `__` to solve this problem. 12 | ;; You will need to replace `__` in the `(tests ...)` 13 | ;; form. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | [__] := (list :a :b :c) (vec '(:a :b :c)) (vector :a :b :c)) 23 | 24 | ;; Share your solution, and/or check how others did it: 25 | ;; https://gist.github.com/82981313411b066055f5a1d1a1e10037 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_044.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-044 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Rotate Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which can rotate a sequence in either 10 | ;; direction. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ 2 [1 2 3 4 5]) := '(3 4 5 1 2) 20 | (__ -2 [1 2 3 4 5]) := '(4 5 1 2 3) 21 | (__ 6 [1 2 3 4 5]) := '(2 3 4 5 1) 22 | (__ 1 '(:a :b :c)) := '(:b :c :a) 23 | (__ -4 '(:a :b :c)) := '(:c :a :b)) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/2a419f3d9e0c22be01179175f6a7d2f0 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_036.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-036 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Let it Be = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Elementary 7 | ;; Tags: [math syntax] 8 | ;; 9 | ;; Can you bind x, y, and z so that these are all true? 10 | ;; 11 | ;; Note: You can't redefine `__` to solve this problem. 12 | ;; You will need to replace `__` in the `(tests ...)` 13 | ;; form. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | 10 := (let __ (+ x y)) 23 | 4 := (let __ (+ y z)) 24 | 1 := (let __ z)) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/ed751b2bcfa34398e03ea9f77a4d12b6 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_047.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-047 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Contain Yourself = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; 8 | ;; The contains? function checks if a KEY is present in a 9 | ;; given collection. This often leads beginner clojurians 10 | ;; to use it incorrectly with numerically indexed 11 | ;; collections like vectors and lists. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | #{4 5 6} := __ 21 | [1 1 1 1 1] := __ 22 | {4 :a 2 :b} := __ 23 | (contains? [1 2 4] __) :=) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/cbcede67c0c350f7d800b078a5c0f525 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_040.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-040 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Interpose a Seq = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which separates the items of a 10 | ;; sequence by an arbitrary value. 11 | 12 | (def restricted [interpose]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ 0 [1 2 3]) := [1 0 2 0 3] 22 | (apply str (__ ", " ["one" "two" "three"])) := "one, two, three" 23 | (__ :z [:a :b :c :d]) := [:a :z :b :z :c :z :d]) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/4f5ffbf020d90a968db4f99478cf358e -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_080.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-080 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Perfect Numbers = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; 8 | ;; A number is "perfect" if the sum of its divisors equal 9 | ;; the number itself. 6 is a perfect number because 10 | ;; 1+2+3=6. Write a function which returns true for 11 | ;; perfect numbers and false otherwise. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ 6) := true 21 | (__ 7) := false 22 | (__ 496) := true 23 | (__ 500) := false 24 | (__ 8128) := true) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/349c937f940951b1ec6972b98834f185 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_033.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-033 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Replicate a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which replicates each element of a 10 | ;; sequence a variable number of times. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ [1 2 3] 2) := '(1 1 2 2 3 3) 20 | (__ [:a :b] 4) := '(:a :a :a :a :b :b :b :b) 21 | (__ [4 5 6] 1) := '(4 5 6) 22 | (__ [[1 2] [3 4]] 2) := '([1 2] [1 2] [3 4] [3 4]) 23 | (__ [44 33] 2) := [44 44 33 33]) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/3e4dc6bf1479a57ffcd2ef05bdc5bb52 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_005.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-005 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Lists: conj = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; When operating on a list, the conj function will return 9 | ;; a new list with one or more items "added" to the front. 10 | ;; 11 | ;; Note that there are two test cases, but you are 12 | ;; expected to supply only one answer, which will cause 13 | ;; all the tests to pass. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | __ := (conj '(2 3 4) 1) 23 | __ := (conj '(3 4) 2 1)) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/efe2f5aa74f08899ea993bdfaab8da59 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_081.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-081 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Set Intersection = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [set-theory] 8 | ;; 9 | ;; Write a function which returns the intersection of two 10 | ;; sets. The intersection is the sub-set of items that 11 | ;; each set has in common. 12 | 13 | (def restricted [intersection]) 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ #{0 1 2 3} #{2 3 4 5}) := #{2 3} 23 | (__ #{0 1 2} #{3 4 5}) := #{} 24 | (__ #{:a :b :c :d} #{:c :e :a :f :d}) := #{:a :c :d}) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/a51be488815f09d5d8e68e93d16f61e8 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_055.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-055 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Count Occurrences = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which returns a map containing the 10 | ;; number of occurrences of each distinct item in a 11 | ;; sequence. 12 | 13 | (def restricted [frequencies]) 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ [1 1 2 3 2 1 1]) := {1 4, 2 2, 3 1} 23 | (__ [:b :a :b :a :b]) := {:a 2, :b 3} 24 | (__ '([1 2] [1 3] [1 3])) := {[1 2] 1, [1 3] 2}) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/b1a32d924734a9c4b6e562363ec8b635 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_027.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-027 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Palindrome Detector = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which returns true if the given 10 | ;; sequence is a palindrome. 11 | ;; 12 | ;; 13 | ;; Hint: "racecar" does not equal '(\r \a \c \e \c \a \r) 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ '(1 2 3 4 5)) := false 23 | (__ "racecar") := true 24 | (__ [:foo :bar :foo]) := true 25 | (__ '(1 1 3 3 1 1)) := true 26 | (__ '(:a :b :c)) := false) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/a9620760aad9da40c497f5750087a095 30 | -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_116.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-116 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Prime Sandwich = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Medium 7 | ;; Tags: [math] 8 | ;; 9 | ;; A balanced prime is a prime number which is also the 10 | ;; mean of the primes directly before and after it in the 11 | ;; sequence of valid primes. Create a function which takes 12 | ;; an integer n, and returns true iff it is a balanced 13 | ;; prime. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | false := (__ 4) 23 | true := (__ 563) 24 | 1103 := (nth (filter __ (range)) 15)) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/286f071c259fb6861c10beb7411bde48 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_061.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-061 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Map Construction = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [core-functions] 8 | ;; 9 | ;; Write a function which takes a vector of keys and a 10 | ;; vector of values and constructs a map from them. 11 | 12 | (def restricted [zipmap]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ [:a :b :c] [1 2 3]) := {:a 1, :b 2, :c 3} 22 | (__ [1 2 3 4] ["one" "two" "three"]) := {1 "one", 2 "two", 3 "three"} 23 | (__ [:foo :bar] ["foo" "bar" "baz"]) := {:foo "foo", :bar "bar"}) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/e9ba5a581234d1bddbae5caa2e51aa51 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_162.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-162 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Logical falsity and truth = 5 | ;; By 4Clojure user: hangkous 6 | ;; Difficulty: Elementary 7 | ;; Tags: [logic] 8 | ;; 9 | ;; In Clojure, only nil and false represent the values of 10 | ;; logical falsity in conditional tests - anything else is 11 | ;; logical truth. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | __ := (if-not false 1 0) 21 | __ := (if-not nil 1 0) 22 | __ := (if true 1 0) 23 | __ := (if [] 1 0) 24 | __ := (if [0] 1 0) 25 | __ := (if 0 1 0) 26 | __ := (if 1 1 0)) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/ef5021f621afad511d1a4b5505fc231f -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_056.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-056 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Find Distinct Items = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which removes the duplicates from a 10 | ;; sequence. Order of the items must be maintained. 11 | 12 | (def restricted [distinct]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ [1 2 1 3 1 2 4]) := [1 2 3 4] 22 | (__ [:a :a :b :b :c :c]) := [:a :b :c] 23 | (__ '([2 4] [1 2] [1 3] [1 3])) := '([2 4] [1 2] [1 3]) 24 | (__ (range 50)) := (range 50)) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/a509841669465f47ccd96fe847387b3e -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_054.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-054 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Partition a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which returns a sequence of lists of x 10 | ;; items each. Lists of less than x items should not be 11 | ;; returned. 12 | 13 | (def restricted [partition partition-all]) 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ 3 (range 9)) := '((0 1 2) (3 4 5) (6 7 8)) 23 | (__ 2 (range 8)) := '((0 1) (2 3) (4 5) (6 7)) 24 | (__ 3 (range 8)) := '((0 1 2) (3 4 5))) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/1da137c7927d083dfbb4db1686a3e3cf -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_173.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-173 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Destructuring 2 = 5 | ;; By 4Clojure user: hangkous 6 | ;; Difficulty: Easy 7 | ;; Tags: [Destructuring] 8 | ;; 9 | ;; Sequential destructuring allows you to bind symbols to 10 | ;; parts of sequential things (vectors, lists, seqs, 11 | ;; etc.): (let [bindings*] exprs*) 12 | ;; 13 | ;; Complete the bindings so all let-parts evaluate to 3. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | 3 := 23 | (let [[__] [+ (range 3)]] (apply __)) 24 | (let [[[__] b] [[+ 1] 2]] (__ b)) 25 | (let [[__] [inc 2]] (__))) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/3b66d5ad0242a7fbbb163a14c2df96f2 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_088.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-088 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Symmetric Difference = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [set-theory] 8 | ;; 9 | ;; Write a function which returns the symmetric difference 10 | ;; of two sets. The symmetric difference is the set of 11 | ;; items belonging to one but not both of the two sets. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ #{1 2 3 4 5 6} #{1 3 5 7}) := #{2 4 6 7} 21 | (__ #{:a :b :c} #{}) := #{:a :b :c} 22 | (__ #{} #{4 5 6}) := #{4 5 6} 23 | (__ #{[1 2] [2 3]} #{[2 3] [3 4]}) := #{[1 2] [3 4]}) 24 | 25 | ;; Share your solution, and/or check how others did it: 26 | ;; https://gist.github.com/b78bc7de41bb6cef6ca18c1e924bb5ac -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_039.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-039 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Interleave Two Seqs = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which takes two sequences and returns 10 | ;; the first item from each, then the second item from 11 | ;; each, then the third, etc. 12 | 13 | (def restricted [interleave]) 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ [1 2 3] [:a :b :c]) := '(1 :a 2 :b 3 :c) 23 | (__ [1 2] [3 4 5 6]) := '(1 3 2 4) 24 | (__ [1 2 3 4] [5]) := [1 5] 25 | (__ [30 20] [25 15]) := [30 25 20 15]) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/65d3ee0ffa567e78927bbebbb9d9cc89 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_001.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-001 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Nothing but the Truth = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; Define `__` or replace the `__` in the `(tests ...)` 9 | ;; form with a value which will make the test pass. 10 | ;; 11 | ;; Run the test by loading the file. Once the file is 12 | ;; loaded you can run the tests again by either reloading 13 | ;; the file or evaluating the `(tests ...)` form. 14 | ;; 15 | ;; Don't over think it! Hint: true is equal to true. 16 | 17 | (def __ :tests-will-fail) 18 | 19 | (comment 20 | 21 | ) 22 | 23 | (tests 24 | __ := true) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/417aa01f5c5bb4dc3b1c0fea8d48ae88 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_085.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-085 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Power Set = 5 | ;; By 4Clojure user: peteris 6 | ;; Difficulty: Medium 7 | ;; Tags: [set-theory] 8 | ;; 9 | ;; Write a function which generates the power set of a 10 | ;; given set. The power set of a set x is the set of all 11 | ;; subsets of x, including the empty set and x itself. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ #{1 :a}) := #{#{1 :a} #{:a} #{} #{1}} 21 | (__ #{}) := #{#{}} 22 | (__ #{1 2 3}) := 23 | #{#{} #{1} #{2} #{3} #{1 2} #{1 3} #{2 3} #{1 2 3}} 24 | (count (__ (into #{} (range 10)))) := 1024) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/ace16b73b8ec73ae84d8c83ceff9e568 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_062.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-062 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Re-implement Iterate = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Given a side-effect free function f and an initial 10 | ;; value x write a function which returns an infinite lazy 11 | ;; sequence of x, (f x), (f (f x)), (f (f (f x))), etc. 12 | 13 | (def restricted [iterate]) 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (take 5 (__ #(* 2 %) 1)) := [1 2 4 8 16] 23 | (take 100 (__ inc 0)) := (take 100 (range)) 24 | (take 9 (__ #(inc (mod % 3)) 1)) := (take 9 (cycle [1 2 3]))) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/8d8c071f4df40d8671599b0882174b44 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_075.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-075 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Euler's Totient Function = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; 8 | ;; Two numbers are coprime if their greatest common 9 | ;; divisor equals 1. Euler's totient function f(x) is 10 | ;; defined as the number of positive integers less than x 11 | ;; which are coprime to x. The special case f(1) equals 1. 12 | ;; Write a function which calculates Euler's totient 13 | ;; function. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ 1) := 1 23 | (__ 10) := (count '(1 3 7 9)) 4 24 | (__ 40) := 16 25 | (__ 99) := 60) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/da5e5c50f14f015708f967e20b450874 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_104.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-104 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Write Roman Numerals = 5 | ;; By 4Clojure user: 0x89 6 | ;; Difficulty: Medium 7 | ;; Tags: [strings math] 8 | ;; 9 | ;; This is the inverse of Problem 92, but much easier. 10 | ;; Given an integer smaller than 4000, return the 11 | ;; corresponding roman numeral in uppercase, adhering to 12 | ;; the subtractive principle . 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | "I" := (__ 1) 22 | "XXX" := (__ 30) 23 | "IV" := (__ 4) 24 | "CXL" := (__ 140) 25 | "DCCCXXVII" := (__ 827) 26 | "MMMCMXCIX" := (__ 3999) 27 | "XLVIII" := (__ 48)) 28 | 29 | ;; Share your solution, and/or check how others did it: 30 | ;; https://gist.github.com/99336a4cfdbb80b30d640f4b51134a42 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_105.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-105 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Identify keys and values = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Medium 7 | ;; Tags: [maps seqs] 8 | ;; 9 | ;; Given an input sequence of keywords and numbers, create 10 | ;; a map such that each key in the map is a keyword, and 11 | ;; the value is a sequence of all the numbers (if any) 12 | ;; between it and the next keyword in the sequence. 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | {} := (__ []) 22 | {:a [1]} := (__ [:a 1]) 23 | {:a [1], :b [2]} := (__ [:a 1, :b 2]) 24 | {:a [1 2 3], :b [], :c [4]} := (__ [:a 1 2 3 :b :c 4])) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/da9a4c4197dc581cb9635fe8358bc62d -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_083.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-083 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = A Half-Truth = 5 | ;; By 4Clojure user: cmeier 6 | ;; Difficulty: Easy 7 | ;; 8 | ;; Write a function which takes a variable number of 9 | ;; booleans. Your function should return true if some of 10 | ;; the parameters are true, but not all of the parameters 11 | ;; are true. Otherwise your function should return false. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | false := (__ false false) 21 | true := (__ true false) 22 | false := (__ true) 23 | true := (__ false true false) 24 | false := (__ true true true) 25 | true := (__ true true true false)) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/7fa7142a58af04b16b35d31ab00248a6 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_053.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-053 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Longest Increasing Sub-Seq = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Hard 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Given a vector of integers, find the longest 10 | ;; consecutive sub-sequence of increasing numbers. If two 11 | ;; sub-sequences have the same length, use the one that 12 | ;; occurs first. An increasing sub-sequence must have a 13 | ;; length of 2 or greater to qualify. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ [1 0 1 2 3 0 4 5]) := [0 1 2 3] 23 | (__ [5 6 1 3 2 7]) := [5 6] 24 | (__ [2 3 3 4 5]) := [3 4 5] 25 | (__ [7 6 5 4]) := []) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/2cd6e7158b0ea3d24d125c997a0f8d1e -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_125.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-125 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Gus' Quinundrum = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Hard 7 | ;; Tags: [logic fun brain-teaser] 8 | ;; 9 | ;; Create a function of no arguments which returns a 10 | ;; string that is an exact copy of the function itself. 11 | ;; 12 | ;; 13 | ;; Hint: read this if you get stuck (this question is 14 | ;; harder than it first appears); but it's worth the 15 | ;; effort to solve it independently if you can! 16 | ;; 17 | ;; 18 | ;; Fun fact: Gus is the name of the 4Clojure dragon . 19 | 20 | (def __ :tests-will-fail) 21 | 22 | (comment 23 | 24 | ) 25 | 26 | (tests 27 | (str '__) := (__)) 28 | 29 | ;; Share your solution, and/or check how others did it: 30 | ;; https://gist.github.com/d3ec1d923933de20ce7ba66afe6a8d45 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_070.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-070 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Word Sorting = 5 | ;; By 4Clojure user: fotland 6 | ;; Difficulty: Medium 7 | ;; Tags: [sorting] 8 | ;; 9 | ;; Write a function that splits a sentence up into a 10 | ;; sorted list of words. Capitalization should not affect 11 | ;; sort order and punctuation should be ignored. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | (__ "Have a nice day.") := 21 | ["a" "day" "Have" "nice"] 22 | (__ "Clojure is a fun language!") := 23 | ["a" "Clojure" "fun" "is" "language"] 24 | (__ "Fools fall for foolish follies.") := 25 | ["fall" "follies" "foolish" "Fools" "for"]) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/ede095348d09c012bb17854de2c26690 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_120.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-120 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sum of square of digits = 5 | ;; By 4Clojure user: danilo 6 | ;; Difficulty: Easy 7 | ;; Tags: [math] 8 | ;; 9 | ;; Write a function which takes a collection of integers 10 | ;; as an argument. Return the count of how many elements 11 | ;; are smaller than the sum of their squared component 12 | ;; digits. For example: 10 is larger than 1 squared plus 0 13 | ;; squared; whereas 15 is smaller than 1 squared plus 5 14 | ;; squared. 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | 8 := (__ (range 10)) 24 | 19 := (__ (range 30)) 25 | 50 := (__ (range 100)) 26 | 50 := (__ (range 1000))) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/80b83817f6c63ee546a5d05e0972a7ea -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_060.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-060 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sequence Reductions = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function which behaves like reduce, but returns 10 | ;; each intermediate value of the reduction. Your function 11 | ;; must accept either two or three arguments, and the 12 | ;; return sequence must be lazy. 13 | 14 | (def restricted [reductions]) 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | (take 5 (__ + (range))) := [0 1 3 6 10] 24 | (__ conj [1] [2 3 4]) := [[1] [1 2] [1 2 3] [1 2 3 4]] 25 | (last (__ * 2 [3 4 5])) := (reduce * 2 [3 4 5]) 120) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/4688fc26154649a2735f14264938fa3b -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_102.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-102 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = intoCamelCase = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Medium 7 | ;; Tags: [strings] 8 | ;; 9 | ;; When working with java, you often need to create an 10 | ;; object with fieldsLikeThis, but you'd rather work with 11 | ;; a hashmap that has :keys-like-this until it's time to 12 | ;; convert. Write a function which takes lower-case 13 | ;; hyphen-separated strings and converts them to 14 | ;; camel-case strings. 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | (__ "something") := "something" 24 | (__ "multi-word-key") := "multiWordKey" 25 | (__ "leaveMeAlone") := "leaveMeAlone") 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/14f3b466feeed4d7c0e456762c042aa6 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_144.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-144 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Oscilrate = 5 | ;; By 4Clojure user: bloop 6 | ;; Difficulty: Medium 7 | ;; Tags: [sequences] 8 | ;; 9 | ;; Write an oscillating iterate: a function that takes an 10 | ;; initial value and a variable number of functions. It 11 | ;; should return a lazy sequence of the functions applied 12 | ;; to the value in order, restarting from the first 13 | ;; function after it hits the end. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (take 3 (__ 3.14 int double)) := [3.14 3 3.0] 23 | (take 5 (__ 3 #(- % 3) #(+ 5 %))) := [3 0 5 2 7] 24 | (take 12 (__ 0 inc dec inc dec inc)) := [0 1 0 1 0 1 2 1 2 1 2 3]) 25 | 26 | ;; Share your solution, and/or check how others did it: 27 | ;; https://gist.github.com/91327b30de4e44c5f3b17d0c17a50e3e -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_059.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-059 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Juxtaposition = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [higher-order-functions core-functions] 8 | ;; 9 | ;; Take a set of functions and return a new function that 10 | ;; takes a variable number of arguments and returns a 11 | ;; sequence containing the result of applying each 12 | ;; function left-to-right to the argument list. 13 | 14 | (def restricted [juxt]) 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | [21 6 1] := ((__ + max min) 2 3 5 1 6 4) 24 | ["HELLO" 5] := ((__ #(.toUpperCase %) count) "hello") 25 | [2 6 4] := ((__ :a :c :b) {:a 2, :b 4, :c 6, :d 8 :e 10})) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/a986cefdc820ca22996a1c74948785d2 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_118.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-118 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Re-implement Map = 5 | ;; By 4Clojure user: semisight 6 | ;; Difficulty: Easy 7 | ;; Tags: [core-seqs] 8 | ;; 9 | ;; Map is one of the core elements of a functional 10 | ;; programming language. Given a function f and an input 11 | ;; sequence s, return a lazy sequence of (f x) for each 12 | ;; element x in s . 13 | 14 | (def restricted [map map-indexed mapcat for]) 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | [3 4 5 6 7] := 24 | (__ inc [2 3 4 5 6]) 25 | (repeat 10 nil) := 26 | (__ (fn [_] nil) (range 10)) 27 | [1000000 1000001] := 28 | (->> (__ inc (range)) 29 | (drop (dec 1000000)) 30 | (take 2))) 31 | 32 | ;; Share your solution, and/or check how others did it: 33 | ;; https://gist.github.com/fcd991cd17eb81a7e9a42d84a0cda89f -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_093.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-093 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Partially Flatten a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which flattens any nested combination 10 | ;; of sequential things (lists, vectors, etc.), but 11 | ;; maintains the lowest level sequential items. The result 12 | ;; should be a sequence of sequences with only one level 13 | ;; of nesting. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ [["Do"] ["Nothing"]]) := 23 | [["Do"] ["Nothing"]] 24 | (__ [[[[:a :b]]] [[:c :d]] [:e :f]]) := 25 | [[:a :b] [:c :d] [:e :f]] 26 | (__ '((1 2)((3 4)((((5 6))))))) := 27 | '((1 2)(3 4)(5 6))) 28 | 29 | ;; Share your solution, and/or check how others did it: 30 | ;; https://gist.github.com/9510d5981c9fc900634ab838fad7db5e -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_115.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-115 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = The Balance of N = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Medium 7 | ;; Tags: [math] 8 | ;; 9 | ;; A balanced number is one whose component digits have 10 | ;; the same sum on the left and right halves of the 11 | ;; number. Write a function which accepts an integer n, 12 | ;; and returns true iff n is balanced. 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | true := (__ 11) 22 | true := (__ 121) 23 | false := (__ 123) 24 | true := (__ 0) 25 | false := (__ 88099) 26 | true := (__ 89098) 27 | true := (__ 89089) 28 | (take 20 (filter __ (range))) := 29 | [0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101]) 30 | 31 | ;; Share your solution, and/or check how others did it: 32 | ;; https://gist.github.com/e4305e689ab891bd802f6292e01c1ad8 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_171.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-171 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intervals = 5 | ;; By 4Clojure user: aiba 6 | ;; Difficulty: Medium 7 | ;; 8 | ;; Write a function that takes a sequence of integers and 9 | ;; returns a sequence of "intervals". Each interval is a a 10 | ;; vector of two integers, start and end, such that all 11 | ;; integers between start and end (inclusive) are 12 | ;; contained in the input sequence. 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ [1 2 3]) := [[1 3]] 22 | (__ [10 9 8 1 2 3]) := [[1 3] [8 10]] 23 | (__ [1 1 1 1 1 1 1]) := [[1 1]] 24 | (__ []) := [] 25 | (__ [19 4 17 1 3 10 2 13 13 2 16 4 2 15 13 9 6 14 2 11]) := 26 | [[1 4] [6 6] [9 11] [13 17] [19 19]]) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/d795ad3f1a7bf7867cb9fde2b0e464fb -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_058.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-058 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Function Composition = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [higher-order-functions core-functions] 8 | ;; 9 | ;; Write a function which allows you to create function 10 | ;; compositions. The parameter list should take a variable 11 | ;; number of functions, and create a function that applies 12 | ;; them from right-to-left. 13 | 14 | (def restricted [comp]) 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | [3 2 1] := ((__ rest reverse) [1 2 3 4]) 24 | 5 := ((__ (partial + 3) second) [1 2 3 4]) 25 | true := ((__ zero? #(mod % 8) +) 3 5 7 9) 26 | "HELLO" := ((__ #(.toUpperCase %) #(apply str %) take) 5 "hello world")) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/ecb209948f280eeb565745dce17937f4 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_068.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-068 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Recurring Theme = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Elementary 7 | ;; Tags: [recursion] 8 | ;; 9 | ;; Clojure only has one non-stack-consuming looping 10 | ;; construct: recur. Either a function or a loop can be 11 | ;; used as the recursion point. Either way, recur rebinds 12 | ;; the bindings of the recursion point to the values it is 13 | ;; passed. Recur must be called from the tail-position, 14 | ;; and calling it elsewhere will result in an error. 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | __ := 24 | (loop [x 5 25 | result []] 26 | (if (> x 0) 27 | (recur (dec x) (conj result (+ 2 x))) 28 | result))) 29 | 30 | ;; Share your solution, and/or check how others did it: 31 | ;; https://gist.github.com/9a8b99903466666209d3edbe4288ed70 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_106.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-106 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Number Maze = 5 | ;; By 4Clojure user: lucas1000001 6 | ;; Difficulty: Hard 7 | ;; Tags: [numbers] 8 | ;; 9 | ;; Given a pair of numbers, the start and end point, find 10 | ;; a path between the two using only three possible 11 | ;; operations: 12 | ;; * double 13 | ;; * halve (odd numbers cannot be halved) 14 | ;; * add 2 15 | ;; 16 | ;; Find the shortest path through the "maze". Because 17 | ;; there are multiple shortest paths, you must return the 18 | ;; length of the shortest path, not the path itself. 19 | 20 | (def __ :tests-will-fail) 21 | 22 | (comment 23 | 24 | ) 25 | 26 | (tests 27 | 1 := (__ 1 1) 28 | 3 := (__ 3 12) 29 | 3 := (__ 12 3) 30 | 3 := (__ 5 9) 31 | 9 := (__ 9 2) 32 | 5 := (__ 9 12)) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/efc7fec9d73f816c75a782476873e83d -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_137.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-137 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Digits and bases = 5 | ;; By 4Clojure user: goranjovic 6 | ;; Difficulty: Medium 7 | ;; Tags: [math] 8 | ;; 9 | ;; Write a function which returns a sequence of digits of 10 | ;; a non-negative number (first argument) in numerical 11 | ;; system with an arbitrary base (second argument). Digits 12 | ;; should be represented with their integer values, e.g. 13 | ;; 15 would be [1 5] in base 10, [1 1 1 1] in base 2 and 14 | ;; [15] in base 16. 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | [1 2 3 4 5 0 1] := (__ 1234501 10) 24 | [0] := (__ 0 11) 25 | [1 0 0 1] := (__ 9 2) 26 | [1 0] := (let [n (rand-int 100000)](__ n n)) 27 | [16 18 5 24 15 1] := (__ Integer/MAX_VALUE 42)) 28 | 29 | ;; Share your solution, and/or check how others did it: 30 | ;; https://gist.github.com/adf96a9ce5784b20a758d0cb1864f0e7 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_108.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-108 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Lazy Searching = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs sorting] 8 | ;; 9 | ;; Given any number of sequences, each sorted from 10 | ;; smallest to largest, find the smallest single number 11 | ;; which appears in all of the sequences. The sequences 12 | ;; may be infinite, so be careful to search lazily. 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | 3 := (__ [3 4 5]) 22 | 4 := (__ [1 2 3 4 5 6 7] [0.5 3/2 4 19]) 23 | 7 := (__ (range) (range 0 100 7/6) [2 3 5 7 11 13]) 24 | 64 := (__ (map #(* % % %) (range)) ;; perfect cubes 25 | (filter #(zero? (bit-and % (dec %))) (range)) ;; powers of 2 26 | (iterate inc 20))) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/4a0485cfb64ebc165afeffc2406b5669 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_156.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-156 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Map Defaults = 5 | ;; By 4Clojure user: jaycfields 6 | ;; Difficulty: Elementary 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; When retrieving values from a map, you can specify 10 | ;; default values in case the key is not found: 11 | ;; 12 | ;; 13 | ;; (= 2 (:foo {:bar 0, :baz 1} 2)) 14 | ;; 15 | ;; 16 | ;; However, what if you want the map itself to contain 17 | ;; the default values? Write a function which takes a 18 | ;; default value and a sequence of keys and constructs a 19 | ;; map. 20 | 21 | (def __ :tests-will-fail) 22 | 23 | (comment 24 | 25 | ) 26 | 27 | (tests 28 | (__ 0 [:a :b :c]) := {:a 0 :b 0 :c 0} 29 | (__ "x" [1 2 3]) := {1 "x" 2 "x" 3 "x"} 30 | (__ [:a :b] [:foo :bar]) := {:foo [:a :b] :bar [:a :b]}) 31 | 32 | ;; Share your solution, and/or check how others did it: 33 | ;; https://gist.github.com/4e79a8389a4687b2564262b46785d45a -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_078.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-078 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Reimplement Trampoline = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [core-functions] 8 | ;; 9 | ;; Reimplement the function described in "Intro to 10 | ;; Trampoline" . 11 | 12 | (def restricted [trampoline]) 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (letfn [(triple [x] #(sub-two (* 3 x))) 22 | (sub-two [x] #(stop?(- x 2))) 23 | (stop? [x] (if (> x 50) x #(triple x)))] 24 | (__ triple 2)) := 25 | 82 26 | (letfn [(my-even? [x] (if (zero? x) true #(my-odd? (dec x)))) 27 | (my-odd? [x] (if (zero? x) false #(my-even? (dec x))))] 28 | (map (partial __ my-even?) (range 6))) := 29 | [true false true false true false]) 30 | 31 | ;; Share your solution, and/or check how others did it: 32 | ;; https://gist.github.com/1849f2788191d83171adfe7d10b9294e -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_050.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-050 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Split by Type = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function which takes a sequence consisting of 10 | ;; items with different types and splits them up into a 11 | ;; set of homogeneous sub-sequences. The internal order of 12 | ;; each sub-sequence should be maintained, but the 13 | ;; sub-sequences themselves can be returned in any order 14 | ;; (this is why 'set' is used in the test cases). 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | (set (__ [1 :a 2 :b 3 :c])) := #{[1 2 3] [:a :b :c]} 24 | (set (__ [:a "foo" "bar" :b])) := #{[:a :b] ["foo" "bar"]} 25 | (set (__ [[1 2] :a [3 4] 5 6 :b])) := #{[[1 2] [3 4]] [:a :b] [5 6]}) 26 | 27 | ;; Share your solution, and/or check how others did it: 28 | ;; https://gist.github.com/56425d809382d4b9f773a2d379cc26e0 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_086.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-086 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Happy numbers = 5 | ;; By 4Clojure user: daviddavis 6 | ;; Difficulty: Medium 7 | ;; Tags: [math] 8 | ;; 9 | ;; Happy numbers are positive integers that follow a 10 | ;; particular formula: take each individual digit, square 11 | ;; it, and then sum the squares to get a new number. 12 | ;; Repeat with the new number and eventually, you might 13 | ;; get to a number whose squared sum is 1. This is a happy 14 | ;; number. An unhappy number (or sad number) is one that 15 | ;; loops endlessly. Write a function that determines if a 16 | ;; number is happy or not. 17 | 18 | (def __ :tests-will-fail) 19 | 20 | (comment 21 | 22 | ) 23 | 24 | (tests 25 | (__ 7) := true 26 | (__ 986543210) := true 27 | (__ 2) := false 28 | (__ 3) := false) 29 | 30 | ;; Share your solution, and/or check how others did it: 31 | ;; https://gist.github.com/b921948244dc0417fc716fe31cecb359 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_071.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-071 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Rearranging Code: -> = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; The -> macro threads an expression x through a variable 9 | ;; number of forms. First, x is inserted as the second 10 | ;; item in the first form, making a list of it if it is 11 | ;; not a list already. Then the first form is inserted as 12 | ;; the second item in the second form, making a list of 13 | ;; that form if necessary. This process continues for all 14 | ;; the forms. Using -> can sometimes make your code more 15 | ;; readable. 16 | 17 | (def __ :tests-will-fail) 18 | 19 | (comment 20 | 21 | ) 22 | 23 | (tests 24 | (__ (sort (rest (reverse [2 5 4 1 3 6])))) := 25 | (-> [2 5 4 1 3 6] (reverse) (rest) (sort) (__)) 26 | 5) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/1c3da359cc636512f717ff01c4ef2388 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_079.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-079 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Triangle Minimal Path = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Hard 7 | ;; Tags: [graph-theory] 8 | ;; 9 | ;; Write a function which calculates the sum of the 10 | ;; minimal path through a triangle. The triangle is 11 | ;; represented as a collection of vectors. The path should 12 | ;; start at the top of the triangle and move to an 13 | ;; adjacent number on the next row until the bottom of the 14 | ;; triangle is reached. 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | 7 := (__ '([1] 24 | [2 4] 25 | [5 1 4] 26 | [2 3 4 5])) 27 | 20 := (__ '([3] 28 | [2 4] 29 | [1 9 3] 30 | [9 9 2 4] 31 | [4 6 6 7 8] 32 | [5 7 3 5 1 4]))) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/de67b77aadddea34b2f4997952d4f101 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_072.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-072 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Rearranging Code: ->> = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Elementary 7 | ;; 8 | ;; The ->> macro threads an expression x through a 9 | ;; variable number of forms. First, x is inserted as the 10 | ;; last item in the first form, making a list of it if it 11 | ;; is not a list already. Then the first form is inserted 12 | ;; as the last item in the second form, making a list of 13 | ;; that form if necessary. This process continues for all 14 | ;; the forms. Using ->> can sometimes make your code more 15 | ;; readable. 16 | 17 | (def __ :tests-will-fail) 18 | 19 | (comment 20 | 21 | ) 22 | 23 | (tests 24 | (__ (map inc (take 3 (drop 2 [2 5 4 1 3 6])))) := 25 | (->> [2 5 4 1 3 6] (drop 2) (take 3) (map inc) (__)) 26 | 11) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/7c7f22cab82de9fbeb08aa518efca3ec -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_063.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-063 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Group a Sequence = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [core-functions] 8 | ;; 9 | ;; Given a function f and a sequence s, write a function 10 | ;; which returns a map. The keys should be the values of f 11 | ;; applied to each item in s. The value at each key should 12 | ;; be a vector of corresponding items in the order they 13 | ;; appear in s. 14 | 15 | (def restricted [group-by]) 16 | 17 | (def __ :tests-will-fail) 18 | 19 | (comment 20 | 21 | ) 22 | 23 | (tests 24 | (__ #(> % 5) [1 3 6 8]) := {false [1 3], true [6 8]} 25 | (__ #(apply / %) [[1 2] [2 4] [4 6] [3 6]]) := 26 | {1/2 [[1 2] [2 4] [3 6]], 2/3 [[4 6]]} 27 | (__ count [[1] [1 2] [3] [1 2 3] [2 3]]) := 28 | {1 [[1] [3]], 2 [[1 2] [2 3]], 3 [[1 2 3]]}) 29 | 30 | ;; Share your solution, and/or check how others did it: 31 | ;; https://gist.github.com/b49b9b1171a0d8340f94893a614a43ec -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_098.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-098 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Equivalence Classes = 5 | ;; By 4Clojure user: drcabana 6 | ;; Difficulty: Medium 7 | ;; 8 | ;; A function f defined on a domain D induces an 9 | ;; equivalence relation on D, as follows: a is equivalent 10 | ;; to b with respect to f if and only if (f a) is equal to 11 | ;; (f b). Write a function with arguments f and D that 12 | ;; computes the equivalence classes of D with respect to 13 | ;; f. 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | (__ #(* % %) #{-2 -1 0 1 2}) := 23 | #{#{0} #{1 -1} #{2 -2}} 24 | (__ #(rem % 3) #{0 1 2 3 4 5 }) := 25 | #{#{0 3} #{1 4} #{2 5}} 26 | (__ identity #{0 1 2 3 4}) := 27 | #{#{0} #{1} #{2} #{3} #{4}} 28 | (__ (constantly true) #{0 1 2 3 4}) := 29 | #{#{0 1 2 3 4}}) 30 | 31 | ;; Share your solution, and/or check how others did it: 32 | ;; https://gist.github.com/b297be9d61e94036a84cfc8de6374ee8 -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_064.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-064 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Reduce = 5 | ;; By 4Clojure user: citizen428 6 | ;; Difficulty: Elementary 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Reduce takes a 2 argument function and an optional 10 | ;; starting value. It then applies the function to the 11 | ;; first 2 items in the sequence (or the starting value 12 | ;; and the first element of the sequence). In the next 13 | ;; iteration the function will be called on the previous 14 | ;; return value and the next item from the sequence, thus 15 | ;; reducing the entire collection to one value. Don't 16 | ;; worry, it's not as complicated as it sounds. 17 | 18 | (def __ :tests-will-fail) 19 | 20 | (comment 21 | 22 | ) 23 | 24 | (tests 25 | 15 := (reduce __ [1 2 3 4 5]) 26 | 0 := (reduce __ []) 27 | 6 := (reduce __ 1 [2 3])) 28 | 29 | ;; Share your solution, and/or check how others did it: 30 | ;; https://gist.github.com/94e7ff60b2323d795e30e8d7e185b72e -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_095.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-095 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = To Tree, or not to Tree = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [trees] 8 | ;; 9 | ;; Write a predicate which checks whether or not a given 10 | ;; sequence represents a binary tree. Each node in the 11 | ;; tree must have a value, a left child, and a right 12 | ;; child. 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | (__ '(:a (:b nil nil) nil)) := 22 | true 23 | (__ '(:a (:b nil nil))) := 24 | false 25 | (__ [1 nil [2 [3 nil nil] [4 nil nil]]]) := 26 | true 27 | (__ [1 [2 nil nil] [3 nil nil] [4 nil nil]]) := 28 | false 29 | (__ [1 [2 [3 [4 nil nil] nil] nil] nil]) := 30 | true 31 | (__ [1 [2 [3 [4 false nil] nil] nil] nil]) := 32 | false 33 | (__ '(:a nil ())) := 34 | false) 35 | 36 | ;; Share your solution, and/or check how others did it: 37 | ;; https://gist.github.com/abc33dae1f190ae44f2ebd6d5aeec567 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_097.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-097 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Pascal's Triangle = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; 8 | ;; Pascal's triangle is a triangle of numbers computed 9 | ;; using the following rules: 10 | ;; 11 | ;; 12 | ;; - The first row is 1. 13 | ;; 14 | ;; - Each successive row is computed by adding together 15 | ;; adjacent numbers in the row above, and adding a 1 to 16 | ;; the beginning and end of the row. 17 | ;; 18 | ;; 19 | ;; Write a function which returns the nth row of Pascal's 20 | ;; Triangle. 21 | 22 | (def __ :tests-will-fail) 23 | 24 | (comment 25 | 26 | ) 27 | 28 | (tests 29 | (__ 1) := [1] 30 | (map __ (range 1 6)) := 31 | [ [1] 32 | [1 1] 33 | [1 2 1] 34 | [1 3 3 1] 35 | [1 4 6 4 1]] 36 | (__ 11) := 37 | [1 10 45 120 210 252 210 120 45 10 1]) 38 | 39 | ;; Share your solution, and/or check how others did it: 40 | ;; https://gist.github.com/8f92cec20d7adca8acce102adcbd01c8 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_090.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-090 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Cartesian Product = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [set-theory] 8 | ;; 9 | ;; Write a function which calculates the Cartesian product 10 | ;; of two sets. 11 | 12 | (def __ :tests-will-fail) 13 | 14 | (comment 15 | 16 | ) 17 | 18 | (tests 19 | (__ #{"ace" "king" "queen"} #{"♠" "♥" "♦" "♣"}) := 20 | #{["ace" "♠"] ["ace" "♥"] ["ace" "♦"] ["ace" "♣"] 21 | ["king" "♠"] ["king" "♥"] ["king" "♦"] ["king" "♣"] 22 | ["queen" "♠"] ["queen" "♥"] ["queen" "♦"] ["queen" "♣"]} 23 | (__ #{1 2 3} #{4 5}) := 24 | #{[1 4] [2 4] [3 4] [1 5] [2 5] [3 5]} 25 | 300 := (count (__ (into #{} (range 10)) 26 | (into #{} (range 30))))) 27 | 28 | ;; Share your solution, and/or check how others did it: 29 | ;; https://gist.github.com/0522d05068eda17584d75260e44c2d93 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_092.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-092 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Read Roman numerals = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Hard 7 | ;; Tags: [strings math] 8 | ;; 9 | ;; Roman numerals are easy to recognize, but not everyone 10 | ;; knows all the rules necessary to work with them. Write 11 | ;; a function to parse a Roman-numeral string and return 12 | ;; the number it represents. 13 | ;; 14 | ;; 15 | ;; You can assume that the input will be well-formed, in 16 | ;; upper-case, and follow the subtractive principle. You 17 | ;; don't need to handle any numbers greater than MMMCMXCIX 18 | ;; (3999), the largest number representable with ordinary 19 | ;; letters. 20 | 21 | (def __ :tests-will-fail) 22 | 23 | (comment 24 | 25 | ) 26 | 27 | (tests 28 | 14 := (__ "XIV") 29 | 827 := (__ "DCCCXXVII") 30 | 3999 := (__ "MMMCMXCIX") 31 | 48 := (__ "XLVIII")) 32 | 33 | ;; Share your solution, and/or check how others did it: 34 | ;; https://gist.github.com/08b1683167d5da70037757a6be3efe0a -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_158.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-158 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Decurry = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Medium 7 | ;; Tags: [partial-functions] 8 | ;; 9 | ;; Write a function that accepts a curried function of 10 | ;; unknown arity n. Return an equivalent function of n 11 | ;; arguments. 12 | ;; 13 | ;; You may wish to read this . 14 | 15 | (def __ :tests-will-fail) 16 | 17 | (comment 18 | 19 | ) 20 | 21 | (tests 22 | 10 := ((__ (fn [a] 23 | (fn [b] 24 | (fn [c] 25 | (fn [d] 26 | (+ a b c d)))))) 27 | 1 2 3 4) 28 | 24 := ((__ (fn [a] 29 | (fn [b] 30 | (fn [c] 31 | (fn [d] 32 | (* a b c d)))))) 33 | 1 2 3 4) 34 | 25 := ((__ (fn [a] 35 | (fn [b] 36 | (* a b)))) 37 | 5 5)) 38 | 39 | ;; Share your solution, and/or check how others did it: 40 | ;; https://gist.github.com/8bdf1dba5225cd748e4d53545f7f68f7 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_077.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-077 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Anagram Finder = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; 8 | ;; Write a function which finds all the anagrams in a 9 | ;; vector of words. A word x is an anagram of word y if 10 | ;; all the letters in x can be rearranged in a different 11 | ;; order to form y. Your function should return a set of 12 | ;; sets, where each sub-set is a group of words which are 13 | ;; anagrams of each other. Each sub-set should have at 14 | ;; least two words. Words without any anagrams should not 15 | ;; be included in the result. 16 | 17 | (def __ :tests-will-fail) 18 | 19 | (comment 20 | 21 | ) 22 | 23 | (tests 24 | (__ ["meat" "mat" "team" "mate" "eat"]) := 25 | #{#{"meat" "team" "mate"}} 26 | (__ ["veer" "lake" "item" "kale" "mite" "ever"]) := 27 | #{#{"veer" "ever"} #{"lake" "kale"} #{"mite" "item"}}) 28 | 29 | ;; Share your solution, and/or check how others did it: 30 | ;; https://gist.github.com/0ffdaf0cd713a4df991004e0fccf19ac -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_135.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-135 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Infix Calculator = 5 | ;; By 4Clojure user: fdaoud 6 | ;; Difficulty: Easy 7 | ;; Tags: [higher-order-functions math] 8 | ;; 9 | ;; Your friend Joe is always whining about Lisps using the 10 | ;; prefix notation for math. Show him how you could easily 11 | ;; write a function that does math using the infix 12 | ;; notation. Is your favorite language that flexible, Joe? 13 | ;; 14 | ;; Write a function that accepts a variable length 15 | ;; mathematical expression consisting of numbers and the 16 | ;; operations +, -, *, and /. Assume a simple calculator 17 | ;; that does not do precedence and instead just calculates 18 | ;; left to right. 19 | 20 | (def __ :tests-will-fail) 21 | 22 | (comment 23 | 24 | ) 25 | 26 | (tests 27 | 7 := (__ 2 + 5) 28 | 42 := (__ 38 + 48 - 2 / 2) 29 | 8 := (__ 10 / 2 - 1 * 2) 30 | 72 := (__ 20 / 2 + 2 + 4 + 8 - 6 - 10 * 9)) 31 | 32 | ;; Share your solution, and/or check how others did it: 33 | ;; https://gist.github.com/3dddcaa33e0bba7b9de89b463d778e55 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_113.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-113 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Making Data Dance = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Hard 7 | ;; Tags: [types] 8 | ;; 9 | ;; Write a function that takes a variable number of 10 | ;; integer arguments. If the output is coerced into a 11 | ;; string, it should return a comma (and space) separated 12 | ;; list of the inputs sorted smallest to largest. If the 13 | ;; output is coerced into a sequence, it should return a 14 | ;; seq of unique input elements in the same order as they 15 | ;; were entered. 16 | 17 | (def restricted [proxy]) 18 | 19 | (def __ :tests-will-fail) 20 | 21 | (comment 22 | 23 | ) 24 | 25 | (tests 26 | "1, 2, 3" := (str (__ 2 1 3)) 27 | '(2 1 3) := (seq (__ 2 1 3)) 28 | '(2 1 3) := (seq (__ 2 1 3 3 1 2)) 29 | '(1) := (seq (apply __ (repeat 5 1))) 30 | "1, 1, 1, 1, 1" := (str (apply __ (repeat 5 1))) 31 | (= nil (seq (__))) := 32 | (= "" (str (__)))) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/5a33bc5bed21cf8105bcdc239b25e2de -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_166.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-166 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Comparisons = 5 | ;; By 4Clojure user: daowen 6 | ;; Difficulty: Easy 7 | ;; 8 | ;; For any orderable data type it's possible to derive all 9 | ;; of the basic comparison operations (<, ≤, =, ≠, ≥, and 10 | ;; >) from a single operation (any operator but = or ≠ 11 | ;; will work). Write a function that takes three 12 | ;; arguments, a less than operator for the data and two 13 | ;; items to compare. The function should return a keyword 14 | ;; describing the relationship between the two items. The 15 | ;; keywords for the relationship between x and y are as 16 | ;; follows: 17 | ;; 18 | ;; * x = y → :eq 19 | ;; * x > y → :gt 20 | ;; * x < y → :lt 21 | 22 | (def __ :tests-will-fail) 23 | 24 | (comment 25 | 26 | ) 27 | 28 | (tests 29 | :gt := (__ < 5 1) 30 | :eq := (__ (fn [x y] (< (count x) (count y))) "pear" "plum") 31 | :lt := (__ (fn [x y] (< (mod x 5) (mod y 5))) 21 3) 32 | :gt := (__ > 0 2)) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/6057fba465c3af1d06a9703d4ebdc7d1 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_084.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-084 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Transitive Closure = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Hard 7 | ;; Tags: [set-theory] 8 | ;; 9 | ;; Write a function which generates the transitive closure 10 | ;; of a binary relation. The relation will be represented 11 | ;; as a set of 2 item vectors. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | [divides #{[8 4] [9 3] [4 2] [27 9]}] := 21 | (= (__ divides) #{[4 2] [8 4] [8 2] [9 3] [27 9] [27 3]}) 22 | [more-legs 23 | #{["cat" "man"] ["man" "snake"] ["spider" "cat"]}] := 24 | (= (__ more-legs) 25 | #{["cat" "man"] ["cat" "snake"] ["man" "snake"] 26 | ["spider" "cat"] ["spider" "man"] ["spider" "snake"]}) 27 | [progeny 28 | #{["father" "son"] ["uncle" "cousin"] ["son" "grandson"]}] := 29 | (= (__ progeny) 30 | #{["father" "son"] ["father" "grandson"] 31 | ["uncle" "cousin"] ["son" "grandson"]})) 32 | 33 | ;; Share your solution, and/or check how others did it: 34 | ;; https://gist.github.com/7935c4eab565b94f96df9da4190b3509 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_076.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-076 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Intro to Trampoline = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [recursion] 8 | ;; 9 | ;; The trampoline function takes a function f and a 10 | ;; variable number of parameters. Trampoline calls f with 11 | ;; any parameters that were supplied. If f returns a 12 | ;; function, trampoline calls that function with no 13 | ;; arguments. This is repeated, until the return value is 14 | ;; not a function, and then trampoline returns that 15 | ;; non-function value. This is useful for implementing 16 | ;; mutually recursive algorithms in a way that won't 17 | ;; consume the stack. 18 | 19 | (def __ :tests-will-fail) 20 | 21 | (comment 22 | 23 | ) 24 | 25 | (tests 26 | __ := 27 | (letfn 28 | [(foo [x y] #(bar (conj x y) y)) 29 | (bar [x y] (if (> (last x) 10) 30 | x 31 | #(foo x (+ 2 y))))] 32 | (trampoline foo [] 1))) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/d43e270986333e278bdcf6431cc2ffd4 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_069.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-069 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Merge with a Function = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Medium 7 | ;; Tags: [core-functions] 8 | ;; 9 | ;; Write a function which takes a function f and a 10 | ;; variable number of maps. Your function should return a 11 | ;; map that consists of the rest of the maps conj-ed onto 12 | ;; the first. If a key occurs in more than one map, the 13 | ;; mapping(s) from the latter (left-to-right) should be 14 | ;; combined with the mapping in the result by calling (f 15 | ;; val-in-result val-in-latter) 16 | 17 | (def restricted [merge-with]) 18 | 19 | (def __ :tests-will-fail) 20 | 21 | (comment 22 | 23 | ) 24 | 25 | (tests 26 | (__ * {:a 2, :b 3, :c 4} {:a 2} {:b 2} {:c 5}) := 27 | {:a 4, :b 6, :c 20} 28 | (__ - {1 10, 2 20} {1 3, 2 10, 3 15}) := 29 | {1 7, 2 10, 3 15} 30 | (__ concat {:a [3], :b [6]} {:a [4 5], :c [8 9]} {:b [7]}) := 31 | {:a [3 4 5], :b [6 7], :c [8 9]}) 32 | 33 | ;; Share your solution, and/or check how others did it: 34 | ;; https://gist.github.com/3dde3e1ed83b73dfe2aca3a07b307a5d -------------------------------------------------------------------------------- /src/rich4clojure/elementary/problem_145.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.elementary.problem-145 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = For the win = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Elementary 7 | ;; Tags: [core-functions seqs] 8 | ;; 9 | ;; Clojure's for macro is a tremendously versatile 10 | ;; mechanism for producing a sequence based on some other 11 | ;; sequence(s). It can take some time to understand how to 12 | ;; use it properly, but that investment will be paid back 13 | ;; with clear, concise sequence-wrangling later. With that 14 | ;; in mind, read over these for expressions and try to see 15 | ;; how each of them produces the same result. 16 | 17 | (def __ :tests-will-fail) 18 | 19 | (comment 20 | 21 | ) 22 | 23 | (tests 24 | __ := (for [x (range 40) 25 | :when (= 1 (rem x 4))] 26 | x) 27 | __ := (for [x (iterate #(+ 4 %) 0) 28 | :let [z (inc x)] 29 | :while (< z 40)] 30 | z) 31 | __ := (for [[x y] (partition 2 (range 20))] 32 | (+ x y))) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/6d7ba65546171142f8d109ee54829a36 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_177.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-177 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Balancing Brackets = 5 | ;; By 4Clojure user: daowen 6 | ;; Difficulty: Medium 7 | ;; Tags: [parsing] 8 | ;; 9 | ;; When parsing a snippet of code it's often a good idea 10 | ;; to do a sanity check to see if all the brackets match 11 | ;; up. Write a function that takes in a string and returns 12 | ;; truthy if all square [] round () and curly {} brackets 13 | ;; are properly paired and legally nested, or returns 14 | ;; falsey otherwise. 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | "This string has no brackets." := 24 | "class Test { 25 | public static void main(String[] args) { 26 | System.out.println(\"Hello world.\"); 27 | } 28 | }" := 29 | (__ "(start, end]") := 30 | (__ "())") := 31 | (__ "[ { ] } ") := 32 | "([]([(()){()}(()(()))(([[]]({}()))())]((((()()))))))" := 33 | (__ "([]([(()){()}(()(()))(([[]]({}([)))())]((((()()))))))") := 34 | (__ "[") :=) 35 | 36 | ;; Share your solution, and/or check how others did it: 37 | ;; https://gist.github.com/6b8d50ee0811042bdc646dc9060037e8 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_132.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-132 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Insert between two items = 5 | ;; By 4Clojure user: srid 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs core-functions] 8 | ;; 9 | ;; Write a function that takes a two-argument predicate, a 10 | ;; value, and a collection; and returns a new collection 11 | ;; where the value is inserted between every two items 12 | ;; that satisfy the predicate. 13 | 14 | (def __ :tests-will-fail) 15 | 16 | (comment 17 | 18 | ) 19 | 20 | (tests 21 | '(1 :less 6 :less 7 4 3) := (__ < :less [1 6 7 4 3]) 22 | '(2) := (__ > :more [2]) 23 | [0 1 :x 2 :x 3 :x 4] := (__ #(and (pos? %) (< % %2)) :x (range 5)) 24 | (__ > :more ()) := 25 | [0 1 :same 1 2 3 :same 5 8 13 :same 21] := 26 | (take 12 (->> [0 1] 27 | (iterate (fn [[a b]] [b (+ a b)])) 28 | (map first) ; fibonacci numbers 29 | (__ (fn [a b] ; both even or both odd 30 | (= (mod a 2) (mod b 2))) 31 | :same)))) 32 | 33 | ;; Share your solution, and/or check how others did it: 34 | ;; https://gist.github.com/742e221111abc4aa551e4f99214e962f -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_112.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-112 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sequs Horribilis = 5 | ;; By 4Clojure user: ghoseb 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Create a function which takes an integer and a nested 10 | ;; collection of integers as arguments. Analyze the 11 | ;; elements of the input collection and return a sequence 12 | ;; which maintains the nested structure, and which 13 | ;; includes all elements starting from the head whose sum 14 | ;; is less than or equal to the input integer. 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | (__ 10 [1 2 [3 [4 5] 6] 7]) := 24 | '(1 2 (3 (4))) 25 | (__ 30 [1 2 [3 [4 [5 [6 [7 8]] 9]] 10] 11]) := 26 | '(1 2 (3 (4 (5 (6 (7)))))) 27 | (__ 9 (range)) := 28 | '(0 1 2 3) 29 | (__ 1 [[[[[1]]]]]) := 30 | '(((((1))))) 31 | (__ 0 [1 2 [3 [4 5] 6] 7]) := 32 | '() 33 | (__ 0 [0 0 [0 [0]]]) := 34 | '(0 0 (0 (0))) 35 | (__ 1 [-10 [1 [2 3 [4 5 [6 7 [8]]]]]]) := 36 | '(-10 (1 (2 3 (4))))) 37 | 38 | ;; Share your solution, and/or check how others did it: 39 | ;; https://gist.github.com/dfcf8ebb019b5ab679b66d1f812edddf -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_148.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-148 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = The Big Divide = 5 | ;; By 4Clojure user: goranjovic 6 | ;; Difficulty: Medium 7 | ;; Tags: [math] 8 | ;; 9 | ;; Write a function which calculates the sum of all 10 | ;; natural numbers under n (first argument) which are 11 | ;; evenly divisible by at least one of a and b (second and 12 | ;; third argument). Numbers a and b are guaranteed to be 13 | ;; coprimes. 14 | ;; 15 | ;; 16 | ;; Note: Some test cases have a very large n, so the most 17 | ;; obvious solution will exceed the time limit. 18 | 19 | (def __ :tests-will-fail) 20 | 21 | (comment 22 | 23 | ) 24 | 25 | (tests 26 | 0 := (__ 3 17 11) 27 | 23 := (__ 10 3 5) 28 | 233168 := (__ 1000 3 5) 29 | "2333333316666668" := (str (__ 100000000 3 5)) 30 | "110389610389889610389610" := 31 | (str (__ (* 10000 10000 10000) 7 11)) 32 | "1277732511922987429116" := 33 | (str (__ (* 10000 10000 10000) 757 809)) 34 | "4530161696788274281" := 35 | (str (__ (* 10000 10000 1000) 1597 3571))) 36 | 37 | ;; Share your solution, and/or check how others did it: 38 | ;; https://gist.github.com/03f17b792f88ed2b870a15ab1f47fa04 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_110.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-110 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sequence of pronunciations = 5 | ;; By 4Clojure user: mlni 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function that returns a lazy sequence of 10 | ;; "pronunciations" of a sequence of numbers. A 11 | ;; pronunciation of each element in the sequence consists 12 | ;; of the number of repeating identical numbers and the 13 | ;; number itself. For example, [1 1] is pronounced as [2 14 | ;; 1] ("two ones"), which in turn is pronounced as [1 2 1 15 | ;; 1] ("one two, one one"). 16 | ;; 17 | ;; Your function should accept an initial sequence of 18 | ;; numbers, and return an infinite lazy sequence of 19 | ;; pronunciations, each element being a pronunciation of 20 | ;; the previous element. 21 | 22 | (def __ :tests-will-fail) 23 | 24 | (comment 25 | 26 | ) 27 | 28 | (tests 29 | [[1 1] [2 1] [1 2 1 1]] := (take 3 (__ [1])) 30 | [3 1 2 4] := (first (__ [1 1 1 4 4])) 31 | [1 1 1 3 2 1 3 2 1 1] := (nth (__ [1]) 6) 32 | 338 := (count (nth (__ [3 2]) 15))) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/f4169b69fdd707f14ae92e5b006ff4b0 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_114.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-114 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Global take-while = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs higher-order-functions] 8 | ;; 9 | ;; take-while is great for filtering sequences, but it 10 | ;; limited: you can only examine a single item of the 11 | ;; sequence at a time. What if you need to keep track of 12 | ;; some state as you go over the sequence? 13 | ;; 14 | ;; 15 | ;; Write a function which accepts an integer n, a 16 | ;; predicate p, and a sequence. It should return a lazy 17 | ;; sequence of items in the list up to, but not including, 18 | ;; the n th item that satisfies the predicate. 19 | 20 | (def __ :tests-will-fail) 21 | 22 | (comment 23 | 24 | ) 25 | 26 | (tests 27 | [2 3 5 7 11 13] := 28 | (__ 4 #(= 2 (mod % 3)) 29 | [2 3 5 7 11 13 17 19 23]) 30 | ["this" "is" "a" "sentence"] := 31 | (__ 3 #(some #{\i} %) 32 | ["this" "is" "a" "sentence" "i" "wrote"]) 33 | ["this" "is"] := 34 | (__ 1 #{"a"} 35 | ["this" "is" "a" "sentence" "i" "wrote"])) 36 | 37 | ;; Share your solution, and/or check how others did it: 38 | ;; https://gist.github.com/9d1067c73d424ab77b3b6f1941f3c7a9 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_121.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-121 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Universal Computation Engine = 5 | ;; By 4Clojure user: mlni 6 | ;; Difficulty: Medium 7 | ;; Tags: [functions] 8 | ;; 9 | ;; Given a mathematical formula in prefix notation, return 10 | ;; a function that calculates the value of the formula. 11 | ;; The formula can contain nested calculations using the 12 | ;; four basic mathematical operators, numeric constants, 13 | ;; and symbols representing variables. The returned 14 | ;; function has to accept a single parameter containing 15 | ;; the map of variable names to their values. 16 | 17 | (def restricted [eval resolve]) 18 | 19 | (def __ :tests-will-fail) 20 | 21 | (comment 22 | 23 | ) 24 | 25 | (tests 26 | 2 := ((__ '(/ a b)) 27 | '{b 8 a 16}) 28 | 8 := ((__ '(+ a b 2)) 29 | '{a 2 b 4}) 30 | [6 0 -4] := 31 | (map (__ '(* (+ 2 a) 32 | (- 10 b))) 33 | '[{a 1 b 8} 34 | {b 5 a -2} 35 | {a 2 b 11}]) 36 | 1 := ((__ '(/ (+ x 2) 37 | (* 3 (+ y 1)))) 38 | '{x 4 y 1})) 39 | 40 | ;; Share your solution, and/or check how others did it: 41 | ;; https://gist.github.com/8056f0c78af9f89d919db9f42c22cb48 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_103.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-103 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Generating k-combinations = 5 | ;; By 4Clojure user: patsp 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs combinatorics] 8 | ;; 9 | ;; Given a sequence S consisting of n elements generate 10 | ;; all k-combinations of S, i. e. generate all possible 11 | ;; sets consisting of k distinct elements taken from S. 12 | ;; 13 | ;; The number of k-combinations for a sequence is equal 14 | ;; to the binomial coefficient . 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | (__ 1 #{4 5 6}) := #{#{4} #{5} #{6}} 24 | (__ 10 #{4 5 6}) := #{} 25 | (__ 2 #{0 1 2}) := #{#{0 1} #{0 2} #{1 2}} 26 | (__ 3 #{0 1 2 3 4}) := #{#{0 1 2} #{0 1 3} #{0 1 4} #{0 2 3} #{0 2 4} 27 | #{0 3 4} #{1 2 3} #{1 2 4} #{1 3 4} #{2 3 4}} 28 | (__ 4 #{[1 2 3] :a "abc" "efg"}) := #{#{[1 2 3] :a "abc" "efg"}} 29 | (__ 2 #{[1 2 3] :a "abc" "efg"}) := #{#{[1 2 3] :a} #{[1 2 3] "abc"} #{[1 2 3] "efg"} 30 | #{:a "abc"} #{:a "efg"} #{"abc" "efg"}}) 31 | 32 | ;; Share your solution, and/or check how others did it: 33 | ;; https://gist.github.com/59cc791bc18d235646673dd8b8b8b66f -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_101.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-101 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Levenshtein Distance = 5 | ;; By 4Clojure user: patsp 6 | ;; Difficulty: Hard 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Given two sequences x and y, calculate the Levenshtein 10 | ;; distance of x and y, i. e. the minimum number of edits 11 | ;; needed to transform x into y. The allowed edits are: 12 | ;; 13 | ;; 14 | ;; - insert a single item 15 | ;; 16 | ;; - delete a single item 17 | ;; 18 | ;; - replace a single item with another item 19 | ;; 20 | ;; 21 | ;; WARNING: Some of the test cases may timeout if you 22 | ;; write an inefficient solution! 23 | 24 | (def __ :tests-will-fail) 25 | 26 | (comment 27 | 28 | ) 29 | 30 | (tests 31 | (__ "kitten" "sitting") := 3 32 | (__ "closure" "clojure") := (__ "clojure" "closure") 1 33 | (__ "xyx" "xyyyx") := 2 34 | (__ "" "123456") := 6 35 | (__ "Clojure" "Clojure") := (__ "" "") (__ [] []) 0 36 | (__ [1 2 3 4] [0 2 3 4 5]) := 2 37 | (__ '(:a :b :c :d) '(:a :d)) := 2 38 | (__ "ttttattttctg" "tcaaccctaccat") := 10 39 | (__ "gaattctaatctc" "caaacaaaaaattt") := 9) 40 | 41 | ;; Share your solution, and/or check how others did it: 42 | ;; https://gist.github.com/e34b0bc82feb2072d007902d3959e83e -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_096.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-096 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Beauty is Symmetry = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Easy 7 | ;; Tags: [trees] 8 | ;; 9 | ;; Let us define a binary tree as "symmetric" if the left 10 | ;; half of the tree is the mirror image of the right half 11 | ;; of the tree. Write a predicate to determine whether or 12 | ;; not a given binary tree is symmetric. (see To Tree, or 13 | ;; not to Tree for a reminder on the tree representation 14 | ;; we're using). 15 | 16 | (def __ :tests-will-fail) 17 | 18 | (comment 19 | 20 | ) 21 | 22 | (tests 23 | (__ '(:a (:b nil nil) (:b nil nil))) := true 24 | (__ '(:a (:b nil nil) nil)) := false 25 | (__ '(:a (:b nil nil) (:c nil nil))) := false 26 | (__ [1 [2 nil [3 [4 [5 nil nil] [6 nil nil]] nil]] 27 | [2 [3 nil [4 [6 nil nil] [5 nil nil]]] nil]]) := 28 | true 29 | (__ [1 [2 nil [3 [4 [5 nil nil] [6 nil nil]] nil]] 30 | [2 [3 nil [4 [5 nil nil] [6 nil nil]]] nil]]) := 31 | false 32 | (__ [1 [2 nil [3 [4 [5 nil nil] [6 nil nil]] nil]] 33 | [2 [3 nil [4 [6 nil nil] nil]] nil]]) := 34 | false) 35 | 36 | ;; Share your solution, and/or check how others did it: 37 | ;; https://gist.github.com/74f30ddf0d4ce94f886ccf412d51a099 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_147.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-147 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Pascal's Trapezoid = 5 | ;; By 4Clojure user: narvius 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; Write a function that, for any given input vector of 10 | ;; numbers, returns an infinite lazy sequence of vectors, 11 | ;; where each next one is constructed from the previous 12 | ;; following the rules used in Pascal's Triangle. For 13 | ;; example, for [3 1 2], the next row is [3 4 3 2]. 14 | ;; 15 | ;; Beware of arithmetic overflow! In clojure (since 16 | ;; version 1.3 in 2011), if you use an arithmetic operator 17 | ;; like + and the result is too large to fit into a 64-bit 18 | ;; integer, an exception is thrown. You can use +' to 19 | ;; indicate that you would rather overflow into Clojure's 20 | ;; slower, arbitrary-precision bigint. 21 | 22 | (def __ :tests-will-fail) 23 | 24 | (comment 25 | 26 | ) 27 | 28 | (tests 29 | (second (__ [2 3 2])) := [2 5 5 2] 30 | (take 5 (__ [1])) := [[1] [1 1] [1 2 1] [1 3 3 1] [1 4 6 4 1]] 31 | (take 2 (__ [3 1 2])) := [[3 1 2] [3 4 3 2]] 32 | (take 100 (__ [2 4 2])) := (rest (take 101 (__ [2 2])))) 33 | 34 | ;; Share your solution, and/or check how others did it: 35 | ;; https://gist.github.com/9dcf23444c51883c4318d69efcd5e9f7 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_082.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-082 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Word Chains = 5 | ;; By 4Clojure user: dbyrne 6 | ;; Difficulty: Hard 7 | ;; Tags: [seqs] 8 | ;; 9 | ;; A word chain consists of a set of words ordered so that 10 | ;; each word differs by only one letter from the words 11 | ;; directly before and after it. The one letter difference 12 | ;; can be either an insertion, a deletion, or a 13 | ;; substitution. Here is an example word chain: 14 | ;; 15 | ;; 16 | ;; cat -> cot -> coat -> oat -> hat -> hot -> hog -> dog 17 | ;; 18 | ;; 19 | ;; Write a function which takes a sequence of words, and 20 | ;; returns true if they can be arranged into one 21 | ;; continuous word chain, and false if they cannot. 22 | 23 | (def __ :tests-will-fail) 24 | 25 | (comment 26 | 27 | ) 28 | 29 | (tests 30 | true := (__ #{"hat" "coat" "dog" "cat" "oat" "cot" "hot" "hog"}) 31 | false := (__ #{"cot" "hot" "bat" "fat"}) 32 | false := (__ #{"to" "top" "stop" "tops" "toss"}) 33 | true := (__ #{"spout" "do" "pot" "pout" "spot" "dot"}) 34 | true := (__ #{"share" "hares" "shares" "hare" "are"}) 35 | false := (__ #{"share" "hares" "hare" "are"})) 36 | 37 | ;; Share your solution, and/or check how others did it: 38 | ;; https://gist.github.com/9f1e130d45a4eb86b174a532a79b69b1 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_089.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-089 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Graph Tour = 5 | ;; By 4Clojure user: lucas1000001 6 | ;; Difficulty: Hard 7 | ;; Tags: [graph-theory] 8 | ;; 9 | ;; Starting with a graph you must write a function that 10 | ;; returns true if it is possible to make a tour of the 11 | ;; graph in which every edge is visited exactly once. 12 | ;; 13 | ;; 14 | ;; The graph is represented by a vector of tuples, where 15 | ;; each tuple represents a single edge. 16 | ;; 17 | ;; 18 | ;; The rules are: 19 | ;; 20 | ;; 21 | ;; - You can start at any node. 22 | ;; 23 | ;; - You must visit each edge exactly once. 24 | ;; 25 | ;; - All edges are undirected. 26 | 27 | (def __ :tests-will-fail) 28 | 29 | (comment 30 | 31 | ) 32 | 33 | (tests 34 | true := (__ [[:a :b]]) 35 | false := (__ [[:a :a] [:b :b]]) 36 | false := (__ [[:a :b] [:a :b] [:a :c] [:c :a] 37 | [:a :d] [:b :d] [:c :d]]) 38 | true := (__ [[1 2] [2 3] [3 4] [4 1]]) 39 | true := (__ [[:a :b] [:a :c] [:c :b] [:a :e] 40 | [:b :e] [:a :d] [:b :d] [:c :e] 41 | [:d :e] [:c :f] [:d :f]]) 42 | false := (__ [[1 2] [2 3] [2 4] [2 5]])) 43 | 44 | ;; Share your solution, and/or check how others did it: 45 | ;; https://gist.github.com/af87973306f77532cbc396513392babc -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_107.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-107 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Simple closures = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Easy 7 | ;; Tags: [higher-order-functions math] 8 | ;; 9 | ;; Lexical scope and first-class functions are two of the 10 | ;; most basic building blocks of a functional language 11 | ;; like Clojure. When you combine the two together, you 12 | ;; get something very powerful called lexical closures. 13 | ;; With these, you can exercise a great deal of control 14 | ;; over the lifetime of your local bindings, saving their 15 | ;; values for use later, long after the code you're 16 | ;; running now has finished. 17 | ;; 18 | ;; 19 | ;; It can be hard to follow in the abstract, so let's 20 | ;; build a simple closure. Given a positive integer n, 21 | ;; return a function (f x) which computes x n. Observe 22 | ;; that the effect of this is to preserve the value of n 23 | ;; for use outside the scope in which it is defined. 24 | 25 | (def __ :tests-will-fail) 26 | 27 | (comment 28 | 29 | ) 30 | 31 | (tests 32 | 256 := ((__ 2) 16), 33 | ((__ 8) 2) 34 | [1 8 27 64] := (map (__ 3) [1 2 3 4]) 35 | [1 2 4 8 16] := (map #((__ %) 2) [0 1 2 3 4])) 36 | 37 | ;; Share your solution, and/or check how others did it: 38 | ;; https://gist.github.com/e94c74af8688f5eca19b4979df86f5f9 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_091.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-091 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Graph Connectivity = 5 | ;; By 4Clojure user: lucas1000001 6 | ;; Difficulty: Hard 7 | ;; Tags: [graph-theory] 8 | ;; 9 | ;; Given a graph, determine whether the graph is 10 | ;; connected. A connected graph is such that a path exists 11 | ;; between any two given nodes. 12 | ;; 13 | ;; 14 | ;; -Your function must return true if the graph is 15 | ;; connected and false otherwise. 16 | ;; 17 | ;; 18 | ;; -You will be given a set of tuples representing the 19 | ;; edges of a graph. Each member of a tuple being a 20 | ;; vertex/node in the graph. 21 | ;; 22 | ;; 23 | ;; -Each edge is undirected (can be traversed either 24 | ;; direction). 25 | 26 | (def __ :tests-will-fail) 27 | 28 | (comment 29 | 30 | ) 31 | 32 | (tests 33 | true := (__ #{[:a :a]}) 34 | true := (__ #{[:a :b]}) 35 | false := (__ #{[1 2] [2 3] [3 1] 36 | [4 5] [5 6] [6 4]}) 37 | true := (__ #{[1 2] [2 3] [3 1] 38 | [4 5] [5 6] [6 4] [3 4]}) 39 | false := (__ #{[:a :b] [:b :c] [:c :d] 40 | [:x :y] [:d :a] [:b :e]}) 41 | true := (__ #{[:a :b] [:b :c] [:c :d] 42 | [:x :y] [:d :a] [:b :e] [:x :a]})) 43 | 44 | ;; Share your solution, and/or check how others did it: 45 | ;; https://gist.github.com/8b02371d00dc7b202887d2acf58f3be8 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_131.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-131 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Sum Some Set Subsets = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Medium 7 | ;; Tags: [math] 8 | ;; 9 | ;; Given a variable number of sets of integers, create a 10 | ;; function which returns true iff all of the sets have a 11 | ;; non-empty subset with an equivalent summation. 12 | 13 | (def __ :tests-will-fail) 14 | 15 | (comment 16 | 17 | ) 18 | 19 | (tests 20 | true := (__ #{-1 1 99} 21 | #{-2 2 888} 22 | #{-3 3 7777}) 23 | false := (__ #{1} 24 | #{2} 25 | #{3} 26 | #{4}) 27 | true := (__ #{1}) 28 | false := (__ #{1 -3 51 9} 29 | #{0} 30 | #{9 2 81 33}) 31 | true := (__ #{1 3 5} 32 | #{9 11 4} 33 | #{-3 12 3} 34 | #{-3 4 -2 10}) 35 | false := (__ #{-1 -2 -3 -4 -5 -6} 36 | #{1 2 3 4 5 6 7 8 9}) 37 | true := (__ #{1 3 5 7} 38 | #{2 4 6 8}) 39 | true := (__ #{-1 3 -5 7 -9 11 -13 15} 40 | #{1 -3 5 -7 9 -11 13 -15} 41 | #{1 -1 2 -2 4 -4 8 -8}) 42 | true := (__ #{-10 9 -8 7 -6 5 -4 3 -2 1} 43 | #{10 -9 8 -7 6 -5 4 -3 2 -1})) 44 | 45 | ;; Share your solution, and/or check how others did it: 46 | ;; https://gist.github.com/208b537ca9e4b62751cfa7aa4fdc2461 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_073.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-073 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Analyze a Tic-Tac-Toe Board = 5 | ;; By 4Clojure user: fotland 6 | ;; Difficulty: Hard 7 | ;; Tags: [game] 8 | ;; 9 | ;; A tic-tac-toe board is represented by a two dimensional 10 | ;; vector. X is represented by :x, O is represented by :o, 11 | ;; and empty is represented by :e. A player wins by 12 | ;; placing three Xs or three Os in a horizontal, vertical, 13 | ;; or diagonal row. Write a function which analyzes a 14 | ;; tic-tac-toe board and returns :x if X has won, :o if O 15 | ;; has won, and nil if neither player has won. 16 | 17 | (def __ :tests-will-fail) 18 | 19 | (comment 20 | 21 | ) 22 | 23 | (tests 24 | nil := (__ [[:e :e :e] 25 | [:e :e :e] 26 | [:e :e :e]]) 27 | :x := (__ [[:x :e :o] 28 | [:x :e :e] 29 | [:x :e :o]]) 30 | :o := (__ [[:e :x :e] 31 | [:o :o :o] 32 | [:x :e :x]]) 33 | nil := (__ [[:x :e :o] 34 | [:x :x :e] 35 | [:o :x :o]]) 36 | :x := (__ [[:x :e :e] 37 | [:o :x :e] 38 | [:o :e :x]]) 39 | :o := (__ [[:x :e :o] 40 | [:x :o :e] 41 | [:o :e :x]]) 42 | nil := (__ [[:x :o :x] 43 | [:x :o :x] 44 | [:o :x :o]])) 45 | 46 | ;; Share your solution, and/or check how others did it: 47 | ;; https://gist.github.com/949f75d5329f9d7c290db7fe69ced4d7 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_065.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-065 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Black Box Testing = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs testing] 8 | ;; 9 | ;; Clojure has many sequence types, which act in subtly 10 | ;; different ways. The core functions typically convert 11 | ;; them into a uniform "sequence" type and work with them 12 | ;; that way, but it can be important to understand the 13 | ;; behavioral and performance differences so that you know 14 | ;; which kind is appropriate for your application. 15 | ;; 16 | ;; 17 | ;; Write a function which takes a collection and returns 18 | ;; one of :map, :set, :list, or :vector - describing the 19 | ;; type of collection it was given. 20 | ;; 21 | ;; You won't be allowed to inspect their class or use the 22 | ;; built-in predicates like list? - the point is to poke 23 | ;; at them and understand their behavior. 24 | 25 | (def restricted [class type Class vector? sequential? list? seq? map? set? instance? getClass]) 26 | 27 | (def __ :tests-will-fail) 28 | 29 | (comment 30 | 31 | ) 32 | 33 | (tests 34 | :map := (__ {:a 1, :b 2}) 35 | :list := (__ (range (rand-int 20))) 36 | :vector := (__ [1 2 3 4 5 6]) 37 | :set := (__ #{10 (rand-int 5)}) 38 | [:map :set :vector :list] := (map __ [{} #{} [] ()])) 39 | 40 | ;; Share your solution, and/or check how others did it: 41 | ;; https://gist.github.com/b4436b33f455e68d322970d458830049 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_150.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-150 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Palindromic Numbers = 5 | ;; By 4Clojure user: maximental 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs math] 8 | ;; 9 | ;; A palindromic number is a number that is the same when 10 | ;; written forwards or backwards (e.g., 3, 99, 14341). 11 | ;; 12 | ;; 13 | ;; Write a function which takes an integer n, as its only 14 | ;; argument, and returns an increasing lazy sequence of 15 | ;; all palindromic numbers that are not less than n. 16 | ;; 17 | ;; 18 | ;; The most simple solution will exceed the time limit! 19 | 20 | (def __ :tests-will-fail) 21 | 22 | (comment 23 | 24 | ) 25 | 26 | (tests 27 | (take 26 (__ 0)) := 28 | [0 1 2 3 4 5 6 7 8 9 29 | 11 22 33 44 55 66 77 88 99 30 | 101 111 121 131 141 151 161] 31 | (take 16 (__ 162)) := 32 | [171 181 191 202 33 | 212 222 232 242 34 | 252 262 272 282 35 | 292 303 313 323] 36 | (take 6 (__ 1234550000)) := 37 | [1234554321 1234664321 1234774321 38 | 1234884321 1234994321 1235005321] 39 | (first (__ (* 111111111 111111111))) := 40 | (* 111111111 111111111) 41 | (set (take 199 (__ 0))) := 42 | (set (map #(first (__ %)) (range 0 10000))) 43 | true := 44 | (apply < (take 6666 (__ 9999999))) 45 | (nth (__ 0) 10101) := 46 | 9102019) 47 | 48 | ;; Share your solution, and/or check how others did it: 49 | ;; https://gist.github.com/a8b9c97438851e16448c3133cfbb2c29 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_119.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-119 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Win at Tic-Tac-Toe = 5 | ;; By 4Clojure user: shockbob 6 | ;; Difficulty: Hard 7 | ;; Tags: [game] 8 | ;; 9 | ;; As in Problem 73, a tic-tac-toe board is represented by 10 | ;; a two dimensional vector. X is represented by :x, O is 11 | ;; represented by :o, and empty is represented by :e. 12 | ;; Create a function that accepts a game piece and board 13 | ;; as arguments, and returns a set (possibly empty) of all 14 | ;; valid board placements of the game piece which would 15 | ;; result in an immediate win. 16 | ;; 17 | ;; 18 | ;; Board coordinates should be as in calls to get-in. For 19 | ;; example, [0 1] is the topmost row, center position. 20 | 21 | (def __ :tests-will-fail) 22 | 23 | (comment 24 | 25 | ) 26 | 27 | (tests 28 | (__ :x [[:o :e :e] 29 | [:o :x :o] 30 | [:x :x :e]]) := 31 | #{[2 2] [0 1] [0 2]} 32 | (__ :x [[:x :o :o] 33 | [:x :x :e] 34 | [:e :o :e]]) := 35 | #{[2 2] [1 2] [2 0]} 36 | (__ :x [[:x :e :x] 37 | [:o :x :o] 38 | [:e :o :e]]) := 39 | #{[2 2] [0 1] [2 0]} 40 | (__ :x [[:x :x :o] 41 | [:e :e :e] 42 | [:e :e :e]]) := 43 | #{} 44 | (__ :o [[:x :x :o] 45 | [:o :e :o] 46 | [:x :e :e]]) := 47 | #{[2 2] [1 1]}) 48 | 49 | ;; Share your solution, and/or check how others did it: 50 | ;; https://gist.github.com/26a87db9c4e1431abdf0f60ee3513ad8 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_146.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-146 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Trees into tables = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Easy 7 | ;; Tags: [seqs maps] 8 | ;; 9 | ;; Because Clojure's for macro allows you to "walk" over 10 | ;; multiple sequences in a nested fashion, it is excellent 11 | ;; for transforming all sorts of sequences. If you don't 12 | ;; want a sequence as your final output (say you want a 13 | ;; map), you are often still best-off using for, because 14 | ;; you can produce a sequence and feed it into a map, for 15 | ;; example. 16 | ;; 17 | ;; 18 | ;; For this problem, your goal is to "flatten" a map of 19 | ;; hashmaps. Each key in your output map should be the 20 | ;; "path" 1 that you would have to take in the original 21 | ;; map to get to a value, so for example {1 {2 3}} should 22 | ;; result in {[1 2] 3}. You only need to flatten one level 23 | ;; of maps: if one of the values is a map, just leave it 24 | ;; alone. 25 | ;; 26 | ;; 27 | ;; 1 That is, (get-in original [k1 k2]) should be the 28 | ;; same as (get result [k1 k2]) 29 | 30 | (def __ :tests-will-fail) 31 | 32 | (comment 33 | 34 | ) 35 | 36 | (tests 37 | (__ '{a {p 1, q 2} 38 | b {m 3, n 4}}) := 39 | '{[a p] 1, [a q] 2 40 | [b m] 3, [b n] 4} 41 | (__ '{[1] {a b c d} 42 | [2] {q r s t u v w x}}) := 43 | '{[[1] a] b, [[1] c] d, 44 | [[2] q] r, [[2] s] t, 45 | [[2] u] v, [[2] w] x} 46 | (__ '{m {1 [a b c] 3 nil}}) := 47 | '{[m 1] [a b c], [m 3] nil}) 48 | 49 | ;; Share your solution, and/or check how others did it: 50 | ;; https://gist.github.com/47dc764177c635896e91c4e4a090903c -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_128.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-128 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Recognize Playing Cards = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Easy 7 | ;; Tags: [strings game] 8 | ;; 9 | ;; A standard American deck of playing cards has four 10 | ;; suits - spades, hearts, diamonds, and clubs - and 11 | ;; thirteen cards in each suit. Two is the lowest rank, 12 | ;; followed by other integers up to ten; then the jack, 13 | ;; queen, king, and ace. 14 | ;; 15 | ;; 16 | ;; It's convenient for humans to represent these cards as 17 | ;; suit/rank pairs, such as H5 or DQ: the heart five and 18 | ;; diamond queen respectively. But these forms are not 19 | ;; convenient for programmers, so to write a card game you 20 | ;; need some way to parse an input string into meaningful 21 | ;; components. For purposes of determining rank, we will 22 | ;; define the cards to be valued from 0 (the two) to 12 23 | ;; (the ace) 24 | ;; 25 | ;; 26 | ;; Write a function which converts (for example) the 27 | ;; string "SJ" into a map of {:suit :spade, :rank 9}. A 28 | ;; ten will always be represented with the single 29 | ;; character "T", rather than the two characters "10". 30 | 31 | (def __ :tests-will-fail) 32 | 33 | (comment 34 | 35 | ) 36 | 37 | (tests 38 | {:suit :diamond :rank 10} := (__ "DQ") 39 | {:suit :heart :rank 3} := (__ "H5") 40 | {:suit :club :rank 12} := (__ "CA") 41 | (range 13) := (map (comp :rank __ str) 42 | '[S2 S3 S4 S5 S6 S7 43 | S8 S9 ST SJ SQ SK SA])) 44 | 45 | ;; Share your solution, and/or check how others did it: 46 | ;; https://gist.github.com/462630c19a4463c7a67c4af684c9f71e -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_138.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-138 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Squares Squared = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Hard 7 | ;; Tags: [data-juggling] 8 | ;; 9 | ;; Create a function of two integer arguments: the start 10 | ;; and end, respectively. You must create a vector of 11 | ;; strings which renders a 45° rotated square of integers 12 | ;; which are successive squares from the start point up to 13 | ;; and including the end point. If a number comprises 14 | ;; multiple digits, wrap them around the shape 15 | ;; individually. If there are not enough digits to 16 | ;; complete the shape, fill in the rest with asterisk 17 | ;; characters. The direction of the drawing should be 18 | ;; clockwise, starting from the center of the shape and 19 | ;; working outwards, with the initial direction being down 20 | ;; and to the right. 21 | 22 | (def __ :tests-will-fail) 23 | 24 | (comment 25 | 26 | ) 27 | 28 | (tests 29 | (__ 2 2) := ["2"] 30 | (__ 2 4) := [" 2 " 31 | "* 4" 32 | " * "] 33 | (__ 3 81) := [" 3 " 34 | "1 9" 35 | " 8 "] 36 | (__ 4 20) := [" 4 " 37 | "* 1" 38 | " 6 "] 39 | (__ 2 256) := [" 6 " 40 | " 5 * " 41 | "2 2 *" 42 | " 6 4 " 43 | " 1 "] 44 | (__ 10 10000) := [" 0 " 45 | " 1 0 " 46 | " 0 1 0 " 47 | "* 0 0 0" 48 | " * 1 * " 49 | " * * " 50 | " * "]) 51 | 52 | ;; Share your solution, and/or check how others did it: 53 | ;; https://gist.github.com/6c062309b30e92df2388c081427a5fd4 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_178.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-178 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Best Hand = 5 | ;; By 4Clojure user: toolkit 6 | ;; Difficulty: Hard 7 | ;; Tags: [strings game] 8 | ;; 9 | ;; Following on from Recognize Playing Cards, determine 10 | ;; the best poker hand that can be made with five cards. 11 | ;; The hand rankings are listed below for your 12 | ;; convenience. 13 | ;; 14 | ;; * Straight flush: All cards in the same suit, and in 15 | ;; sequence 16 | ;; * Four of a kind: Four of the cards have the same rank 17 | ;; * Full House: Three cards of one rank, the other two of 18 | ;; another rank 19 | ;; * Flush: All cards in the same suit 20 | ;; * Straight: All cards in sequence (aces can be high or 21 | ;; low, but not both at once) 22 | ;; * Three of a kind: Three of the cards have the same 23 | ;; rank 24 | ;; * Two pair: Two pairs of cards have the same rank 25 | ;; * Pair: Two cards have the same rank 26 | ;; * High card: None of the above conditions are met 27 | 28 | (def __ :tests-will-fail) 29 | 30 | (comment 31 | 32 | ) 33 | 34 | (tests 35 | :high-card := (__ ["HA" "D2" "H3" "C9" "DJ"]) 36 | :pair := (__ ["HA" "HQ" "SJ" "DA" "HT"]) 37 | :two-pair := (__ ["HA" "DA" "HQ" "SQ" "HT"]) 38 | :three-of-a-kind := (__ ["HA" "DA" "CA" "HJ" "HT"]) 39 | :straight := (__ ["HA" "DK" "HQ" "HJ" "HT"]) 40 | :straight := (__ ["HA" "H2" "S3" "D4" "C5"]) 41 | :flush := (__ ["HA" "HK" "H2" "H4" "HT"]) 42 | :full-house := (__ ["HA" "DA" "CA" "HJ" "DJ"]) 43 | :four-of-a-kind := (__ ["HA" "DA" "CA" "SA" "DJ"]) 44 | :straight-flush := (__ ["HA" "HK" "HQ" "HJ" "HT"])) 45 | 46 | ;; Share your solution, and/or check how others did it: 47 | ;; https://gist.github.com/c4520f456f1200f3690bace30d9e26f9 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_195.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-195 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Parentheses... Again = 5 | ;; By 4Clojure user: djtrack16 6 | ;; Difficulty: Medium 7 | ;; Tags: [math combinatorics] 8 | ;; 9 | ;; In a family of languages like Lisp, having balanced 10 | ;; parentheses is a defining feature of the language. 11 | ;; Luckily, Lisp has almost no syntax, except for these 12 | ;; "delimiters" -- and that hardly qualifies as "syntax", 13 | ;; at least in any useful computer programming sense. 14 | ;; 15 | ;; 16 | ;; It is not a difficult exercise to find all the 17 | ;; combinations of well-formed parentheses if we only have 18 | ;; N pairs to work with. For instance, if we only have 2 19 | ;; pairs, we only have two possible combinations: "()()" 20 | ;; and "(())". Any other combination of length 4 is 21 | ;; ill-formed. Can you see why? 22 | ;; 23 | ;; 24 | ;; Generate all possible combinations of well-formed 25 | ;; parentheses of length 2n (n pairs of parentheses). For 26 | ;; this problem, we only consider '(' and ')', but the 27 | ;; answer is similar if you work with only {} or only []. 28 | ;; 29 | ;; 30 | ;; There is an interesting pattern in the numbers! 31 | 32 | (def __ :tests-will-fail) 33 | 34 | (comment 35 | 36 | ) 37 | 38 | (tests 39 | [#{""} #{"()"} #{"()()" "(())"}] := (map (fn [n] (__ n)) [0 1 2]) 40 | #{"((()))" "()()()" "()(())" "(())()" "(()())"} := (__ 3) 41 | 16796 := (count (__ 10)) 42 | (nth (sort (filter #(.contains ^String % "(()()()())") (__ 9))) 6) := "(((()()()())(())))" 43 | (nth (sort (__ 12)) 5000) := "(((((()()()()()))))(()))") 44 | 45 | ;; Share your solution, and/or check how others did it: 46 | ;; https://gist.github.com/2e0e77396aba863340bdf804ecb8c35c -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_124.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-124 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Analyze Reversi = 5 | ;; By 4Clojure user: shockbob 6 | ;; Difficulty: Hard 7 | ;; Tags: [game] 8 | ;; 9 | ;; Reversi is normally played on an 8 by 8 board. In this 10 | ;; problem, a 4 by 4 board is represented as a 11 | ;; two-dimensional vector with black, white, and empty 12 | ;; pieces represented by 'b, 'w, and 'e, respectively. 13 | ;; Create a function that accepts a game board and color 14 | ;; as arguments, and returns a map of legal moves for that 15 | ;; color. Each key should be the coordinates of a legal 16 | ;; move, and its value a set of the coordinates of the 17 | ;; pieces flipped by that move. 18 | ;; 19 | ;; 20 | ;; Board coordinates should be as in calls to get-in. For 21 | ;; example, [0 1] is the topmost row, second column from 22 | ;; the left. 23 | 24 | (def __ :tests-will-fail) 25 | 26 | (comment 27 | 28 | ) 29 | 30 | (tests 31 | {[1 3] #{[1 2]}, [0 2] #{[1 2]}, [3 1] #{[2 1]}, [2 0] #{[2 1]}} := 32 | (__ '[[e e e e] 33 | [e w b e] 34 | [e b w e] 35 | [e e e e]] 'w) 36 | {[3 2] #{[2 2]}, [3 0] #{[2 1]}, [1 0] #{[1 1]}} := 37 | (__ '[[e e e e] 38 | [e w b e] 39 | [w w w e] 40 | [e e e e]] 'b) 41 | {[0 3] #{[1 2]}, [1 3] #{[1 2]}, [3 3] #{[2 2]}, [2 3] #{[2 2]}} := 42 | (__ '[[e e e e] 43 | [e w b e] 44 | [w w b e] 45 | [e e b e]] 'w) 46 | {[0 3] #{[2 1] [1 2]}, [1 3] #{[1 2]}, [2 3] #{[2 1] [2 2]}} := 47 | (__ '[[e e w e] 48 | [b b w e] 49 | [b w w e] 50 | [b w w w]] 'b)) 51 | 52 | ;; Share your solution, and/or check how others did it: 53 | ;; https://gist.github.com/8a958ec70d026ea9435e9c03f6538cc0 -------------------------------------------------------------------------------- /src/rich4clojure/easy/problem_153.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.easy.problem-153 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Pairwise Disjoint Sets = 5 | ;; By 4Clojure user: maximental 6 | ;; Difficulty: Easy 7 | ;; Tags: [set-theory] 8 | ;; 9 | ;; Given a set of sets, create a function which returns 10 | ;; true if no two of those sets have any elements in 11 | ;; common 1 and false otherwise. Some of the test cases 12 | ;; are a bit tricky, so pay a little more attention to 13 | ;; them. 14 | ;; 15 | ;; 16 | ;; 1 Such sets are usually called pairwise disjoint or 17 | ;; mutually disjoint . 18 | 19 | (def __ :tests-will-fail) 20 | 21 | (comment 22 | 23 | ) 24 | 25 | (tests 26 | (__ #{#{\U} #{\s} #{\e \R \E} #{\P \L} #{\.}}) := 27 | true 28 | (__ #{#{:a :b :c :d :e} 29 | #{:a :b :c :d} 30 | #{:a :b :c} 31 | #{:a :b} 32 | #{:a}}) := 33 | false 34 | (__ #{#{[1 2 3] [4 5]} 35 | #{[1 2] [3 4 5]} 36 | #{[1] [2] 3 4 5} 37 | #{1 2 [3 4] [5]}}) := 38 | true 39 | (__ #{#{'a 'b} 40 | #{'c 'd 'e} 41 | #{'f 'g 'h 'i} 42 | #{''a ''c ''f}}) := 43 | true 44 | (__ #{#{'(:x :y :z) '(:x :y) '(:z) '()} 45 | #{#{:x :y :z} #{:x :y} #{:z} #{}} 46 | #{'[:x :y :z] [:x :y] [:z] [] {}}}) := 47 | false 48 | (__ #{#{(= "true") false} 49 | #{:yes :no} 50 | #{(class 1) 0} 51 | #{(symbol "true") 'false} 52 | #{(keyword "yes") ::no} 53 | #{(class '1) (int \0)}}) := 54 | false 55 | (__ #{#{distinct?} 56 | #{#(-> %) #(-> %)} 57 | #{#(-> %) #(-> %) #(-> %)} 58 | #{#(-> %) #(-> %) #(-> %)}}) := 59 | true 60 | (__ #{#{(#(-> *)) + (quote mapcat) #_ nil} 61 | #{'+ '* mapcat (comment mapcat)} 62 | #{(do) set contains? nil?} 63 | #{, , , #_, , empty?}}) := 64 | false) 65 | 66 | ;; Share your solution, and/or check how others did it: 67 | ;; https://gist.github.com/2993425c507c8be6d1abeae28f8e2511 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_094.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-094 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Game of Life = 5 | ;; By 4Clojure user: lucas1000001 6 | ;; Difficulty: Hard 7 | ;; Tags: [game] 8 | ;; 9 | ;; The game of life is a cellular automaton devised by 10 | ;; mathematician John Conway. 11 | ;; 12 | ;; 13 | ;; The 'board' consists of both live (#) and dead () 14 | ;; cells. Each cell interacts with its eight neighbours 15 | ;; (horizontal, vertical, diagonal), and its next state is 16 | ;; dependent on the following rules: 17 | ;; 18 | ;; 19 | ;; 1) Any live cell with fewer than two live neighbours 20 | ;; dies, as if caused by under-population. 21 | ;; 22 | ;; 2) Any live cell with two or three live neighbours 23 | ;; lives on to the next generation. 24 | ;; 25 | ;; 3) Any live cell with more than three live neighbours 26 | ;; dies, as if by overcrowding. 27 | ;; 28 | ;; 4) Any dead cell with exactly three live neighbours 29 | ;; becomes a live cell, as if by reproduction. 30 | ;; 31 | ;; 32 | ;; Write a function that accepts a board, and returns a 33 | ;; board representing the next generation of cells. 34 | 35 | (def __ :tests-will-fail) 36 | 37 | (comment 38 | 39 | ) 40 | 41 | (tests 42 | (__ [" " 43 | " ## " 44 | " ## " 45 | " ## " 46 | " ## " 47 | " "]) := 48 | [" " 49 | " ## " 50 | " # " 51 | " # " 52 | " ## " 53 | " "] 54 | (__ [" " 55 | " " 56 | " ### " 57 | " " 58 | " "]) := 59 | [" " 60 | " # " 61 | " # " 62 | " # " 63 | " "] 64 | (__ [" " 65 | " " 66 | " ### " 67 | " ### " 68 | " " 69 | " "]) := 70 | [" " 71 | " # " 72 | " # # " 73 | " # # " 74 | " # " 75 | " "]) 76 | 77 | ;; Share your solution, and/or check how others did it: 78 | ;; https://gist.github.com/32267ae08ba3fd2f0fc18cd85f2cfb4c -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_111.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-111 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Crossword puzzle = 5 | ;; By 4Clojure user: mlni 6 | ;; Difficulty: Hard 7 | ;; Tags: [game] 8 | ;; 9 | ;; Write a function that takes a string and a 10 | ;; partially-filled crossword puzzle board, and determines 11 | ;; if the input string can be legally placed onto the 12 | ;; board. 13 | ;; 14 | ;; 15 | ;; 16 | ;; The crossword puzzle board consists of a collection of 17 | ;; partially-filled rows. Empty spaces are denoted with an 18 | ;; underscore (_), unusable spaces are denoted with a hash 19 | ;; symbol (#), and pre-filled spaces have a character in 20 | ;; place; the whitespace characters are for legibility and 21 | ;; should be ignored. 22 | ;; 23 | ;; 24 | ;; For a word to be legally placed on the board: 25 | ;; 26 | ;; - It may use empty spaces (underscores) 27 | ;; 28 | ;; - It may use but must not conflict with any pre-filled 29 | ;; characters. 30 | ;; 31 | ;; - It must not use any unusable spaces (hashes). 32 | ;; 33 | ;; - There must be no empty spaces (underscores) or extra 34 | ;; characters before or after the word (the word may be 35 | ;; bound by unusable spaces though). 36 | ;; 37 | ;; - Characters are not case-sensitive. 38 | ;; 39 | ;; - Words may be placed vertically (proceeding top-down 40 | ;; only), or horizontally (proceeding left-right only). 41 | 42 | (def __ :tests-will-fail) 43 | 44 | (comment 45 | 46 | ) 47 | 48 | (tests 49 | true := (__ "the" ["_ # _ _ e"]) 50 | false := (__ "the" ["c _ _ _" 51 | "d _ # e" 52 | "r y _ _"]) 53 | true := (__ "joy" ["c _ _ _" 54 | "d _ # e" 55 | "r y _ _"]) 56 | false := (__ "joy" ["c o n j" 57 | "_ _ y _" 58 | "r _ _ #"]) 59 | true := (__ "clojure" ["_ _ _ # j o y" 60 | "_ _ o _ _ _ _" 61 | "_ _ f _ # _ _"])) 62 | 63 | ;; Share your solution, and/or check how others did it: 64 | ;; https://gist.github.com/0e3f475f79893da6587e2af394f07ed4 -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_141.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-141 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Tricky card games = 5 | ;; By 4Clojure user: amalloy 6 | ;; Difficulty: Medium 7 | ;; Tags: [game cards] 8 | ;; 9 | ;; In trick-taking card games such as bridge, spades, or 10 | ;; hearts, cards are played in groups known as "tricks" - 11 | ;; each player plays a single card, in order; the first 12 | ;; player is said to "lead" to the trick. After all 13 | ;; players have played, one card is said to have "won" the 14 | ;; trick. How the winner is determined will vary by game, 15 | ;; but generally the winner is the highest card played in 16 | ;; the suit that was led. Sometimes (again varying by 17 | ;; game), a particular suit will be designated "trump", 18 | ;; meaning that its cards are more powerful than any 19 | ;; others: if there is a trump suit, and any trumps are 20 | ;; played, then the highest trump wins regardless of what 21 | ;; was led. 22 | ;; 23 | ;; Your goal is to devise a function that can determine 24 | ;; which of a number of cards has won a trick. You should 25 | ;; accept a trump suit, and return a function winner. 26 | ;; Winner will be called on a sequence of cards, and 27 | ;; should return the one which wins the trick. Cards will 28 | ;; be represented in the format returned by Problem 128, 29 | ;; Recognize Playing Cards : a hash-map of :suit and a 30 | ;; numeric :rank. Cards with a larger rank are stronger. 31 | 32 | (def __ :tests-will-fail) 33 | 34 | (comment 35 | 36 | ) 37 | 38 | (tests 39 | [notrump (__ nil)] := 40 | (and (= {:suit :club :rank 9} (notrump [{:suit :club :rank 4} 41 | {:suit :club :rank 9}])) 42 | (= {:suit :spade :rank 2} (notrump [{:suit :spade :rank 2} 43 | {:suit :club :rank 10}]))) 44 | {:suit :club :rank 10} := ((__ :club) [{:suit :spade :rank 2} 45 | {:suit :club :rank 10}]) 46 | {:suit :heart :rank 8} := 47 | ((__ :heart) [{:suit :heart :rank 6} {:suit :heart :rank 8} 48 | {:suit :diamond :rank 10} {:suit :heart :rank 4}])) 49 | 50 | ;; Share your solution, and/or check how others did it: 51 | ;; https://gist.github.com/7e0c1414a4608324194462939c7d6121 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_117.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-117 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = For Science! = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Hard 7 | ;; Tags: [game] 8 | ;; 9 | ;; A mad scientist with tenure has created an experiment 10 | ;; tracking mice in a maze. Several mazes have been 11 | ;; randomly generated, and you've been tasked with writing 12 | ;; a program to determine the mazes in which it's possible 13 | ;; for the mouse to reach the cheesy endpoint. Write a 14 | ;; function which accepts a maze in the form of a 15 | ;; collection of rows, each row is a string where: 16 | ;; * spaces represent areas where the mouse can walk 17 | ;; freely 18 | ;; * hashes (#) represent walls where the mouse can not 19 | ;; walk 20 | ;; * M represents the mouse's starting point 21 | ;; * C represents the cheese which the mouse must reach 22 | ;; The mouse is not allowed to travel diagonally in the 23 | ;; maze (only up/down/left/right), nor can he escape the 24 | ;; edge of the maze. Your function must return true iff 25 | ;; the maze is solvable by the mouse. 26 | 27 | (def __ :tests-will-fail) 28 | 29 | (comment 30 | 31 | ) 32 | 33 | (tests 34 | true := (__ ["M C"]) 35 | false := (__ ["M # C"]) 36 | true := (__ ["#######" 37 | "# #" 38 | "# # #" 39 | "#M # C#" 40 | "#######"]) 41 | false := (__ ["########" 42 | "#M # #" 43 | "# # #" 44 | "# # # #" 45 | "# # #" 46 | "# # #" 47 | "# # # #" 48 | "# # #" 49 | "# # C#" 50 | "########"]) 51 | false := (__ ["M " 52 | " " 53 | " " 54 | " " 55 | " ##" 56 | " #C"]) 57 | true := (__ ["C######" 58 | " # " 59 | " # # " 60 | " # #M" 61 | " # "]) 62 | true := (__ ["C# # # #" 63 | " " 64 | "# # # # " 65 | " " 66 | " # # # #" 67 | " " 68 | "# # # #M"])) 69 | 70 | ;; Share your solution, and/or check how others did it: 71 | ;; https://gist.github.com/5476ec0b709661a0f20937546efd630b -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_130.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-130 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Tree reparenting = 5 | ;; By 4Clojure user: chouser 6 | ;; Difficulty: Hard 7 | ;; Tags: [tree] 8 | ;; 9 | ;; Every node of a tree is connected to each of its 10 | ;; children as well as its parent. One can imagine 11 | ;; grabbing one node of a tree and dragging it up to the 12 | ;; root position, leaving all connections intact. For 13 | ;; example, below on the left is a binary tree. By pulling 14 | ;; the "c" node up to the root, we obtain the tree on the 15 | ;; right. 16 | ;; 17 | ;; 18 | ;; Note it is no longer binary as "c" had three 19 | ;; connections total -- two children and one parent. 20 | ;; 21 | ;; Each node is represented as a vector, which always has 22 | ;; at least one element giving the name of the node as a 23 | ;; symbol. Subsequent items in the vector represent the 24 | ;; children of the node. Because the children are ordered 25 | ;; it's important that the tree you return keeps the 26 | ;; children of each node in order and that the old parent 27 | ;; node, if any, is appended on the right. 28 | ;; 29 | ;; Your function will be given two args -- the name of 30 | ;; the node that should become the new root, and the tree 31 | ;; to transform. 32 | 33 | (def __ :tests-will-fail) 34 | 35 | (comment 36 | 37 | ) 38 | 39 | (tests 40 | '(n) := 41 | (__ 'n '(n)) 42 | '(a (t (e))) := 43 | (__ 'a '(t (e) (a))) 44 | '(e (t (a))) := 45 | (__ 'e '(a (t (e)))) 46 | '(a (b (c))) := 47 | (__ 'a '(c (b (a)))) 48 | '(d 49 | (b 50 | (c) 51 | (e) 52 | (a 53 | (f 54 | (g) 55 | (h))))) := 56 | (__ 'd '(a 57 | (b 58 | (c) 59 | (d) 60 | (e)) 61 | (f 62 | (g) 63 | (h)))) 64 | '(c 65 | (d) 66 | (e) 67 | (b 68 | (f 69 | (g) 70 | (h)) 71 | (a 72 | (i 73 | (j 74 | (k) 75 | (l)) 76 | (m 77 | (n) 78 | (o)))))) := 79 | (__ 'c '(a 80 | (b 81 | (c 82 | (d) 83 | (e)) 84 | (f 85 | (g) 86 | (h))) 87 | (i 88 | (j 89 | (k) 90 | (l)) 91 | (m 92 | (n) 93 | (o)))))) 94 | 95 | ;; Share your solution, and/or check how others did it: 96 | ;; https://gist.github.com/58bf3dd77606f53cced382363c343ed4 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_127.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-127 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Love Triangle = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Hard 7 | ;; Tags: [search data-juggling] 8 | ;; 9 | ;; Everyone loves triangles, and it's easy to understand 10 | ;; why—they're so wonderfully symmetric (except scalenes, 11 | ;; they suck). 12 | ;; 13 | ;; 14 | ;; Your passion for triangles has led you to become a 15 | ;; miner (and part-time Clojure programmer) where you work 16 | ;; all day to chip out isosceles-shaped minerals from 17 | ;; rocks gathered in a nearby open-pit mine. There are too 18 | ;; many rocks coming from the mine to harvest them all so 19 | ;; you've been tasked with writing a program to analyze 20 | ;; the mineral patterns of each rock, and determine which 21 | ;; rocks have the biggest minerals. 22 | ;; 23 | ;; 24 | ;; Someone has already written a computer-vision system 25 | ;; for the mine. It images each rock as it comes into the 26 | ;; processing centre and creates a cross-sectional bitmap 27 | ;; of mineral (1) and rock (0) concentrations for each 28 | ;; one. 29 | ;; 30 | ;; 31 | ;; You must now create a function which accepts a 32 | ;; collection of integers, each integer when read in 33 | ;; base-2 gives the bit-representation of the rock (again, 34 | ;; 1s are mineral and 0s are worthless scalene-like rock). 35 | ;; You must return the cross-sectional area of the largest 36 | ;; harvestable mineral from the input rock, as follows: 37 | ;; 38 | ;; * The minerals only have smooth faces when sheared 39 | ;; vertically or horizontally from the rock's 40 | ;; cross-section 41 | ;; * The mine is only concerned with harvesting isosceles 42 | ;; triangles (such that one or two sides can be sheared) 43 | ;; * If only one face of the mineral is sheared, its 44 | ;; opposing vertex must be a point (ie. the smooth face 45 | ;; must be of odd length), and its two equal-length sides 46 | ;; must intersect the shear face at 45° (ie. those sides 47 | ;; must cut even-diagonally) 48 | ;; * The harvested mineral may not contain any traces of 49 | ;; rock 50 | ;; * The mineral may lie in any orientation in the plane 51 | ;; * Area should be calculated as the sum of 1s that 52 | ;; comprise the mineral 53 | ;; * Minerals must have a minimum of three measures of 54 | ;; area to be harvested 55 | ;; * If no minerals can be harvested from the rock, your 56 | ;; function should return nil 57 | 58 | (def __ :tests-will-fail) 59 | 60 | (comment 61 | 62 | ) 63 | 64 | (tests 65 | 10 := (__ [15 15 15 15 15]) 66 | 15 := (__ [1 3 7 15 31]) 67 | 3 := (__ [3 3]) 68 | 4 := (__ [7 3]) 69 | 6 := (__ [17 22 6 14 22]) 70 | 9 := (__ [18 7 14 14 6 3]) 71 | nil := (__ [21 10 21 10]) 72 | nil := (__ [0 31 0 31 0])) 73 | 74 | ;; Share your solution, and/or check how others did it: 75 | ;; https://gist.github.com/cc4268faf5c59e3ce0d1a309228a78ae -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_140.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-140 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Veitch, Please! = 5 | ;; By 4Clojure user: amcnamara 6 | ;; Difficulty: Hard 7 | ;; Tags: [math circuit-design] 8 | ;; 9 | ;; Create a function which accepts as input a boolean 10 | ;; algebra function in the form of a set of sets, where 11 | ;; the inner sets are collections of symbols corresponding 12 | ;; to the input boolean variables which satisfy the 13 | ;; function (the inputs of the inner sets are conjoint, 14 | ;; and the sets themselves are disjoint... also known as 15 | ;; canonical minterms). Note: capitalized symbols 16 | ;; represent truth, and lower-case symbols represent 17 | ;; negation of the inputs. Your function must return the 18 | ;; minimal function which is logically equivalent to the 19 | ;; input. 20 | ;; 21 | ;; 22 | ;; PS — You may want to give this a read before 23 | ;; proceeding: K-Maps 24 | 25 | (def __ :tests-will-fail) 26 | 27 | (comment 28 | 29 | ) 30 | 31 | (tests 32 | (__ #{#{'a 'B 'C 'd} 33 | #{'A 'b 'c 'd} 34 | #{'A 'b 'c 'D} 35 | #{'A 'b 'C 'd} 36 | #{'A 'b 'C 'D} 37 | #{'A 'B 'c 'd} 38 | #{'A 'B 'c 'D} 39 | #{'A 'B 'C 'd}}) := 40 | #{#{'A 'c} 41 | #{'A 'b} 42 | #{'B 'C 'd}} 43 | (__ #{#{'A 'B 'C 'D} 44 | #{'A 'B 'C 'd}}) := 45 | #{#{'A 'B 'C}} 46 | (__ #{#{'a 'b 'c 'd} 47 | #{'a 'B 'c 'd} 48 | #{'a 'b 'c 'D} 49 | #{'a 'B 'c 'D} 50 | #{'A 'B 'C 'd} 51 | #{'A 'B 'C 'D} 52 | #{'A 'b 'C 'd} 53 | #{'A 'b 'C 'D}}) := 54 | #{#{'a 'c} 55 | #{'A 'C}} 56 | (__ #{#{'a 'b 'c} 57 | #{'a 'B 'c} 58 | #{'a 'b 'C} 59 | #{'a 'B 'C}}) := 60 | #{#{'a}} 61 | (__ #{#{'a 'B 'c 'd} 62 | #{'A 'B 'c 'D} 63 | #{'A 'b 'C 'D} 64 | #{'a 'b 'c 'D} 65 | #{'a 'B 'C 'D} 66 | #{'A 'B 'C 'd}}) := 67 | #{#{'a 'B 'c 'd} 68 | #{'A 'B 'c 'D} 69 | #{'A 'b 'C 'D} 70 | #{'a 'b 'c 'D} 71 | #{'a 'B 'C 'D} 72 | #{'A 'B 'C 'd}} 73 | (__ #{#{'a 'b 'c 'd} 74 | #{'a 'B 'c 'd} 75 | #{'A 'B 'c 'd} 76 | #{'a 'b 'c 'D} 77 | #{'a 'B 'c 'D} 78 | #{'A 'B 'c 'D}}) := 79 | #{#{'a 'c} 80 | #{'B 'c}} 81 | (__ #{#{'a 'B 'c 'd} 82 | #{'A 'B 'c 'd} 83 | #{'a 'b 'c 'D} 84 | #{'a 'b 'C 'D} 85 | #{'A 'b 'c 'D} 86 | #{'A 'b 'C 'D} 87 | #{'a 'B 'C 'd} 88 | #{'A 'B 'C 'd}}) := 89 | #{#{'B 'd} 90 | #{'b 'D}} 91 | (__ #{#{'a 'b 'c 'd} 92 | #{'A 'b 'c 'd} 93 | #{'a 'B 'c 'D} 94 | #{'A 'B 'c 'D} 95 | #{'a 'B 'C 'D} 96 | #{'A 'B 'C 'D} 97 | #{'a 'b 'C 'd} 98 | #{'A 'b 'C 'd}}) := 99 | #{#{'B 'D} 100 | #{'b 'd}}) 101 | 102 | ;; Share your solution, and/or check how others did it: 103 | ;; https://gist.github.com/27a9305db8ae02a33054c1b730c2ef6d -------------------------------------------------------------------------------- /src/rich4clojure/medium/problem_168.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.medium.problem-168 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Infinite Matrix = 5 | ;; By 4Clojure user: maximental 6 | ;; Difficulty: Medium 7 | ;; Tags: [seqs recursion math] 8 | ;; 9 | ;; In what follows, m, n, s, t denote nonnegative 10 | ;; integers, f denotes a function that accepts two 11 | ;; arguments and is defined for all nonnegative integers 12 | ;; in both arguments. 13 | ;; 14 | ;; 15 | ;; In mathematics, the function f can be interpreted as 16 | ;; an infinite matrix with infinitely many rows and 17 | ;; columns that, when written, looks like an ordinary 18 | ;; matrix but its rows and columns cannot be written down 19 | ;; completely, so are terminated with ellipses. In 20 | ;; Clojure, such infinite matrix can be represented as an 21 | ;; infinite lazy sequence of infinite lazy sequences, 22 | ;; where the inner sequences represent rows. 23 | ;; 24 | ;; 25 | ;; Write a function that accepts 1, 3 and 5 arguments 26 | ;; * with the argument f, it returns the infinite matrix A 27 | ;; that has the entry in the i -th row and the j -th 28 | ;; column equal to f(i,j) for i,j = 0,1,2,... ; 29 | ;; * with the arguments f, m, n, it returns the infinite 30 | ;; matrix B that equals the remainder of the matrix A 31 | ;; after the removal of the first m rows and the first n 32 | ;; columns; 33 | ;; * with the arguments f, m, n, s, t, it returns the 34 | ;; finite s-by-t matrix that consists of the first t 35 | ;; entries of each of the first s rows of the matrix B or, 36 | ;; equivalently, that consists of the first s entries of 37 | ;; each of the first t columns of the matrix B . 38 | 39 | (def restricted [for range iterate repeat cycle drop]) 40 | 41 | (def __ :tests-will-fail) 42 | 43 | (comment 44 | 45 | ) 46 | 47 | (tests 48 | (take 5 (map #(take 6 %) (__ str))) := 49 | [["00" "01" "02" "03" "04" "05"] 50 | ["10" "11" "12" "13" "14" "15"] 51 | ["20" "21" "22" "23" "24" "25"] 52 | ["30" "31" "32" "33" "34" "35"] 53 | ["40" "41" "42" "43" "44" "45"]] 54 | (take 6 (map #(take 5 %) (__ str 3 2))) := 55 | [["32" "33" "34" "35" "36"] 56 | ["42" "43" "44" "45" "46"] 57 | ["52" "53" "54" "55" "56"] 58 | ["62" "63" "64" "65" "66"] 59 | ["72" "73" "74" "75" "76"] 60 | ["82" "83" "84" "85" "86"]] 61 | (__ * 3 5 5 7) := 62 | [[15 18 21 24 27 30 33] 63 | [20 24 28 32 36 40 44] 64 | [25 30 35 40 45 50 55] 65 | [30 36 42 48 54 60 66] 66 | [35 42 49 56 63 70 77]] 67 | (__ #(/ % (inc %2)) 1 0 6 4) := 68 | [[1/1 1/2 1/3 1/4] 69 | [2/1 2/2 2/3 1/2] 70 | [3/1 3/2 3/3 3/4] 71 | [4/1 4/2 4/3 4/4] 72 | [5/1 5/2 5/3 5/4] 73 | [6/1 6/2 6/3 6/4]] 74 | (class (__ (juxt bit-or bit-xor))) := 75 | (class (__ (juxt quot mod) 13 21)) 76 | (class (lazy-seq)) 77 | (class (nth (__ (constantly 10946)) 34)) := 78 | (class (nth (__ (constantly 0) 5 8) 55)) 79 | (class (lazy-seq)) 80 | (let [m 377 n 610 w 987 81 | check (fn [f s] (every? true? (map-indexed f s))) 82 | row (take w (nth (__ vector) m)) 83 | column (take w (map first (__ vector m n))) 84 | diagonal (map-indexed #(nth %2 %) (__ vector m n w w))] 85 | (and (check #(= %2 [m %]) row) 86 | (check #(= %2 [(+ m %) n]) column) 87 | (check #(= %2 [(+ m %) (+ n %)]) diagonal))) := 88 | true) 89 | 90 | ;; Share your solution, and/or check how others did it: 91 | ;; https://gist.github.com/eb5fc589dbf145b47bb8370b9c8d25e1 -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_152.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-152 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Latin Square Slicing = 5 | ;; By 4Clojure user: maximental 6 | ;; Difficulty: Hard 7 | ;; Tags: [data-analysis math] 8 | ;; 9 | ;; A Latin square of order n is an n x n array that 10 | ;; contains n different elements, each occurring exactly 11 | ;; once in each row, and exactly once in each column. For 12 | ;; example, among the following arrays only the first one 13 | ;; forms a Latin square: A B C A B C A B C B C A B C A B D 14 | ;; A C A B C A C C A B 15 | ;; 16 | ;; 17 | ;; Let V be a vector of such vectors 1 that they may 18 | ;; differ in length 2. We will say that an arrangement of 19 | ;; vectors of V in consecutive rows is an alignment (of 20 | ;; vectors) of V if the following conditions are 21 | ;; satisfied: 22 | ;; * All vectors of V are used. 23 | ;; * Each row contains just one vector. 24 | ;; * The order of V is preserved. 25 | ;; * All vectors of maximal length are horizontally 26 | ;; aligned each other. 27 | ;; * If a vector is not of maximal length then all its 28 | ;; elements are aligned with elements of some subvector of 29 | ;; a vector of maximal length. Let L denote a Latin square 30 | ;; of order 2 or greater. We will say that L is included 31 | ;; in V or that V includes L iff there exists an alignment 32 | ;; of V such that contains a subsquare that is equal to L. 33 | ;; 34 | ;; 35 | ;; For example, if V equals [[1 2 3][2 3 1 2 1][3 1 2]] 36 | ;; then there are nine alignments of V (brackets omitted): 37 | ;; 1 2 3 38 | ;; 39 | ;; 1 2 3 1 2 3 1 2 3 A 2 3 1 2 1 2 3 1 2 1 2 3 1 2 1 3 1 40 | ;; 2 3 1 2 3 1 2 41 | ;; 42 | ;; 1 2 3 1 2 3 1 2 3 B 2 3 1 2 1 2 3 1 2 1 2 3 1 2 1 3 1 43 | ;; 2 3 1 2 3 1 2 44 | ;; 45 | ;; 1 2 3 1 2 3 1 2 3 C 2 3 1 2 1 2 3 1 2 1 2 3 1 2 1 3 1 46 | ;; 2 3 1 2 3 1 2 Alignment A1 contains Latin square [[1 2 47 | ;; 3][2 3 1][3 1 2]], alignments A2, A3, B1, B2, B3 48 | ;; contain no Latin squares, and alignments C1, C2, C3 49 | ;; contain [[2 1][1 2]]. Thus in this case V includes one 50 | ;; Latin square of order 3 and one of order 2 which is 51 | ;; included three times. 52 | ;; 53 | ;; 54 | ;; Our aim is to implement a function which accepts a 55 | ;; vector of vectors V as an argument, and returns a map 56 | ;; which keys and values are integers. Each key should be 57 | ;; the order of a Latin square included in V, and its 58 | ;; value a count of different Latin squares of that order 59 | ;; included in V. If V does not include any Latin squares 60 | ;; an empty map should be returned. In the previous 61 | ;; example the correct output of such a function is {3 1, 62 | ;; 2 1} and not {3 1, 2 3}. 63 | ;; 64 | ;; 1 Of course, we can consider sequences instead of 65 | ;; vectors. 66 | ;; 67 | ;; 2 Length of a vector is the number of elements in the 68 | ;; vector. 69 | 70 | (def __ :tests-will-fail) 71 | 72 | (comment 73 | 74 | ) 75 | 76 | (tests 77 | (__ '[[A B C D] 78 | [A C D B] 79 | [B A D C] 80 | [D C A B]]) := 81 | {} 82 | (__ '[[A B C D E F] 83 | [B C D E F A] 84 | [C D E F A B] 85 | [D E F A B C] 86 | [E F A B C D] 87 | [F A B C D E]]) := 88 | {6 1} 89 | (__ '[[A B C D] 90 | [B A D C] 91 | [D C B A] 92 | [C D A B]]) := 93 | {4 1, 2 4} 94 | (__ '[[B D A C B] 95 | [D A B C A] 96 | [A B C A B] 97 | [B C A B C] 98 | [A D B C A]]) := 99 | {3 3} 100 | (__ [ [2 4 6 3] 101 | [3 4 6 2] 102 | [6 2 4] ]) := 103 | {} 104 | (__ [[1] 105 | [1 2 1 2] 106 | [2 1 2 1] 107 | [1 2 1 2] 108 | [] ]) := 109 | {2 2} 110 | (__ [[3 1 2] 111 | [1 2 3 1 3 4] 112 | [2 3 1 3] ]) := 113 | {3 1, 2 2} 114 | (__ [[8 6 7 3 2 5 1 4] 115 | [6 8 3 7] 116 | [7 3 8 6] 117 | [3 7 6 8 1 4 5 2] 118 | [1 8 5 2 4] 119 | [8 1 2 4 5]]) := 120 | {4 1, 3 1, 2 7}) 121 | 122 | ;; Share your solution, and/or check how others did it: 123 | ;; https://gist.github.com/b9199ba2f5dc17a4c94e0d450f13a03b -------------------------------------------------------------------------------- /src/rich4clojure/hard/problem_164.clj: -------------------------------------------------------------------------------- 1 | (ns rich4clojure.hard.problem-164 2 | (:require [hyperfiddle.rcf :refer [tests]])) 3 | 4 | ;; = Language of a DFA = 5 | ;; By 4Clojure user: daowen 6 | ;; Difficulty: Hard 7 | ;; Tags: [automata seqs] 8 | ;; 9 | ;; A deterministic finite automaton (DFA) is an abstract 10 | ;; machine that recognizes a regular language. Usually a 11 | ;; DFA is defined by a 5-tuple, but instead we'll use a 12 | ;; map with 5 keys: 13 | ;; 14 | ;; * :states is the set of states for the DFA. 15 | ;; * :alphabet is the set of symbols included in the 16 | ;; language recognized by the DFA. 17 | ;; * :start is the start state of the DFA. 18 | ;; * :accepts is the set of accept states in the DFA. 19 | ;; * :transitions is the transition function for the DFA, 20 | ;; mapping :states ⨯ :alphabet onto :states. 21 | ;; 22 | ;; Write a function that takes as input a DFA definition 23 | ;; (as described above) and returns a sequence enumerating 24 | ;; all strings in the language recognized by the DFA. 25 | ;; 26 | ;; Note: Although the DFA itself is finite and only 27 | ;; recognizes finite-length strings it can still recognize 28 | ;; an infinite set of finite-length strings. And because 29 | ;; stack space is finite, make sure you don't get stuck in 30 | ;; an infinite loop that's not producing results every so 31 | ;; often! 32 | 33 | (def __ :tests-will-fail) 34 | 35 | (comment 36 | 37 | ) 38 | 39 | (tests 40 | #{"a" "ab" "abc"} := 41 | (set (__ '{:states #{q0 q1 q2 q3} 42 | :alphabet #{a b c} 43 | :start q0 44 | :accepts #{q1 q2 q3} 45 | :transitions {q0 {a q1} 46 | q1 {b q2} 47 | q2 {c q3}}})) 48 | #{"hi" "hey" "hello"} := 49 | (set (__ '{:states #{q0 q1 q2 q3 q4 q5 q6 q7} 50 | :alphabet #{e h i l o y} 51 | :start q0 52 | :accepts #{q2 q4 q7} 53 | :transitions {q0 {h q1} 54 | q1 {i q2, e q3} 55 | q3 {l q5, y q4} 56 | q5 {l q6} 57 | q6 {o q7}}})) 58 | (set (let [ss "vwxyz"] (for [i ss, j ss, k ss, l ss] (str i j k l)))) := 59 | (set (__ '{:states #{q0 q1 q2 q3 q4} 60 | :alphabet #{v w x y z} 61 | :start q0 62 | :accepts #{q4} 63 | :transitions {q0 {v q1, w q1, x q1, y q1, z q1} 64 | q1 {v q2, w q2, x q2, y q2, z q2} 65 | q2 {v q3, w q3, x q3, y q3, z q3} 66 | q3 {v q4, w q4, x q4, y q4, z q4}}})) 67 | [res (take 2000 (__ '{:states #{q0 q1} 68 | :alphabet #{0 1} 69 | :start q0 70 | :accepts #{q0} 71 | :transitions {q0 {0 q0, 1 q1} 72 | q1 {0 q1, 1 q0}}}))] := 73 | (and (every? (partial re-matches #"0*(?:10*10*)*") res) 74 | (= res (distinct res))) 75 | [res (take 2000 (__ '{:states #{q0 q1} 76 | :alphabet #{n m} 77 | :start q0 78 | :accepts #{q1} 79 | :transitions {q0 {n q0, m q1}}}))] := 80 | (and (every? (partial re-matches #"n*m") res) 81 | (= res (distinct res))) 82 | [res (take 2000 (__ '{:states #{q0 q1 q2 q3 q4 q5 q6 q7 q8 q9} 83 | :alphabet #{i l o m p t} 84 | :start q0 85 | :accepts #{q5 q8} 86 | :transitions {q0 {l q1} 87 | q1 {i q2, o q6} 88 | q2 {m q3} 89 | q3 {i q4} 90 | q4 {t q5} 91 | q6 {o q7} 92 | q7 {p q8} 93 | q8 {l q9} 94 | q9 {o q6}}}))] := 95 | (and (every? (partial re-matches #"limit|(?:loop)+") res) 96 | (= res (distinct res)))) 97 | 98 | ;; Share your solution, and/or check how others did it: 99 | ;; https://gist.github.com/0596dedb16b7064ee6e8e1c6ff085558 -------------------------------------------------------------------------------- /doc/INSTRUCTIONS.md: -------------------------------------------------------------------------------- 1 | # How to use Rich 4Clojure 2 | 3 | You will be solving problems one at a time, each in a separate `problem_###.clj` file. 4 | 5 | First you need to start the project and connect the editor to its REPL. This is how you do it using Calva: 6 | 7 | 1. Click the **REPL** button in the status bar 8 | ![VS Code Statusbar with Clojure REPL button](images/status-bar-nrepl-button.png) 9 |
Don't see the REPL button? 10 | 11 | This could be because some firewall or adblocker prevents the downloading of the Calva extension. See [issue #9](https://github.com/PEZ/rich4clojure/issues/9) for more clues on this. 12 | 13 |
14 | 1. Select **Start your project with a REPL server and connect (aka Jack-in)** from the quick pick menu 15 | 1. Select **Rich 4Clojure** in the **Project types** menu. 16 | 17 | The **REPL** button will turn Calva-yellow when the REPL is connected. Now Calva is ready to be used for solving problems! See below. 18 | 19 | Or, maybe _you_ don't feel ready just yet? 20 | 21 |
22 | 23 | I am new to Calva 24 | 25 | 26 | To learn the basics of using the Clojure REPL with Calva, use the **REPL** button and select **Fire up the ”Getting Started” REPL**. 27 | 28 | ![Calva's Clojure Getting Started REPL](images/clojure-getting-started-repl.png) 29 | 30 | (If you are already connected, you'll first need to select **Disconnect from the REPL server**. Then click the button again.) 31 | 32 | See [calva.io/getting-started](https://calva.io/getting-started/) for what to expect. 33 | 34 |
35 | 36 | 37 |
38 | 39 | I am new to Clojure 40 | 41 | 42 | Welcome to Clojure! ❤️ 43 | 44 | There is a lot to learn and many concepts to grasp. But fear not! 45 | 46 | * With the Gitpod option provided with this repository, you don't need to install anything and can wait with having to understand all the things about how Clojure is wired up and such. 47 | * The [**Getting Started REPL**](https://calva.io/getting-started/) (see *I am new to Calva*) also sports a basic Clojure guide. 48 | * The problems in this repository starts of from really easy and are designed to build up your Clojure skills. 49 | 50 |
51 |
52 | 53 | ## Find problems 54 | 55 | The problems reside, each in separate `.clj` files, in subfolders of the general difficulty level: 56 | 57 | 1. `src/rich4clojure/elementary` 58 | 1. `src/rich4clojure/easy` 59 | 1. `src/rich4clojure/medium` 60 | 1. `src/rich4clojure/hard` 61 | 62 | Find suitable/fun problems and hack away on it. Right now the original site's guidance on which order to take on the problems are not translated to this repo, but generally just in numerical order at the difficulty level of your choice should work. 63 | 64 | ## Solve problems 65 | 66 | Each problem has at least: 67 | 68 | 1. A title 69 | 1. A description 70 | 1. A definition of the symbol `__` 71 | ```clojure 72 | (def __ :tests-will-fail) 73 | ``` 74 | * The tests reference this symbol. You are sometimes supposed to redefine it to something that makes the tests pass, and sometimes you will need to edit the `(tests ...)` form and replace this symbol with something else. 75 | 1. An ”open” `(comment ...)` form (a Rich Comment Form, RFC) 76 | 1. A `(tests ...)` form 77 | 78 | Some problems also have restrictions, e.g: 79 | 80 | ```clojure 81 | (def restricted [map mapcat]) 82 | ``` 83 | 84 | Restrictions list ”things” shipped with **Clojure** that your solution is not allowed to use. Your editor's *lookup* features can give you documentation and defintions for the restricted symbols. Sometimes using the restricted ”things” in your solution, or just peeking at their documentation can help you find solutions that don't use them. 85 | 86 | ### Running the tests 87 | 88 | The process of taking on a problem is: 89 | 90 | 0. Open the file 91 | 1. Load the file (run the command **Calva: Load Current File and Dependencies**) 92 | * This will run the tests and they will fail. A failing test looks something like so: 93 | ``` 94 | clj:rich4clojure.easy.problem-39=> 95 | ; Evaluating file: problem_39.clj 96 | ❌ rich4clojure.easy.problem-39:21 97 | in (__ [30 20] [25 15]) 98 | 99 | [25 15] 100 | :≠ 101 | [30 25 20 15] 102 | 103 | ... 104 | ``` 105 | 1. Make the tests pass. 106 | * A successful test run looks something like so: 107 | ``` 108 | clj:rich4clojure.easy.problem-39=> 109 | ; Evaluating file: problem_39.clj 110 | ✅✅✅✅ 111 | ``` 112 | 113 | To rerun the tests, either reload the file or evaluate the `(tests ...)` form. This depends a bit on the problem and on your workflow. Here's a suggestion for problems which wants you to define a function: 114 | 115 | 1. Start with moving the `(def __ :tests-will-fail)` form inside the `(comment ...)` form 116 | 1. Redefine `__` to a function that will make the tests pass 117 | ```clojure 118 | (defn __ [x y z] ...) 119 | ``` 120 | **NB**: For some problems you rather need to edit the tests and *replace* each instance of `__` with your answer. 121 | 122 | Basically, treat both the `(comment ...)` and the `(tests ...)` forms as your RCFs. Think, fiddle, evaluate, think, fiddle, evaluate, ... until you have found the solution. 123 | 124 | ### Solved it? 125 | 126 | _Each problem has a **gist** linked at the bottom._ Please consider sharing your solution as a comment on the gist. Other Clojurians will benefit from seeing how you solved it. 127 | 128 | Also: 🍰 **Congratulations!** 🎉 Now take on the next problem! 129 | 130 | ### Stuck? 131 | 132 | The `#4clojure`, `#beginners` and `#clojure` channels at the [Clojurian's Slack](http://clojurians.net/) might be places to ask for help. You can also try the [Discussions](https://github.com/PEZ/rich4clojure/discussions) tab in this repo. Also: _Each problem has a **gist** linked at the bottom._ The gist's comment section might contain solutions from other users. Sometimes a peek is enough to give some guidance. But, yeah, major **spoiler alert**. 133 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rich 4Clojure 2 | 3 | *Practice Clojure using Interactive Programming in your editor.* 4 | 5 | ![Rich 4Clojure](doc/images/rich4clojure-header.png) 6 | 7 | Welcome to the [Rich Comment Form](https://calva.io/rich-comments/) (RCF) port of 4Clojure! ❤️ 8 | 9 | ## What it is 10 | 11 | 1. A repository that lets you learn and practice [Clojure](https://clojure.org), directly from the comfort of a Clojure REPL enhanced editor. (Even from your web browser, With zero installs, if you like.) 12 | 2. A ”copy” of the now closed **4clojure.com** site. It served us so wonderfully well! 13 | 14 | _Anyway, if you have come here to practice Clojure, you are at the right place!_ 15 | 16 | ## How it works 17 | 18 | You solve programming problems using Clojure and your editor of choice. The problems are designed in increasing difficulty levels. You can use it to take your first baby steps in the language and also to hone your Clojure skills. 19 | 20 | ## Get started 21 | 22 | To get to where you can start solving problems there are basically two alternatives: 23 | 24 | 1. Use your machine. 25 | 1. With **[Calva](https://calva.io)** 26 | 1. With some other editor with Clojure abilities 27 | 1. Using Calva in your browser, powered by ([Gitpod](https://gitpod.io)). 28 | 29 | ### On your machine 30 | 31 | This option can be easy, if you have Java and the Clojure tools installed and you know how to work with Clojure projects in your editor. It can also be a bit cumbersome if you don't. (In this case, I suggest you use Gitpod first and have some fun with the 4Clojure problems before you invest the time to figure out how to install the things.) 32 | 33 | Once you have Java and the Clojure tools installed: 34 | 35 | 0. Clone this repo to your machine. 36 | 37 | If you are using VS Code, install Calva and follow the same instructions as for the Gitpod option above, from step 2 and on. 38 | 39 | If you are using some other editor: 40 | 41 | 1. Open the project in your Clojure-enabled editor. 42 | 1. Start the project REPL and connect it to your editor session (Calva Jack-in). 43 | 1. Open [doc/INSTRUCTIONS.md](doc/INSTRUCTIONS.md) to find out how to use **Rich 4Clojure** to practice Clojure. 44 | 45 | ### Calva in Gitpod - Zero installs! 46 | 47 | This repository is prepared for [Gitpod](https://gitpod.io) such that both the [Clojure tools](https://clojure.org/guides/deps_and_cli) and [Calva](https://calva.io) are installed. 48 | 49 | That means you can start training Clojure like so by opening https://gitpod.io/#https://github.com/PEZ/rich4clojure in your web browser. 50 | 51 | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/pez/rich4clojure) 52 | 53 | It sometimes takes quite a while (even 5+ minutes) for the environment to be prepared. Grab some coffee. 😄 54 | 55 | Then you will see VS Code in your browser! And the [instructions for how to use Rich 4Clojure](doc/INSTRUCTIONS.md) will be opened for you. The instructions also contains information for what to do if you are new to Clojure and/or Calva. 56 | 57 | #### OK, so what is Gitpod? 58 | 59 | Gitpod is a development environment that runs in your web browser. It uses [VS Code](https://code.visualstudio.com). You can open any **GitHub**/**GitLab**/**Bitbucket** repository in the Gitpod development environment by prepending the repository URL with `https://gitpod.io/#`. That's how this repository's Gitpod URL is constructed: 60 | 61 | [https://gitpod.io/#https://github.com/pez/rich4clojure](https://gitpod.io/#https://github.com/pez/rich4clojure) 62 | 63 | See https://gitpod.io for the full story. 64 | 65 | ## What about the name? 66 | 67 | The name, **Rich 4Clojure**, comes from the fact that it relies on [Rich Comments](https://calva.io/rich-comments/) and also uses a test utility called [RCF](https://github.com/hyperfiddle/rcf) which is building on the same philosophy. Also, reading it like ”rich forclosure”, haha (or something)! 68 | 69 | ## Work in progress 70 | 71 | This is an early version from a quite automated translation of the original problems. There'll be dragons. Please consider filing issues, starting [discussions](https://github.com/PEZ/rich4clojure/discussions), or chat me up on the [Clojurian's Slack](http://clojurians.net/) (I'm **@pez**, there). 72 | 73 | PR:s are also welcome. If it is the content of a particular problem you want to suggest changes for, edit the `etc/problems.edn` file. 74 | 75 | ## Quirks 76 | 77 | I'll collect known/discovered quirks here if they are show stoppers. 78 | 79 | ### Zero install failing 80 | 81 | The Gitpod option can fail installing (I know, the irony) the Calva extension if you have adblockers/etcetera active on the Gitpod site. See [Issue #9](https://github.com/PEZ/rich4clojure/issues/9) for clues and workarounds. 82 | 83 | 84 | 85 | ## Support the project 86 | 87 | Consider starring ⭐ this repository, pretty please. 🙏 88 | 89 | Please also consider contributing. Issues and pull requests are welcome. Right now the ”other editor” instructions can improve greatly, especially with **Editor X** configuration added to this repository. Please consider sending PRs with instructions and settings for your favorite editor. 90 | 91 | The [Discussions](https://github.com/PEZ/rich4clojure/discussions) tab is also there for us to discuss both the project and solutions. 92 | 93 | If you want to contrubute with problems, that is also super welcome. I suggest you start that with adding it as a suggestion in [Discussions](https://github.com/PEZ/rich4clojure/discussions). Then, if we agree the problem should be added, add it as a PR to the `etc/problems.edn` file, and I will generate the files and gists from it. (Until I've set up a CI/CD pipeline.) 94 | 95 | If you want to encourage my work with this project you are welcome to sponsor me: 96 | 97 | * [Sponsor Peter Strömberg a.k.a. PEZ](https://github.com/sponsors/PEZ) ❤️ 98 | 99 | ## See also 100 | 101 | * [4ever-clojure](https://4clojure.oxal.org) – A web based ”always up” copy of the original 4Clojure. It has listings of all working solutions that that were submitted to the original site. 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | --------------------------------------------------------------------------------