Skip to content

Instantly share code, notes, and snippets.

View EmmanuelOga's full-sized avatar
🕹️

Emmanuel Oga EmmanuelOga

🕹️
View GitHub Profile
@EmmanuelOga
EmmanuelOga / ispalindrome.hs
Created December 10, 2014 08:20
Yet another palindrome checker.
isPalindrome :: Eq a => [a] -> Bool
isPalindrome list = result where
(_, result) = checkPal list (length list)
checkPal (_) 0 = ([], True)
checkPal (x:xs) 1 = (xs, True)
checkPal (x0:x1:xs) 2 = (xs, x0 == x1)
checkPal (x:xs) l = (rs, b && x == r) where (r:rs, b) = checkPal xs (l - 2)
main = do
print ( isPalindrome [1] )
@EmmanuelOga
EmmanuelOga / bubble_sort_two_stacks.rb
Created December 10, 2014 10:15
Sorts a stack using a second stack as intermediate storage.
def popMin(stack, aux)
# aux stack is reused to hold the sorted numbers and to inspect all numbers
aux_empty_len = aux.length
# Find the max. Need to inspect every entry.
max = stack.last
while !stack.empty?
v = stack.pop
aux.push v
@EmmanuelOga
EmmanuelOga / gcdmonad.hs
Created December 19, 2014 00:51
GCD with Writer Monad
import Control.Monad.Writer
agcd a 0 = do
tell ["Result: " ++ show a]
return a
agcd a b = do
tell ["Recusive call: " ++ (show a) ++ " % " ++ (show b)]
agcd b (a `mod` b)
@EmmanuelOga
EmmanuelOga / nums.hsout
Created December 19, 2014 10:10
num for functions
λ> instance Num b => Num (a -> b) where f + g = \x -> f x + g x
<interactive>:2:10: Warning:
No explicit implementation for
‘*’, ‘abs’, ‘signum’, ‘fromInteger’, and (either ‘negate’ or ‘-’)
In the instance declaration for ‘Num (a -> b)’
(0.04 secs, 9972760 bytes)
λ> :t (*2) + (*3)
(*2) + (*3) :: Num a => a -> a
λ> (*2) + (*3) $ 4 -- 4*2 + 4*3...
20
@EmmanuelOga
EmmanuelOga / stack.hs
Created December 19, 2014 11:01
A Haskell Stack with the State monad
import Control.Monad.State
type Stack = [Int]
pop :: State Stack Int
pop = state $ \(x:xs) -> (x,xs)
push :: Int -> State Stack ()
push a = state $ \xs -> ((),a:xs)
@EmmanuelOga
EmmanuelOga / DoubleRation.java
Created December 25, 2014 12:57
DoubleRation experiments. If the ratio gets closer and close to a fixed limit, the runtime of the algo. can be approximated using that ratio.
// http://algs4.cs.princeton.edu/14analysis/
// http://algs4.cs.princeton.edu/14analysis/DoublingRatio.java.html
public class DoublingRatio
{
public static double timeTrial(int N)
public static void main(String[] args)
{
double prev = timeTrial(125);
for (int N = 250; true; N += N)
@EmmanuelOga
EmmanuelOga / ObjectSizeFetcher.java
Created December 25, 2014 13:19
Determining java Obj size with java.lang.instrument package. Compile and put this class in a JAR:
// http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
@EmmanuelOga
EmmanuelOga / UnionFind.xtend
Last active August 29, 2015 14:12
Non-Weighted UnionFind (disjoint set) implementation in Xtend (http://algs4.cs.princeton.edu/15uf/)
class UnionFind {
val int[] ids
var count = 0
new(int size) {
count = size
ids = newIntArrayOfSize(size)
(0 ..< size).forEach[i|ids.set(i, i)]
}
@EmmanuelOga
EmmanuelOga / select.java
Created December 27, 2014 07:56
A partial sort can be used to select the kth smallest element from an array (modifying the arr in place)
// This is an alternative to using a priority queue for the same purpose.
// http://algs4.cs.princeton.edu/24pq/
// http://en.wikipedia.org/wiki/Selection_algorithm
public static Comparable select(Comparable[] a, int k) {
shuffle(a); // since this algo uses quicksort's partition, it is better to shuffle the arr to avoid qs worst case
int lo = 0, hi = a.length - 1;
while (hi > lo) {
int j = partition(a, lo, hi); // invoke quicksort's partition, we get all smaller items to the left.
if (j == k) return a[k]; // if the pivot lies on the kth position, we are done (also, all numbers to left are smaller)
@EmmanuelOga
EmmanuelOga / addfivemulthree.js
Last active August 29, 2015 14:12
Natural numbers that can be generated *IN EXACTLY THREE WAYS* by starting from the number 1 and repeatedly either adding 5 or multiplying by 3.
function findSolution(target) {
function find(number, history) {
if (number > target) {
return [];
} else if (number < target) {
return find(number * 3, "(" + history + " * 3)").concat(
find(number + 5, "(" + history + " + 5)"));
} else { // if (number == target) {
return [history];
}