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
;; fixed version of 'state' at http://mishadoff.com/blog/clojure-design-patterns/#episode-3-state | |
;; takeaway: if you call 'swap!' twice on the same atom, you are probably making a mistake | |
(def user {:name "Jackie Brown" | |
:balance 0 | |
:subscription? false}) | |
(def ^:const SUBSCRIPTION_COST 30) | |
(defn pay |
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
:duct.server.http/jetty{:async? true} |
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 powerset.core-test | |
(:use (clojure set test))) | |
(defn powerset [s] | |
(apply union | |
#{s} ;the complete set of all s | |
(map (fn [i] (powerset (disj s i))) s))) | |
(deftest test-powerset | |
(is (= #{#{}} |
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
package com.algorithms; | |
/** | |
* Created by Hadi on 10/25/2016. | |
*/ | |
public class LongestCommonSubstring { | |
public static void main(String[] args){ | |
System.out.println(longestCommonSubstring("blue","cluees")); | |
} |
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
package com.algorithms; | |
import static java.lang.Math.max; | |
/** | |
* Created by Hadi on 10/24/2016. | |
*/ | |
public class KnapSack { | |
public static void main(String[] args){ |
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
package com.algorithms; | |
/** | |
* Created by Hadi on 10/21/2016. | |
*/ | |
public class CoinChange { | |
public static void main(String[] args){ | |
int arr[] = {1, 3, 5}; |
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
package com.algorithms; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Scanner; | |
import static java.lang.Math.*; | |
import static java.util.Arrays.*; | |
import static java.util.stream.IntStream.*; |
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 items [[1 2] [2 4] [3 6] [10 5]]) | |
(defn values [] (into [] ((fn [items] (for [i items] {:weight (first i) :value (last i)}))items))) | |
(defn knapsack [weight values] | |
(if | |
(empty? values) 0 ;; base case | |
(if (> (:weight (first values)) weight) | |
(knapsack weight (rest values)) ;; continue |
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
(defn longest [xs ys] (if (> (count xs) (count ys)) xs ys)) | |
(def lcs | |
(memoize | |
(fn [[x & xs] [y & ys]] | |
(cond | |
(or (= x nil) (= y nil) ) nil | |
(= x y) (cons x (lcs xs ys)) | |
:else (longest (lcs (cons x xs) ys) (lcs xs (cons y ys))))))) |
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
(defn node [g] | |
(set (flatten g))) | |
(defn adjList [g] | |
(into {} (vec ((fn [m] into {} (for [[k v]m] {k (node v)}))(group-by first g))))) | |
(defn connected? [g a b] | |
(loop [n (a g) | |
v #{}] | |
(let [diff (clojure.set/difference n v)] |
NewerOlder