Git Product home page Git Product logo

4clojure-solutions's Introduction

Markdown 4clojure solutions generated with 4clojure-to-md

#1 Nothing but the Truth [Elementary]

true

#2 Simple Math [Elementary]

4

#3 Intro to Strings [Elementary]

"HELLO WORLD"

#4 Intro to Lists [Elementary]

:a :b :c

#5 Lists: conj [Elementary]

'(1 2 3 4)

#6 Intro to Vectors [Elementary]

:a :b :c

#7 Vectors: conj [Elementary]

[1 2 3 4]

#8 Intro to Sets [Elementary]

#{:a :b :c :d}

#9 Sets: conj [Elementary]

2

#10 Intro to Maps [Elementary]

20

#11 Maps: conj [Elementary]

{:b 2}

#12 Intro to Sequences [Elementary]

3

#13 Sequences: rest [Elementary]

[20 30 40]

#14 Intro to Functions [Elementary]

8

#15 Double Down [Elementary]

(partial * 2)

#16 Hello World [Elementary]

#(str "Hello, " % "!")

#17 Sequences: map [Elementary]

'(6 7 8)

#18 Sequences: filter [Elementary]

'(6 7)

#19 Last Element [Easy]

(fn my-last [coll]
  (if (empty? (rest coll))
    (first coll)
    (my-last (rest coll))))

#20 Penultimate Element [Easy]

(fn my-last [coll]
  (if (empty? (rest (rest coll)))
    (first coll)
    (my-last (rest coll))))

#21 Nth Element [Easy]

(fn my-nth [coll n]
  (if (= n 0) 
    (first coll)
    (my-nth (rest coll) (dec n))))

#22 Count a Sequence [Easy]

(fn my-count [coll]
  (if (empty? coll) 0
    (inc (my-count (rest coll)))))

#23 Reverse a Sequence [Easy]

(fn my-reverse [coll]
  (if (empty? coll) nil
    (concat (my-reverse (rest coll)) (list (first coll)))))

#24 Sum It All Up [Easy]

(fn sum [coll]
  (if (empty? coll) 0
    (+ (first coll) (sum (rest coll)))))

#25 Find the odd numbers [Easy]

(fn filter-odd [coll]
  (if (empty? coll) '()
    (let [elem (first coll)]
      (if (= (mod elem 2) 1)
        (conj (filter-odd (rest coll)) elem)
        (filter-odd (rest coll))))))

#26 Fibonacci Sequence [Easy]

#(take % ((fn fibgen [n1 n2] (lazy-seq (cons n1 (fibgen n2 (+ n1 n2))))) 1 1))

#27 Palindrome Detector [Easy]

#(= (seq %) (reverse (seq %)))

#28 Flatten a Sequence [Easy]

(fn flat [coll]
  (when-let [s (seq coll)]
    (if (coll? (first s))
      (concat (flat (first s)) (flat (rest s)))
      (cons (first s) (flat (rest s))))))

#29 Get the Caps [Easy]

