Last active
December 10, 2015 16:28
-
-
Save jcla1/4461611 to your computer and use it in GitHub Desktop.
A little challenge @qmacro gave me, about time.
This file contains hidden or 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
SECOND = 1 | |
MINUTE = 60 * SECOND | |
HOUR = 60 * MINUTE | |
time = Time.new 0, 1, 1, 0, 0 | |
end_time = Time.new 0, 1, 2, 0, 0 | |
$coincidences = [] | |
class Time | |
def to_s | |
hours = self.hour | |
minutes = self.min | |
time_string = "%02d:%02d" % [hours, minutes] | |
return time_string | |
end | |
# Methods to check coincidences | |
def palindrome? | |
is_palindrome = self.to_s == self.to_s.reverse | |
$coincidences.push self if is_palindrome | |
return is_palindrome | |
end | |
def hours_are_same_as_mins? | |
hours = "%02d" % [self.hour] | |
mins = "%02d" % [self.min] | |
hours_are_same_as_mins = hours == mins | |
$coincidences.push self if hours_are_same_as_mins | |
return hours_are_same_as_mins | |
end | |
def ladder? | |
hours = "%02d" % [self.hour] | |
mins = "%02d" % [self.min] | |
first_digit = hours[0].to_i | |
ladder_asc = [first_digit, first_digit+1, first_digit+2, first_digit+3].map {|e| e.to_s} | |
time_arr = (hours + mins).split "" | |
ladder = time_arr == ladder_asc | |
$coincidences.push self if ladder | |
return ladder | |
end | |
end | |
while time < end_time | |
time += MINUTE | |
next if time.palindrome? | |
next if time.hours_are_same_as_mins? | |
next if time.ladder? | |
end | |
puts $coincidences |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment