This file contains hidden or 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
#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) | |
{ |
This file contains hidden or 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
# 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 | |
# |
This file contains hidden or 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
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; |
This file contains hidden or 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 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 } |
This file contains hidden or 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 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?("()(((())") |
This file contains hidden or 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 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 |
This file contains hidden or 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
require 'benchmark' | |
class SlowTrie | |
attr_accessor :word, :nodes | |
def initialize | |
@word, @nodes = false, {} | |
end | |
def <<(word) |
This file contains hidden or 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
/* | |
* 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. |
This file contains hidden or 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 RedisQueue | |
include Enumerable | |
def initialize(queue) | |
@queue = queue | |
end | |
def each(&block) | |
while element = redis.lpop(@queue) | |
block.call(*JSON.parse(element)) |
This file contains hidden or 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
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 |