Skip to content

Instantly share code, notes, and snippets.

@kotp
Forked from odiepus/LeapYear.rb
Created December 29, 2011 06:34
Show Gist options
  • Save kotp/1532396 to your computer and use it in GitHub Desktop.
Save kotp/1532396 to your computer and use it in GitHub Desktop.
Leap Year with Minutes In Year
=begin
doctest: leap_year? for 1900 and 1999 and 2001 is false
>> leap_year?(1900)
=> false
>> leap_year?(1999)
=> false
>> leap_year?(2001)
=> false
doctest: leap_year? for 1904, 1996 and 2000 are true
>> leap_year? 1904
=> true
>> leap_year? 1996
=> true
>> leap_year? 2000
=> true
=end
def leap_year? (input_year)
input_year % 400 == 0 || input_year % 100 != 0 && input_year % 4 == 0
end
=begin
doctest: minutes_in_year gives me 525600 for non-leap
>> minutes_in_year(1900)
=> 525600
doctest: minutes_in_year gives me 525040 for leap years
>> minutes_in_year(2000)
=> 527040
=end
def minutes_in_year(year)
24 * 60 * ( leap_year?(year) ? 366 : 365)
end
=begin
doctest: Using the methods to get output that I expect
=end
if __FILE__ == $0 # This checks to see if this is the file that is executed or if it was loaded as a library file.
puts "Please enter a year to see if it is a leap year: "
STDOUT.flush
year_input = gets.to_i
puts "Year is #{year_input} and it is a #{leap_year?(year_input) ? "leap" : "standard"} year, with #{minutes_in_year(year_input)} minutes."
input_years = [1900, 1999, 2001, 1904, 1996, 2000]
input_years.each do |year_input|
puts "Year is #{year_input} and it is a #{leap_year?(year_input) ? "leap" : "standard"} year, with #{minutes_in_year(year_input)} minutes."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment