This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; string s = "123456789" | |
;; int target = 100 | |
;; how many combinations of +/- give you the target int? | |
;; + and - can be inserted at any index | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;;;;;;helper functions;;;;;; | |
(defn str->vec | |
"converts a string of consecutive numbers to a vector of individual numbers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;;;;;;;Given this yucky implementation: | |
(defn print-row [num] | |
(loop [iter 0]) | |
(if (< iter num) | |
(do (print "*") | |
(recur (inc iter))) | |
(println ""))) | |
(defn print-a-triangle [num] | |
(loop [iter 0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(set! *unchecked-math* false) | |
(defn find-the-pairs [^long n] | |
"takes an upper bound and returns all the pairs of numbers where | |
(* x y) equals the sum of the entire collection without x and y | |
(find-the-pairs 26) => [[15 21] [21 15]] | |
" | |
(let [^long coll-sum (/ (* n (inc n)) 2)] | |
(for [^long i (range 1 (inc n)) ^long j (range 1 (inc n)) | |
:when (= (- coll-sum i j) (* i j))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;this works until you pass in ridiculously large numbers | |
;; the for loop lets me try all of the numbers in the collection with each other | |
(defn find-the-pairs [n] | |
"takes an upper bound and returns all the pairs of numbers where | |
(* x y) equals the sum of the entire collection without x and y | |
(find-the-pairs 26) => [[15 21] [21 15]] | |
" | |
(let [coll (take n (iterate inc 1)) | |
coll-sum (apply + coll)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def inc-coll (partial map inc)) | |
;;#'user/inc-coll | |
(inc-coll [4 5 6]) | |
;;(5 6 7) | |
(def inc-coll-coll (partial map inc-coll)) | |
;;#'user/inc-coll-coll | |
(inc-coll-coll [[1] [2]]) | |
;;((2) (3)) | |
(inc-coll-coll [[1] 3]) | |
;;IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:542) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Filtering | |
; product visible when no filter | |
(define [:app :visible-product/id ?e] :- [:not [_ :product-filter/range]] | |
[:not [_ :product-filter/category]] | |
[[?e :product/name]]) | |
; product visible when filter active and price in range | |
(define [:app :visible-product/id ?e] | |
:- [[_ :product-filter/range ?range]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns spec-test.core | |
(:require [clojure.spec :as s])) | |
(defn x-integer? [x] | |
(if (integer? x) | |
x | |
(if (string? x) | |
(try | |
(Integer/parseInt x) | |
(catch Exception e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*in* | |
*ns* | |
aget | |
amap | |
as-> | |
aset | |
atom | |
bean | |
byte | |
case |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;compare the two approaches to solving the same problem | |
;;A child plays with a ball on the nth floor of a big building the height of which is known | |
;;(float parameter "h" in meters, h > 0) . | |
;;He lets out the ball. The ball rebounds for example to two-thirds | |
;;(float parameter "bounce", 0 < bounce < 1) | |
;;of its height. | |
;;His mother looks out of a window that is 1.5 meters from the ground | |
;;(float parameters window < h). | |
;;How many times will the mother see the ball either falling or bouncing in front of the window |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- server passes in a collection of product maps --> | |
<!-- controller now sends a collection of collections of product maps, specifically (partition-all 4 products) --> | |
{% for product-partition in partitioned-products %} | |
{% for product in product-partition %} | |
<div class="col-sm-10 col-sm-offset-2"> | |
<div class="row"> | |
<div class="col-sm-3" style="height:250px;"> | |
<p>{{product.name}}</p> | |
<p>Price ${{product.price}}</p> |
NewerOlder