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 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 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 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 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 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 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 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<complex> | |
#include<cmath> | |
#include<iostream> | |
using namespace std; | |
typedef complex<double> Point; | |
#define x imag() | |
#define y real() |
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
s = gets.strip | |
m = { | |
?L => ->(z) { z + z.real }, | |
?R => ->(z) { z + Complex(z.real, z.real) }, | |
?P => ->(z) { z }, | |
?* => ->(z) { m[?L][z] + m[?R][z] + z } | |
} | |
z = s.reverse.each_char.inject(Complex(1)) { |z, c| m[c][z] } |
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 Bayes | |
def initialize | |
@frequency = {} | |
@categories = Hash.new 0 | |
end | |
# Prob(Category|Features) = Prob(Category) Prob(Features|Category) | |
def probability(features, category) | |
features_probability(features, category) * category_probability(category) | |
end |
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
#include<iostream> | |
#include<cstring> | |
using namespace std; | |
string s1, s2; | |
int n; | |
int lcs() | |
{ |