(fn [s] (apply str (filter #(not= (str %) (clojure.string/lower-case %)) s)))

#30 Compress a Sequence [Easy]

#(map first (partition-by identity %))

#31 Pack a Sequence [Easy]

#(partition-by identity %)

#32 Duplicate a Sequence [Easy]

mapcat #(repeat 2 %)

#33 Replicate a Sequence [Easy]

(fn [coll times] (mapcat #(repeat times %) coll))

#34 Implement range [Easy]

#(take (- %2 %1) (iterate inc %1))

#35 Local bindings [Elementary]

7

#36 Let it Be [Elementary]

[x 7 y 3 z 1]

#37 Regular Expressions [Elementary]

"ABC"

#38 Maximum value [Easy]

#((fn max-iterative [coll current-max] 
    (if (empty? coll) current-max
      (let [possible-new-max (first coll)]
        (if (> possible-new-max current-max) (max-iterative (rest coll) possible-new-max) 
          (max-iterative (rest coll) current-max))))) %& 0)

#39 Interleave Two Seqs [Easy]

#(mapcat vector %1 %2)

#40 Interpose a Seq [Easy]

(fn [separator coll] (drop-last (mapcat #(vector % separator) coll)))

#41 Drop Every Nth Item [Easy]

(fn [coll size] (mapcat #(take (dec size) %) (partition-all size coll)))

#42 Factorial Fun [Easy]

#(reduce * (range 1 (inc %)))

#43 Reverse Interleave [Medium]

#(apply map list (partition %2 %1))

#44 Rotate Sequence [Medium]

(fn go [amount coll]
  (if (< amount 0) (go (+ (count coll) amount) coll)
      (if (= amount 0) coll
          (go (dec amount) (reverse (into '() (conj (vec (drop 1 coll)) (first coll))))))))

#45 Intro to Iterate [Easy]

'(1 4 7 10 13)

#46 Flipping out [Medium]

(fn [op] (fn [a b] (op b a)))

#47 Contain Yourself [Easy]

4

#48 Intro to some [Easy]

6

#49 Split a sequence [Easy]

(fn [n coll] (list (vec (take n coll)) (vec (nthrest coll n))))

#50 Split by Type [Medium]

(fn [coll]
    (vals (group-by type coll)))

#51 Advanced Destructuring [Easy]

[1 2 3 4 5]

#52 Intro to Destructuring [Elementary]

[c e]

#53 Longest Increasing Sub-Seq [Hard]

(fn [coll]
    (letfn [(is-inc? [a e]
              (or (empty? a)
                  (= (inc (last a)) e)))
            (inner-loop [c acc max-acc]
              (if (empty? c) (if (= (count max-acc) 1) [] ; because of [(first c)]
                                 (if (= (last (drop-last 1 coll)) (last max-acc)) ; we miss the last one in the latest if (this is a hack)...
                                   (conj max-acc (last coll))
                                   max-acc))
                  (inner-loop (rest c)
                              (if (is-inc? acc (first c))
                                (conj acc (first c))
                                [(first c)])
                              (if (>= (count acc) (count max-acc))
                                acc
                                max-acc))))]
      (inner-loop coll [] [])))

#54 Partition a Sequence [Medium]

(fn [x coll]
  (loop [c coll
         acc []]
    (if (or (empty? c)
            (< (count c) x))
      acc
      (recur (drop x c) 
             (conj acc (take x c))))))

#55 Count Occurrences [Medium]

(fn [coll]
    (into {} (map (fn [[k v]] [k (count v)]) (group-by identity coll))))

#56 Find Distinct Items [Medium]

(fn [coll]
  (let [step (fn step [xs seen]
               (lazy-seq
                ((fn [[f :as xs] seen]
                   (when-let [s (seq xs)]
                     (if (contains? seen f)
                       (recur (rest s) seen)
                       (cons f (step (rest s) (conj seen f))))))
                 xs seen)))]
    (step coll #{})))

#57 Simple Recursion [Elementary]

[5 4 3 2 1]

#58 Function Composition [Medium]

(fn [& fs]
    (fn [& args]
      (let [sf (reverse fs)]
        (if (nth sf 2 nil)
          ((nth sf 2) ((second sf) (apply (first sf) args)))
          ((second sf) (apply (first sf) args))))))

#59 Juxtaposition [Medium]

(fn [& fs]
    (fn [& args]
      (mapv (fn [f] (apply f args)) fs)))

#60 Sequence Reductions [Medium]

(fn step
    ([f coll]
     (step f (f (first coll)) (next coll)))
    ([f e coll]
     (if (not-empty coll)
       (lazy-seq (cons e
                       (step f
                             (f e (first coll))
                             (next coll))))
       (list e))))

#61 Map Construction [Easy]

(fn [ks vs]
    (apply hash-map (interleave ks vs)))

#62 Re-implement Iterate [Easy]

(fn step [f val]
    (lazy-seq (cons val (step f (f val)))))

#63 Group a Sequence [Easy]

(fn step
    ([f coll] (step f coll {}))
    ([f coll m]
     (if coll
       (let [k (f (first coll))]
         (step
          f
          (next coll)
          (assoc m k (conj (get m k []) (first coll)))))
       m)))

#64 Intro to Reduce [Elementary]

+

#65 Black Box Testing [Medium]

(fn [data]
    (cond (or (= data {}) (:a data))
          :map

          (= (inc (count data)) (count (conj data :data :data :data)))
          :set

          (= (first (conj data 20 21)) 21)
          :list

          (= (last (conj data 20 21)) 21)
          :vector))

#66 Greatest Common Divisor [Easy]

(fn gcd [a b]
    (if (zero? b)
        a
        (gcd b (mod a b))))

#67 Prime Numbers [Medium]

(fn [n]
    (take n
          ((fn primes [candidate]
             (lazy-seq
              (if (some #(zero? (mod candidate %)) (range 2 candidate))
                (primes (inc candidate))
                (cons candidate (primes (inc candidate))))))
           2)))

#68 Recurring Theme [Elementary]

[7 6 5 4 3]

#69 Merge with a Function [Medium]

(fn [f & ms]
    (let [keyvals (mapcat seq ms)]
      (loop [m     {}
             coll  (next keyvals)
             [k v] (first keyvals)]
        (if v
          (recur (assoc m k (if-let [v2 (get m k)]
                              (f v2 v)
                              v))
                 (next coll)
                 (first coll))
          m))))

#70 Word Sorting [Medium]

(fn [sentence]
    (sort-by #(clojure.string/lower-case %)
             (-> (clojure.string/replace sentence #"[!.]" "")
                 (clojure.string/split #" "))))

#71 Rearranging Code: -> [Elementary]

last

#72 Rearranging Code: ->> [Elementary]

apply +

#73 Analyze a Tic-Tac-Toe Board [Hard]

(letfn [(transpose [m] (apply mapv vector m))]
    (fn [board]
      (cond (some #(= % [:x :x :x]) (concat board (transpose board)))
            :x

            (some #(= % [:o :o :o]) (concat board (transpose board)))
            :o

            (or (and (= (ffirst board) :x)
                     (= (second (second board)) :x)
                     (= (-> board (nth 2) (nth 2)) :x))
                (and (= (first (last board)) :x)
                     (= (second (second board)) :x)
                     (= (last (first board)) :x)))
            :x

            (or (and (= (ffirst board) :o)
                     (= (second (second board)) :o)
                     (= (-> board (nth 2) (nth 2)) :o))
                (and (= (first (last board)) :o)
                     (= (second (second board)) :o)
                     (= (last (first board)) :o)))
            :o)))

#74 Filter Perfect Squares [Medium]

(fn [s]
    (letfn [(perfect-square [n]
              (let [root (Math/sqrt n)]
                (= (Math/floor root) root)))]
      (->> (clojure.string/split s #",")
           (map #(Integer/valueOf %))
           (filter perfect-square)
           (interpose ",")
           (apply str))))

#75 Euler's Totient Function [Medium]

(letfn [(gcd [a b]
            (if (zero? b)
              a
              (gcd b (mod a b))))]
    (fn [n]
      (if (= n 1) 1
          (->> (range 1 n)
               (filter #(= (gcd n %) 1))
               (count)))))

#76 Intro to Trampoline [Medium]

[1 3 5 7 9 11]

#77 Anagram Finder [Medium]

(fn [words]
    (->> (group-by sort words)
         (vals)
         (filter #(> (count %) 1))
         (map set)
         (set)))

#78 Reimplement Trampoline [Medium]

(fn [f & args]
    (loop [res (apply f args)]
      (if (fn? res)
        (recur (res))
        res)))

#79 Triangle Minimal Path [Hard]

(fn [triangle]
    (let [row (last triangle)]
      (if (= (count row) 1)
        (first row)
        (recur (concat (drop-last 2 triangle)
                       [(mapv (fn [a [b c]] (min (+ a b) (+ a c)))
                              (last (drop-last triangle))
                              (partition 2 1 (last triangle)))])))))

#80 Perfect Numbers [Medium]

(fn [x]
    (letfn [(divisors [n] (filter #(integer? (/ n %)) (range 1 n)))
            (perfect? [n] (= n (apply + (divisors n))))]
      (perfect? x)))

#81 Set Intersection [Easy]

(fn [s1 s2]
    (letfn [(dups [c] (for [[n freq] (frequencies c)
                            :when (> freq 1)]
                        n))]
      (set (dups (concat s1 s2)))))

#82 Word Chains [Hard]

TODO

#83 A Half-Truth [Easy]

TODO

#84 Transitive Closure [Hard]

TODO

#85 Power Set [Medium]

TODO

#86 Happy numbers [Medium]

TODO

[]

TODO

#88 Symmetric Difference [Easy]

TODO

#89 Graph Tour [Hard]

TODO

#90 Cartesian Product [Easy]

TODO

#91 Graph Connectivity [Hard]

TODO

#92 Read Roman numerals [Hard]

TODO

#93 Partially Flatten a Sequence [Medium]

TODO

#94 Game of Life [Hard]

TODO

#95 To Tree, or not to Tree [Easy]

TODO

#96 Beauty is Symmetry [Easy]

TODO

#97 Pascal's Triangle [Easy]

TODO

#98 Equivalence Classes [Medium]

TODO

#99 Product Digits [Easy]

TODO

#100 Least Common Multiple [Easy]

TODO

#101 Levenshtein Distance [Hard]

TODO

#102 intoCamelCase [Medium]

TODO

#103 Generating k-combinations [Medium]

TODO

#104 Write Roman Numerals [Medium]

TODO

#105 Identify keys and values [Medium]

TODO

#106 Number Maze [Hard]

TODO

#107 Simple closures [Easy]

TODO

#108 Lazy Searching [Medium]

TODO

[]

TODO

#110 Sequence of pronunciations [Medium]

TODO

#111 Crossword puzzle [Hard]

TODO

#112 Sequs Horribilis [Medium]

TODO

#113 Making Data Dance [Hard]

TODO

#114 Global take-while [Medium]

TODO

#115 The Balance of N [Medium]

TODO

#116 Prime Sandwich [Medium]

TODO

#117 For Science! [Hard]

TODO

#118 Re-implement Map [Easy]

TODO

#119 Win at Tic-Tac-Toe [Hard]

TODO

#120 Sum of square of digits [Easy]

TODO

#121 Universal Computation Engine [Medium]

TODO

#122 Read a binary number [Easy]

TODO

[]

TODO

#124 Analyze Reversi [Hard]

TODO

#125 Gus' Quinundrum [Hard]

TODO

#126 Through the Looking Class [Easy]

TODO

#127 Love Triangle [Hard]

TODO

#128 Recognize Playing Cards [Easy]

TODO

[]

TODO

#130 Tree reparenting [Hard]

TODO

#131 Sum Some Set Subsets [Medium]

TODO

#132 Insert between two items [Medium]

TODO

[]

TODO

#134 A nil key [Elementary]

#(and (contains? %2 %1) (nil? (%1 %2)))

#135 Infix Calculator [Easy]

TODO

[]

TODO

#137 Digits and bases [Medium]

TODO

#138 Squares Squared [Hard]

TODO

[]

TODO

#140 Veitch, Please! [Hard]

TODO

#141 Tricky card games [Medium]

TODO

[]

TODO

#143 dot product [Easy]

TODO

#144 Oscilrate [Medium]

(fn [val & fns]
    (let [fns (cycle fns)]
      (reductions (fn [acc f] (f acc)) val fns)))

#145 For the win [Elementary]

TODO

#146 Trees into tables [Easy]

TODO

#147 Pascal's Trapezoid [Easy]

TODO

#148 The Big Divide [Medium]

TODO

[]

TODO

#150 Palindromic Numbers [Medium]

TODO

[]

TODO

#152 Latin Square Slicing [Hard]

TODO

#153 Pairwise Disjoint Sets [Easy]

TODO

[]

TODO

[]

TODO

#156 Map Defaults [Elementary]

TODO

#157 Indexing Sequences [Easy]

TODO

#158 Decurry [Medium]

TODO

[]

TODO

[]

TODO

4clojure-solutions's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.