View mastermind_test.rb
1 2 3 4 5 6 7 8 9 10
require 'minitest/autorun'
require 'minitest/spec'
require 'minitest/pride'
require 'stringio'
 
 
# I *might* wind up with classes like this
class RandomInputGenerator
def call
4.times.map { ['R', 'G', 'B', 'Y'].sample }.join
View experiment.rb
1 2 3 4 5 6 7 8 9 10
# ===== Environment (MRI 2.1.1) =====
RUBY_VERSION # => "2.1.1"
RUBY_PLATFORM # => "x86_64-darwin13.0"
RbConfig.ruby # => "/Users/josh/.rubies/ruby-2.1.1/bin/ruby"
`#{RbConfig.ruby} -v` # => "ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]\n"
 
# ===== The Hypothesis =====
# A recursive method can call itself about 5000 times before it overflows.
 
# ===== Test #1 =====
View review.md
View path.rb
1 2 3 4 5 6 7 8 9 10
def Path(str)
Path.new str
end # => :Path
class Path
def initialize(str='.')
@this = File.join to_paths(str) # => ".", "/a/b/c/d", "./code", "./code/projects", "/Users/josh", "/Users/josh", "/Users/josh/code", "/Users/josh/code/projects", "/a/b/c", "/a/b/c", "/a/b", "/a/b/c", "/a/b", "/a", "/a/b/c", "/a/b", "/a", "/", "d", "d", "/a/b/c", "c", "d", "/a/b/c", "c", "/a/b", "b", "d", "/a/b/c", "c", "/a/b", "b", "/a", "a", "d", "/a/b/c", "c", "/a/b", "b", "/a", "a", "/", "/", "a/b/c", "a/b/c", "a/b", "a/b/...
end # => :initialize
def ~@
new File.expand_path this # => #<Path /Users/josh>, #<Path /Users/josh>
View floodfill.rb
1 2 3 4 5 6 7 8 9 10
# Solution to this problem
# from http://www.reddit.com/r/dailyprogrammer/comments/2ug3hx/20150202_challenge_200_easy_floodfill/
#
# Mimicing this python solution in Ruby
# http://www.reddit.com/r/dailyprogrammer/comments/2ug3hx/20150202_challenge_200_easy_floodfill/co83w1u
 
def floodfill(instream, outstream)
width, height = instream.gets.split.map &:to_i
grid = Array.new(height) { instream.gets }
x, y, newchar = instream.gets.split
View mincut.rb
1 2 3 4 5 6 7 8 9 10
def non_cyclical_paths adjacency, &block
adjacency.each do |current, associations|
_non_cyclical_paths adjacency, [current], &block
end
end
 
def _non_cyclical_paths adjacency, path, &block
current = path.last
adjacency[current].each do |adjacent|
next if path.include? adjacent
View mincut-smallest-paths-proof.txt
1 2 3 4 5 6 7 8 9 10
Say we have the graph G, and if we were to make the minimum cut,
we would have two subgraphs.
 
Say we have 2 nodes from G, then, we'll call them A and B.
 
There are two possibilities, then. Either A and B are in the same subgraph, or they aren't.
 
If they are in different subgraphs, then how many connections do they have to each other?
It must be the number that we cut, because that's the number of paths that connect the two
subgraphs. We know it can use each of these paths, because it can reach any other node in
View min_cut.txt
1 2 3 4 5 6 7 8 9 10
In this graph, the minimum cut is 2 (if you cut any 2 edges, you would have 2 graphs)
a --- b
| |
c --- d
 
In this graph, the minimum cut is 1 (cut ab to get [a], and [b, c])
a --- b --- c
\_/
 
In this graph, the minimum cut is 1 (cut de to get [a,b,c,d] and [e,f,g,h])
View shell-notes-for-kit.txt
1 2 3 4 5 6 7 8 9 10
Readline keybindings (work in the shell, some of the nav ones work everywhere in a mac)
C-something # hold control and press the "something" key
M-something # hold control and press the "something" key
 
Navigation
C-a # beginning of line
C-e # end of line
 
C-b # back a char
M-b # back a word
View option_parser_example.rb
1 2 3 4 5 6 7 8 9 10
# Have to hijack the global environment, b/c that's how invasive it is!
def self.wtf
to_return = {exception: nil, stdout: nil, stderr: nil}
require 'stringio'
$stdout = StringIO.new
$stderr = StringIO.new
to_return[:returned] = yield
rescue Exception
to_return[:exception] = $!
ensure
Something went wrong with that request. Please try again.