Last active
August 29, 2015 14:01
-
-
Save emadb/68fa25fdcfabbf3973ef to your computer and use it in GitHub Desktop.
Berlin clock kata implemented in ruby (http://content.codersdojo.org/code-kata-catalogue/berlin-clock/)
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
class BerlinClock | |
SECONDS = "Y" | |
HOURS_FIVES = "RRRR" | |
HOURS_ONES = "RRRR" | |
MINUTES_FIVES = "YYRYYRYYRYY" | |
MINUTES_ONES = "YYYY" | |
def initialize(h,m,s) | |
@h, @m, @s = h, m, s | |
end | |
def format(line, part, op, mod=5) | |
on_count = part.public_send(op, mod) | |
off_count = line.length - on_count | |
line[0, on_count] + "O" * off_count | |
end | |
def seconds | |
format(SECONDS, @s + 1, :%, 2) | |
end | |
def hours_fives | |
format(HOURS_FIVES, @h, :/) | |
end | |
def hours_ones | |
format(HOURS_ONES, @h, :%) | |
end | |
def minutes_ones | |
format(MINUTES_ONES, @m, :%) | |
end | |
def minutes_fives | |
format(MINUTES_FIVES, @m, :/) | |
end | |
def to_s | |
"#{seconds}\n#{hours_fives}\n#{hours_ones}\n#{minutes_fives}\n#{minutes_ones}" | |
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
require './lib/berlin_clock' | |
describe BerlinClock do | |
it 'midnight all lights are off, but Y' do | |
clock = BerlinClock.new(0,0,0) | |
expect(clock.to_s).to eq("Y\nOOOO\nOOOO\nOOOOOOOOOOO\nOOOO") | |
end | |
it '1 second after midnight all lights are off' do | |
clock = BerlinClock.new(0,0,1) | |
expect(clock.to_s).to eq("O\nOOOO\nOOOO\nOOOOOOOOOOO\nOOOO") | |
end | |
it '1 hour after midnight third row has 1 light on' do | |
clock = BerlinClock.new(1,0,0) | |
expect(clock.to_s).to eq("Y\nOOOO\nROOO\nOOOOOOOOOOO\nOOOO") | |
end | |
it '2 hours after midnight third row has 2 light on' do | |
clock = BerlinClock.new(2,0,0) | |
expect(clock.to_s).to eq("Y\nOOOO\nRROO\nOOOOOOOOOOO\nOOOO") | |
end | |
it '5 hours after midnight second row has 1 light on' do | |
clock = BerlinClock.new(5,0,0) | |
expect(clock.to_s).to eq("Y\nROOO\nOOOO\nOOOOOOOOOOO\nOOOO") | |
end | |
it '6 hours after midnight second row has 1 light on and third has 1 light' do | |
clock = BerlinClock.new(6,0,0) | |
expect(clock.to_s).to eq("Y\nROOO\nROOO\nOOOOOOOOOOO\nOOOO") | |
end | |
it '13 hours after midnight second row has 2 lights on and third has 3 lights' do | |
clock = BerlinClock.new(13,0,0) | |
expect(clock.to_s).to eq("Y\nRROO\nRRRO\nOOOOOOOOOOO\nOOOO") | |
end | |
it '1 minute after midnight last row has 1 lights on' do | |
clock = BerlinClock.new(0,1,0) | |
expect(clock.to_s).to eq("Y\nOOOO\nOOOO\nOOOOOOOOOOO\nYOOO") | |
end | |
it '5 minutes after midnight minutes row has 1 lights on' do | |
clock = BerlinClock.new(0,5,0) | |
expect(clock.to_s).to eq("Y\nOOOO\nOOOO\nYOOOOOOOOOO\nOOOO") | |
end | |
it '13:17:01' do | |
clock = BerlinClock.new(13,17,01) | |
expect(clock.to_s).to eq("O\nRROO\nRRRO\nYYROOOOOOOO\nYYOO") | |
end | |
it '23:59:59' do | |
clock = BerlinClock.new(23,59,59) | |
expect(clock.to_s).to eq("O\nRRRR\nRRRO\nYYRYYRYYRYY\nYYYY") | |
end | |
it '24:00:00' do | |
clock = BerlinClock.new(24,00,00) | |
expect(clock.to_s).to eq("Y\nRRRR\nRRRR\nOOOOOOOOOOO\nOOOO") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment