Created
November 22, 2013 19:33
-
-
Save geronimod/7605526 to your computer and use it in GitHub Desktop.
Kata 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
require 'minitest/autorun' | |
class BerlinClock | |
def self.seconds(seconds) | |
seconds.even? ? 'Y' : 'O' | |
end | |
def self.minutes(minutes) | |
row1 = Array.new(11, 'O').unshift(['Y'] * (minutes / 5)).flatten.first(11) | |
row2 = Array.new(4, 'O').unshift(['Y'] * (minutes % 5)).flatten.first(4) | |
[2,5,8].each { |ix| row1[ix] = 'R' if row1[ix] == 'Y' } | |
"#{row1.join('')} #{row2.join('')}" | |
end | |
def self.hours(hours) | |
row1 = Array.new(4, 'O').unshift(['R'] * (hours / 5)).flatten.first(4) | |
row2 = Array.new(4, 'O').unshift(['R'] * (hours % 5)).flatten.first(4) | |
"#{row1.join('')} #{row2.join('')}" | |
end | |
end | |
class String | |
def to_berlin_clock | |
hours, minutes, seconds = self.split(':').map &:to_i | |
"#{BerlinClock.seconds(seconds)} #{BerlinClock.hours(hours)} #{BerlinClock.minutes(minutes)}" | |
end | |
end | |
describe 'BerlinClock' do | |
describe 'internals methods' do | |
it 'seconds must to return Y/O' do | |
BerlinClock.seconds(0).must_equal 'Y' | |
BerlinClock.seconds(1).must_equal 'O' | |
BerlinClock.seconds(28).must_equal 'Y' | |
BerlinClock.seconds(57).must_equal 'O' | |
end | |
it 'hours must to return R/O in 2 rows' do | |
BerlinClock.hours(0).must_equal 'OOOO OOOO' | |
BerlinClock.hours(1).must_equal 'OOOO ROOO' | |
BerlinClock.hours(13).must_equal 'RROO RRRO' | |
BerlinClock.hours(16).must_equal 'RRRO ROOO' | |
end | |
it 'minutes must to return R/Y/O in the first row and Y/O in second one' do | |
BerlinClock.minutes(0).must_equal 'OOOOOOOOOOO OOOO' | |
BerlinClock.minutes(1).must_equal 'OOOOOOOOOOO YOOO' | |
BerlinClock.minutes(13).must_equal 'YYOOOOOOOOO YYYO' | |
BerlinClock.minutes(48).must_equal 'YYRYYRYYROO YYYO' | |
end | |
end | |
it "must to represent the input time in berlin-clock form" do | |
'00:00:00'.to_berlin_clock.must_equal 'Y OOOO OOOO OOOOOOOOOOO OOOO' | |
'13:17:01'.to_berlin_clock.must_equal 'O RROO RRRO YYROOOOOOOO YYOO' | |
'23:59:59'.to_berlin_clock.must_equal 'O RRRR RRRO YYRYYRYYRYY YYYY' | |
'24:00:00'.to_berlin_clock.must_equal 'Y RRRR RRRR OOOOOOOOOOO OOOO' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment