Created
June 23, 2010 19:18
-
-
Save madebydna/450414 to your computer and use it in GitHub Desktop.
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
# Triangle Problem: | |
def triangle(a, b, c) | |
raise TriangleError if [a,b,c].detect{|arg| arg <= 0 } | |
if a == b && a == c | |
:equilateral | |
elsif (a == b) || (a == c) || (b == c) | |
h = Hash.new(0) | |
[a,b,c].each do |v| | |
h[v] += 1 | |
end | |
base = h.index 1 | |
side = h.index 2 | |
raise TriangleError if (side*side - (base.to_f/2)*(base.to_f/2)) <= 0 | |
:isosceles | |
else | |
:scalene | |
end | |
end | |
# Error class used in part 2. No need to change this code. | |
class TriangleError < StandardError | |
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
# Keeping score for the dice game Greed | |
def score(dice) | |
score = 0 | |
h = Hash.new(0) | |
dice.each do |number| | |
h[number] += 1 | |
end | |
h.each do |number, count| | |
if count >= 3 | |
number == 1 ? score += 1000 : score += number * 100 | |
count -= 3 | |
score += 100 * count if number == 1 | |
score += 50 * count if number == 5 | |
else | |
score += 100 * count if number == 1 | |
score += 50 * count if number == 5 | |
end | |
end | |
return score | |
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
# Creating a proxy class that keeps track of methods invoked | |
class Proxy | |
def initialize(target_object) | |
@object = target_object | |
@messages = [] | |
end | |
def messages | |
@messages | |
end | |
def called?(method_name) | |
@messages.include?(method_name) | |
end | |
def number_of_times_called(method_name) | |
@messages.select {|m| m == method_name }.length | |
end | |
def method_missing(method_name, *args, &block) | |
@messages << method_name.to_sym | |
@object.send(method_name, *args, &block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment