Skip to content

Instantly share code, notes, and snippets.

View seanhandley's full-sized avatar
🧀
Eating cheese

Sean Handley seanhandley

🧀
Eating cheese
View GitHub Profile
@seanhandley
seanhandley / next_to.rb
Created November 14, 2015 22:07
Einstein 7: Next To
def left_of?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i+1] == val_b
end
end
# Instead of defining a right_of?, we can just swap the values we input to left_of?
def next_to?(set_a, val_a, set_b, val_b)
@seanhandley
seanhandley / left_of.rb
Created November 14, 2015 21:59
Einstein 6: Left of
def left_of?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i+1] == val_b
end
end
colors = [:white, :yellow, :blue, :red, :green]
left_of?(colors, :green, colors, :white)
@seanhandley
seanhandley / implies_facts.rb
Last active November 14, 2015 21:49
Einstein 5: All the implies
implies?(nationalities, :british, colors, :red)
implies?(nationalities, :swedish, pets, :dogs)
implies?(colors, :green, drinks, :coffee)
implies?(nationalities, :danish, drinks, :tea)
implies?(cigars, :pall_mall, pets, :birds)
implies?(nationalities , :german, cigars, :prince)
implies?(colors, :yellow, cigars, :dunhill)
implies?(cigars, :bluemasters, drinks, :beer)
@seanhandley
seanhandley / implies.rb
Created November 14, 2015 21:36
Einstein 4: The Implies Method
def implies?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i] == val_b
end
end
# The German lives in the third house (position 2)
nationalities = [:swedish, :british, :german, :norwegian, :danish]
# The third house' occupant smokes Prince (position 2)
@seanhandley
seanhandley / array.rb
Last active November 14, 2015 21:27
Einstein 3: Array ordering
colors = [:white, :yellow, :blue, :red, :green]
# House 1 => white
# House 2 => yellow
# House 3 => blue
# House 4 => red
# House 5 => green
@seanhandley
seanhandley / loops.rb
Last active November 14, 2015 21:09
Einstein 2: Nesting the loops
[:white, :yellow, :blue, :red, :green].permutation.each do |colors|
[:german, :swedish, :british, :norwegian, :danish,].permutation.each do |nationalities|
[:birds, :cats, :horses, :fish, :dogs].permutation.each do |pets|
[:beer, :water, :tea, :milk, :coffee].permutation.each do |drinks|
[:blends, :pall_mall, :prince, :bluemasters, :dunhill].permutation.each do |cigars|
# ...
end
end
end
end
@seanhandley
seanhandley / permutation.rb
Last active November 14, 2015 23:37
Einstein 1: Ruby's Array#permutation Method
colors = [:white, :yellow, :blue, :red, :green].permutation
# => #<Enumerator: [:white, :yellow, :blue, :red, :green]:permutation>
colors = [:white, :yellow, :blue, :red, :green].permutation.to_a[0]
# => [:white, :yellow, :blue, :red, :green]
colors = [:white, :yellow, :blue, :red, :green].permutation.to_a[1]
# => [:white, :yellow, :blue, :green, :red]
colors = [:white, :yellow, :blue, :red, :green].permutation.to_a[2]
# => [:white, :yellow, :red, :blue, :green]

Keybase proof

I hereby claim:

  • I am seanhandley on github.
  • I am seanhandley (https://keybase.io/seanhandley) on keybase.
  • I have a public key whose fingerprint is 9685 2CA9 5B02 1A6F 9DB4 73FC 42C5 9834 59D4 0286

To claim this, I am signing this object:

@seanhandley
seanhandley / einstein.rb
Created October 26, 2015 11:53
Einstein's Puzzle
# There are five houses in five different colors in a row.
# In each house lives a person with a different nationality.
# The five owners drink a certain type of beverage,
# smoke a certain brand of cigar and keep a certain pet.
# No owners have the same pet, smoke the same brand of cigar,
# or drink the same beverage.
# The question is: who owns the fish?
time_start = Time.now
@seanhandley
seanhandley / 1 sources.list
Last active October 18, 2015 08:41
Enable HTTP2 in NginX on Ubuntu
# ...
deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx
# Where 'trusty' is the Ubuntu release codename (see `cat /etc/lsb-release`)