Skip to content

Instantly share code, notes, and snippets.

@raven29
raven29 / teams.erl
Created September 6, 2012 12:54
Soccer teams
-module(teams).
-export([start/0, sets/1]).
start() ->
Pid = spawn(fun loop/0),
spawn(teams, sets, [Pid]).
loop() ->
process_flag(trap_exit, true),
loop([], 0).
@raven29
raven29 / InversionCounterArrayNum.rb
Created July 3, 2012 07:18
InversionCounterArrayNum.rb - for an array of arbitrary real numbers
class FenwickTree
def initialize(n)
@n = n
@t = Array.new(n, 0)
end
def update(i, delta)
while i < @n do
@raven29
raven29 / InversionCounterArrayInt.rb
Created July 3, 2012 07:05
InversionCounterArrayInt.rb - for an array of arbitrary integers
class FenwickTree
def initialize(n)
@n = n
@t = Array.new(n, 0)
end
def update(i, delta)
while i < @n do
@t[i] += delta
@raven29
raven29 / InversionCounterPermut.rb
Created July 2, 2012 07:03
InversionCounterPermut.rb
class FenwickTree
def initialize(n)
@n = n
@t = Array.new(n, 0)
end
def update(i, delta)
while i < @n do
@t[i] += delta
@raven29
raven29 / SumCheckerHash.rb
Created June 25, 2012 06:59
SumCheckerHash
class SumCheckerHash
def initialize(file_name)
@hsh = {}
File.open(file_name).each{|line| @hsh[line.to_i] = @hsh[line.to_i].nil? ? 1 : (@hsh[line.to_i]+1)}
end
def check(sum)
@hsh.each_key{|i| return true if @hsh.has_key?(sum - i) && ((2*i != sum) || (@hsh[i] > 1))}
return false
@raven29
raven29 / SumCheckerBinary.rb
Created June 25, 2012 06:53
SumCheckerBinary
class MySortedArray < Array
def initialize(ary)
super(ary.sort!)
end
def search_binary(el, start_pos=0, end_pos=self.length-1)
if start_pos == end_pos - 1
if self[start_pos] == el or self[end_pos] == el
return true
@raven29
raven29 / gist:2908925
Created June 11, 2012 07:38
Skip finder corrected
class File
attr_reader :pos_start_line, :eol_length
def set_eol_length
rewind
str = gets
@eol_length = self.pos - str.length + 1
rewind
end
@raven29
raven29 / gist:2874101
Created June 5, 2012 09:58
Skip finder
class File
attr_reader :pos_start_line
def seek_line(pos)
if pos < 1
seek(0)
@pos_start_line = 0
return gets
else