Skip to content

Instantly share code, notes, and snippets.

@arjans
arjans / gist:3259688
Created August 4, 2012 20:16 — forked from janewang/gist:3259652
Y Combinator vs. U Combinator vs. Typical Recursion
// Y combinator
function Y(f) {
return (
(function (x) {
return f(function (v) { return x(x)(v); }); })
(function (x) {
return f(function (v) { return x(x)(v); }); })
);
}
@arjans
arjans / gist:3259868
Created August 4, 2012 20:54
Recursive and Memoized Y-Combinator Fibonacci Functions
//Recursive fibonacci
var fib_recur = function (n) {
if (n == 0) return 0 ;
if (n == 1) return 1 ;
return fib_recur(n-1) + fib_recur(n-2) ;
} ;
//Recursive fibonacci with memoization (but without the Y-Combinator)
var fibonacci = (function ( ) {
var memo = [0, 1];
@arjans
arjans / stack.rb
Created November 18, 2016 19:59
Stack in Ruby
# Implementing a stack for algorithm study group.
# Can't use pop, <<, +, or any Ruby methods that would defeat the purpose of this exercise.
class Stack
def initialize
@bottom = 3 # zero-indexed
@top = 3 # zero-indexed
@items = [nil,nil,nil,nil]
end
@arjans
arjans / queue.rb
Created November 18, 2016 20:00
Queue in Ruby
# Implementing a queue for algorithm study group.
class Queue
def initialize
@front = 0
@back = 0
@items = [nil, nil, nil, nil]
@num_items = 0
end
@arjans
arjans / set_game.rb
Created November 18, 2016 22:18
Solver for the game of Set
#!/usr/bin/env ruby
#
# Solver for the game of Set.
#
class Card
# Constants deriving from the rules of the game
COLORS = ["red", "blue", "green"]
SHAPES = ["circle", "square", "triangle"]
@arjans
arjans / trie.rb
Created December 2, 2016 20:51
Trie Implementation in Ruby
# Meant for strings consisting of English characters.
class TrieNode
attr_accessor :terminal, :character
def initialize(character: "", terminal: false)
@character = character
@terminal = terminal
@children = []
end
@arjans
arjans / hash.rb
Last active December 28, 2017 06:59
Hash Table Implementation in Ruby
# Meant to work with strings as keys.
class MyHash
def initialize(size: 100)
@size = size
@hash = Array.new(size)
end
attr_accessor :hash