Skip to content

Instantly share code, notes, and snippets.

View EmmanuelOga's full-sized avatar
🕹️

Emmanuel Oga EmmanuelOga

🕹️
View GitHub Profile
@EmmanuelOga
EmmanuelOga / commit-msg
Created February 7, 2014 17:40
Commit message hook: include name of the branch on the commit message.
#!/usr/bin/env ruby
#
# Git commit-msg hook. If your branch name is in the form "US1234-postfix", or
# "US1234_postfix", it automatically adds the prefix "[US1234]" to commit
# messages.
#
# If you include "#noref" in the commit message, nothing will be added to the
# commit message, and the "#noref" itself will be stripped.
#
# Install:
# Insertions, Removals and Random Access in O(1),
# assuming the complexity of growing an array is negligible.
# Otherwise a constant size array with a max-size could be used.
class LoadBalancer
def initialize
@map, @vector = {}, []
end
def insert(val)
@map[val] = @vector.length
@EmmanuelOga
EmmanuelOga / permutations.ceylon
Last active August 29, 2015 14:06
Generate a list of permutations given a list of numbers.
// Port to ceylon of number permutations code found here:
// https://github.com/johnwhitington/more-ocaml-exercises/blob/master/exercises/Chapter10/exercises.ml#L3-L25
alias IntList => {Integer*};
"intersperce the elem in the list
example:
intersperce({ 1, 2 }, 3)
produces:
{ { 3, 1, 2 }, { 1, 3, 2 }, { 1, 2, 3 } }"
@EmmanuelOga
EmmanuelOga / permutations.rb
Created September 12, 2014 09:24
Permutations, this time with ruby.
class Array
def interleave(n)
(0...length).map do |i|
(0...length).map do |j|
i == j ? [n, self[j]] : self[j]
end.flatten
end.concat([self + [n]])
end
def combine(n)
@EmmanuelOga
EmmanuelOga / equals-hashcode-tostring.java
Created September 29, 2014 02:55
Equals / HashCode / ToString template
import java.util.Objects;
/*
* When defining equals, hashCode should be defined too.
* It is the civil thing to define toString too.
*/
class Base {
public String a = "a";
@EmmanuelOga
EmmanuelOga / ArrayCopyOf.java
Created September 29, 2014 03:54
Custom Array copyOf
import java.lang.reflect.Array;
import java.util.Arrays;
public class Sandbox {
public static Object arrayCopyOf(Object arr, int newLen) {
Class cl = arr.getClass();
if (!cl.isArray()) { return null; }
Class componentType = cl.getComponentType();
int length = Array.getLength(arr);
Object newArray = Array.newInstance(componentType, newLen);
@EmmanuelOga
EmmanuelOga / permutations.hs
Created November 4, 2014 03:30
permutations.hs
-- Adapted from https://gist.github.com/EmmanuelOga/18873d5f34bc4a5e3d10
-- insert elem on a list at the i position
-- pinsert 7 [1,2] 1 -> [1,7,2]
pinsert :: a -> [a] -> Int -> [a]
pinsert e xs i = (take i xs) ++ [e] ++ (drop i xs)
-- intersperce an elem in all posible places of a list
-- ex.: pinters 1 [2, 3] -> [[1,2,3], [2, 1, 3], [2, 3, 1]]
pinters :: a -> [a] -> [[a]]
@EmmanuelOga
EmmanuelOga / balancing.hs
Created November 27, 2014 03:47
Balancing routine example from http://learnyouahaskell.com/ book.
import Control.Monad
import Control.Monad.State
type Birds = Int
type Pole = (Birds, Birds)
type Balance = Either String Pole
birdLand :: Pole -> Balance
birdLand pole@(left, right) = if balanced
@EmmanuelOga
EmmanuelOga / coin.hs
Created November 28, 2014 08:55
Coin tossing
import Data.List (all)
import Data.Ratio
newtype Prob a = Prob { getProb :: [(a, Rational)] }
instance Functor Prob where
fmap f (Prob xs) = Prob $ map (\(x, p) -> (f x, p)) xs
flatten :: Prob (Prob a) -> Prob a
flatten (Prob xs) = Prob $ concat $ map multAll xs
@EmmanuelOga
EmmanuelOga / boggle.rb
Created December 5, 2014 08:44
Quick and Dirty Boggle-Like game algo
def initialize(words)
@words = words
end
def isWord?(word)
@words.include?(word)
end
# TODO: implement trie :)
def isPrefix?(word)