Skip to content

Instantly share code, notes, and snippets.

View sirupsen's full-sized avatar
🐡

Simon Eskildsen sirupsen

🐡
View GitHub Profile
@sirupsen
sirupsen / gist:5787227
Last active December 18, 2015 12:59
Simple example of finding the lowest common ancestor. The range minimum query is done in O(n) time, but can be done in O(log(n)) time with a O(n*log(n)) precomputation, with a e.g. a segment tree.
#include <vector>
#include <climits>
using namespace std;
int iteration = 0;
vector<size_t> h, l, e;
vector<vector<size_t> > adj;
void lca_dfs(size_t v, size_t depth)
{
@sirupsen
sirupsen / gist:6225767
Last active December 21, 2015 01:09
Ruby 2.0.0 coerce incompatibility.
# The issue here, is that when Ruby encounters an expression like:
#
# 5 < Mass.new(10)
#
# The Fixnum doesn't know how to compare itself to a Mass. However, a Mass object knows
# how to compare itself with a Fixnum. Ruby calls #coerce on 5, to switch the parameters
# around, so now we have:
#
# Mass.new(10) > 5
#
@sirupsen
sirupsen / posix-mqueue-receive.c
Last active December 22, 2015 04:18
Extraction from the posix-mqueue gem of how it receives messages from the queue.
VALUE
posix_mqueue_receive(VALUE self)
{
// Contains any error returned by the syscall
int err;
// Buffer data from the message queue is read into
size_t buf_size;
char *buf;
@sirupsen
sirupsen / gist:6479740
Last active April 5, 2019 16:28
Implementation of a trie in Ruby.
class Trie
attr_accessor :word, :trie
def initialize
@trie = {}
@word = false
end
def <<(string)
node = string.each_char.inject(self) { |node, char| node.trie[char] ||= Trie.new }
@sirupsen
sirupsen / gist:6480217
Last active December 22, 2015 13:38
Quick and dirty way to check whether a string of parentheses are balanced.
def balanced?(string)
string.each_char.inject(0) { |open, char|
return false if open < 0
char == '(' ? open + 1 : open - 1
} == 0
end
p balanced?("()((()))")
p balanced?("())))")
p balanced?("()(((())")
@sirupsen
sirupsen / balanced.rb
Last active December 22, 2015 13:48
Benchmarks for a variety of ways to search through an array of strings for a prefix match.
class BBT
def initialize(array)
@set = array.sort
end
def lower_bound(element)
lower, upper = 0, @set.size - 1
while lower != upper
middle = (lower + upper) / 2
@sirupsen
sirupsen / gist:6481936
Last active May 4, 2019 05:07
Ruby implementation of a trie to use for a Letterpress/Wordfeud/Scrabble cheater.
require 'benchmark'
class SlowTrie
attr_accessor :word, :nodes
def initialize
@word, @nodes = false, {}
end
def <<(word)
@sirupsen
sirupsen / segment_tree.c
Last active December 23, 2015 02:39
Memory mapped minimum range segment tree.
/*
* This is a simple example of using memory mapped I/O with a data structure.
* The data structure used is a minimum query segment tree. This data structure
* can answer queries of which value in some interval from i..j is the minimum
* in O(log(n)) time. Updates are also O(log(n)).
*
* The data structure is persisted to the file passed as the first argument to
* the compiled program. Memory mapped I/O is nice because:
*
* 1. You get the speed and convenience of accessing memory.
@sirupsen
sirupsen / redis_queue.rb
Created September 15, 2013 15:59
Wrap Redis queues in an enumerable.
class RedisQueue
include Enumerable
def initialize(queue)
@queue = queue
end
def each(&block)
while element = redis.lpop(@queue)
block.call(*JSON.parse(element))
@sirupsen
sirupsen / hipsterfm.rb
Created September 15, 2013 16:01
How hipster is your taste in music?
require 'lastfm'
LASTFM_API_KEY = ""
LASTFM_SECRET = ""
SIRUPSEN = "f6b7ffe9b0c87fe8a341eb7a474f4574"
def get_token
lastfm = Lastfm.new(LASTFM_API_KEY, LASTFM_SECRET)
token = lastfm.auth.get_token