Skip to content

Instantly share code, notes, and snippets.

@hadielmougy
hadielmougy / atomic_state_update.clj
Created November 25, 2017 19:26 — forked from stuarthalloway/atomic_state_update.clj
Update state with a pure function
;; 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
:duct.server.http/jetty{:async? true}
@hadielmougy
hadielmougy / gist:5d468409e2e5d75e1f33fba92cca721b
Created March 23, 2017 14:40 — forked from dekellum/gist:4171049
powerset in clojure using sets,union
(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 (= #{#{}}
@hadielmougy
hadielmougy / LongestCommonSubstring.java
Created October 24, 2016 23:30
LongestCommonSubstring
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"));
}
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){
@hadielmougy
hadielmougy / CoinChange.java
Created October 23, 2016 23:22
Coin Change
package com.algorithms;
/**
* Created by Hadi on 10/21/2016.
*/
public class CoinChange {
public static void main(String[] args){
int arr[] = {1, 3, 5};
@hadielmougy
hadielmougy / Solution.java
Last active October 20, 2016 16:45
maximum subarray
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.*;
(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
(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)))))))
(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)]