Chris Pine Leap Year

This one is stumping me because of all the variables needed. Only thing
taught before this challenge was basic branches and loops. This is what
I have so far.

puts 'Leap Year Calculator'
puts ' '
puts 'Enter the starting year.'
starting_year = gets.chomp
puts 'Enter the ending year.'
ending_year = gets.chomp

while true
  if starting_year % 400 ||starting_year % 4
    leap_year = starting_year
  elsif starting_year % 100
    leap_year = starting_year + 4
  else
    # need something to check until it reaches / by 4 or 400, adding one
each time maybe.
  end
    while leap_year.to_i < ending_year.to_i
      puts leap_year
      leap_year = leap_year.to_i + 4
    end
  break
end

Works okay if I put a leap year, or one dividable by 100. But I'm not
sure how to do this when I enter a number not dividable by 100, 4, or
400 (Where I have the comment is where I think the code needs to go.)
Also what I'm using is all the book has gone over so far so please,
nothing not using what I'm using. Thanks in advance.

···

--
Posted via http://www.ruby-forum.com/.

The rest doesn't work at all because if you put something like 2096-2104
you would print out 2096, 2100 which is incorrect in two ways

1) 2100 isn't a leap year
2) 2104 should be printed and your second while clause precludes that

You shouldn't be worrying about the starting year as much as you are - you
need to handle every leap year in the same fashion

So lets try something like this

leap_year = starting_year + starting_year % 4 #set yourself onto a possible
leap year to start with

while leap_year <= ending_year
  if (leap_year % 100) != 0 || (leap_year % 400) == 0
    puts leap_year
  end
  leap_year = leap_year + 4
end

And if for some reason you have a problem with the or statement ("||") you
can use this instead

if (leap_year % 100) != 0
  puts leap_year
elsif (leap_year % 400) == 0
  puts leap_year
end

John

···

On Thu, Mar 28, 2013 at 1:17 PM, Phil H. <lists@ruby-forum.com> wrote:

This one is stumping me because of all the variables needed. Only thing
taught before this challenge was basic branches and loops. This is what
I have so far.

puts 'Leap Year Calculator'
puts ' '
puts 'Enter the starting year.'
starting_year = gets.chomp
puts 'Enter the ending year.'
ending_year = gets.chomp

I'm a bit depressed that worked so easily :frowning:

I just could not come to that simple conclusion. Yes we did learn about
OR, AND, and one other that escapes me. I though about that as well.
Also setting the leap year ,it wouldn't work unless I implicitly set it
to an integer. So my final code is:

puts 'Leap Year Calculator'
puts ' '
puts 'Enter the starting year.'
starting_year = gets.chomp
puts 'Enter the ending year.'
ending_year = gets.chomp
leap_year = starting_year.to_i + starting_year.to_i % 4

while leap_year.to_i <= ending_year.to_i
  if (leap_year % 100) != 0 || (leap_year % 400) == 0
    puts leap_year
  end
  leap_year = leap_year.to_i + 4
end

Sobering :frowning:

···

--
Posted via http://www.ruby-forum.com/.

does not work:

(1..12).each {|year| puts year + year % 4 }
2
4
6
4
6
8
10
8
10
12
14
12

···

Am 28.03.2013 21:48, schrieb John W Higgins:

On Thu, Mar 28, 2013 at 1:17 PM, Phil H. <lists@ruby-forum.com > <mailto:lists@ruby-forum.com>> wrote:

    This one is stumping me because of all the variables needed. Only thing
    taught before this challenge was basic branches and loops. This is what
    I have so far.

    puts 'Leap Year Calculator'
    puts ' '
    puts 'Enter the starting year.'
    starting_year = gets.chomp
    puts 'Enter the ending year.'
    ending_year = gets.chomp

The rest doesn't work at all because if you put something like 2096-2104
you would print out 2096, 2100 which is incorrect in two ways

1) 2100 isn't a leap year
2) 2104 should be printed and your second while clause precludes that

You shouldn't be worrying about the starting year as much as you are -
you need to handle every leap year in the same fashion

So lets try something like this

leap_year = starting_year + starting_year % 4 #set yourself onto a
possible leap year to start with

--
<https://github.com/stomar/&gt;

Absolutely correct - stupid line by me

How about

sy = starting_year.to_i

then either of

leap_year = sy - sy % 4
leap_year = leap_year + 4 unless sy == leap_year

or a single line overly cute option

leap_year = sy - sy % 4 + 4 * (sy % 4 <=> 0) # I can't think of another way
to get the signum function in ruby

John

···

On Thu, Mar 28, 2013 at 3:05 PM, <sto.mar@web.de> wrote:

Am 28.03.2013 21:48, schrieb John W Higgins:

On Thu, Mar 28, 2013 at 1:17 PM, Phil H. <lists@ruby-forum.com >> <mailto:lists@ruby-forum.com>> wrote:

    This one is stumping me because of all the variables needed. Only
thing
    taught before this challenge was basic branches and loops. This is
what
    I have so far.

    puts 'Leap Year Calculator'
    puts ' '
    puts 'Enter the starting year.'
    starting_year = gets.chomp
    puts 'Enter the ending year.'
    ending_year = gets.chomp

The rest doesn't work at all because if you put something like 2096-2104
you would print out 2096, 2100 which is incorrect in two ways

1) 2100 isn't a leap year
2) 2104 should be printed and your second while clause precludes that

You shouldn't be worrying about the starting year as much as you are -
you need to handle every leap year in the same fashion

So lets try something like this

leap_year = starting_year + starting_year % 4 #set yourself onto a
possible leap year to start with

does not work: