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
#!/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: |
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
# 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 |
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
// 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 } }" |
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
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) |
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
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"; | |
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
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); |
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
-- 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]] |
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
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 |
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
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 |
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 initialize(words) | |
@words = words | |
end | |
def isWord?(word) | |
@words.include?(word) | |
end | |
# TODO: implement trie :) | |
def isPrefix?(word) |
OlderNewer