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
#!/usr/bin/env ruby | |
def rand5() | |
rand(5) + 1 | |
end | |
def rand7() | |
(7 * rand5()) / 5 | |
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
#!/usr/bin/env ruby | |
def out_of_order set | |
n = set.size() | |
inv = 0 | |
n.times do |i| | |
(i+1...n).each do |j| | |
inv += 1 if set[i] > set[j] | |
end | |
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
#!/usr/bin/env ruby | |
class Stack | |
def initialize(init=nil) | |
@stack = [] | |
@stack << init if !init.nil? | |
end | |
def push val | |
@stack << val |
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
#!/usr/bin/env ruby | |
def isSubsetSum(set,n, sum) | |
return true if (sum == 0) | |
return false if (n == 0 and sum != 0) | |
return isSubsetSum(set, n - 1, sum) if (set[n - 1] > sum) | |
return isSubsetSum(set, n-1, sum) || isSubsetSum(set, n-1, sum - set[n-1]) | |
end | |
s = [12, 1, 61, 5, 9, 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
#!/usr/bin/env ruby | |
def isSubsetSum(set,n, sum) | |
return true if (sum == 0) | |
return false if (n == 0 and sum != 0) | |
return isSubsetSum(set, n - 1, sum) if (set[n - 1] > sum) | |
return isSubsetSum(set, n-1, sum) || isSubsetSum(set, n-1, sum - set[n-1]) | |
end | |
s = [12, 1, 61, 5, 9, 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
#!/usr/bin/env ruby | |
def non_duplicate_in_set set | |
freq = set.inject(Hash.new(0)) {|hash,word| hash[word] += 1; hash } | |
freq.keys.each { |k| return k if freq[k] < 3 } | |
end | |
set = [6, 1, 3, 3, 3, 6, 6] | |
puts non_duplicate_in_set(set) |
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 medians(stream) | |
smaller, greater = Array.new, Array.new | |
med = stream[0] | |
smaller << stream[0] | |
puts med | |
for i in 1...stream.length | |
current = stream[i] | |
if smaller.size() > greater.size() | |
if current < med | |
greater << smaller[-1] |
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
#!/usr/bin/env ruby | |
def power_set(set) | |
size = set.size() ** 2 | |
size.times do |counter| | |
size.times do |j| | |
if ((counter & (1 << j)) > 0) | |
print set[j] | |
end | |
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
#!/usr/bin/env ruby | |
class Node | |
attr_accessor :val, :left, :right | |
def initialize(val, left=nil, right=nil) | |
@val = val | |
@left = left | |
@right = right | |
end | |
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
#!/usr/bin/env ruby | |
def sort(array) | |
order = ["R","G","B"] | |
res = Array.new(order.size()) { Array.new } | |
array.each { |c| res[order.index(c)] << c } | |
return res.flatten | |
end | |
def sort_proper(array) |