Skip to content

Instantly share code, notes, and snippets.

View dscataglini's full-sized avatar

Diego Scataglini dscataglini

View GitHub Profile
@dscataglini
dscataglini / gist:6345942
Created August 26, 2013 20:00
Fizz Buzz
# Fizz Buzz
# Print the numbers from 1 to 100
# If a number is divisible by 3 print "Fizz" instead
# If a number is divisible by 5 print "Buzz" instead
# If a number is divisible by 3 and 5 print "FizzBuzz" instead
# Sample output
# 1
# 2
# Fizz
# 4
@dscataglini
dscataglini / gist:6345933
Created August 26, 2013 19:59
Factorial
# write a recursive function that brings back a factorial
#
# How does factorial work:
# 0! = 1 #=> 1
# 1! = 1 #=> 1
# 2! = 2 * 1 #=> 2
# 3! = 3 * 2 * 1 #=> 6
# 4! = 4 * 3 * 2 * 1 #=> 24
# 5! = 5 * 4 * 3 * 2 * 1 #=> 120
@dscataglini
dscataglini / gist:6345924
Created August 26, 2013 19:57
Rails Problem #1
# Write a url shortener application in Rails.
@dscataglini
dscataglini / gist:6345914
Created August 26, 2013 19:57
Rails Problem #1
# Write a url shortener application in Rails.
@dscataglini
dscataglini / gist:6345908
Created August 26, 2013 19:56
Erb question
# How would you write an erb file that produces exactly the following output
<articles>
<article>Title</article>
<article>Title</article>
<article class=”high”>Title</article>
<article>Title</article>
<article>Title</article>
<article class=”high”>Title</article>
<article>Title</article>
<article>Title</article>
@dscataglini
dscataglini / gist:6345896
Created August 26, 2013 19:55
Hash Traversal
# Given the following hash
h = {a: 1, b1: {b2: {b3: nil}, c2: nil}, c1: {c2: {c3: nil}}}
# Write a function that prints out all the keys
# Given the following array:
# assume ruby 1.9+
array = [1,2,3,4,5,6,7,8,9,10]
# You want to print them out like so
# [first, last]
# [first, rest]
# [first, rest minus last, last]
@dscataglini
dscataglini / gist:6345869
Created August 26, 2013 19:53
Robot Problem
# There are 2 robots placed on an infinitely wide 2 dimensional line.
# The robots can’t see each other.
# Each robot has 1 marker to the right but 1 of them doesn’t have one on the left.
# - unit of distance
# ^ marker
# ~ indefinite amount of units of distance
# ---~---(Robot)---~---^---~---(Robot)---~--^---~---
# Write a program that will make that 2 robots collide using the following Robot Api:
# move_left(n)
@dscataglini
dscataglini / railsconf_schedule.rb
Created April 17, 2012 14:57
Getting railsconf schedule grouped by time
require 'open-uri'
require 'json'
def get(key)
lambda { |s| s[key].empty? ? nil : s[key] }
end
# ["title", "abstract", "name", "bio", "starts_at", "ends_at", "category", "room"]
@dscataglini
dscataglini / PaymentProxy.rb
Created March 23, 2012 17:41
PaymentProxy.rb
class PaymentProxy
def initialize(gateway)
@gateway = gateway
@observers = []
end
def register_observer(o)
# ...
end