Help with a program to determin leap years.

Discussion in 'Ruby' started by Shiloh Madsen, Nov 7, 2006.

  1. So, I'm trying to go through the Teach Yourself Programming book by
    Pragmatic Press and I am encountering a few hurdles. The chapter I am
    working on now is asking me to create a program which will ask for a
    start and end year and then calculate all leap years in that range.
    The logic behind leap years (for those who need a refresher) is all
    years divisible by for are leap years EXCEPT those that are divisible
    by 100 UNLESS they are also divisible by 400. I am somewhat at a loss
    for how to handle the logic for this...finding all numbers that are
    divisible by 4 and removing those divisible by 100 is easy. Its adding
    in that third condition which adds some of the removed numbers back
    into the "true" group that I am having trouble with...or maybe I am
    just not wrapping my mind around the problem well
    enough...suggestions?
    Shiloh Madsen, Nov 7, 2006
    #1
    1. Advertising

  2. On Nov 7, 2006, at 12:15 PM, Shiloh Madsen wrote:

    > So, I'm trying to go through the Teach Yourself Programming book by
    > Pragmatic Press and I am encountering a few hurdles. The chapter I am
    > working on now is asking me to create a program which will ask for a
    > start and end year and then calculate all leap years in that range.
    > The logic behind leap years (for those who need a refresher) is all
    > years divisible by for are leap years EXCEPT those that are divisible
    > by 100 UNLESS they are also divisible by 400. I am somewhat at a loss
    > for how to handle the logic for this...finding all numbers that are
    > divisible by 4 and removing those divisible by 100 is easy. Its adding
    > in that third condition which adds some of the removed numbers back
    > into the "true" group that I am having trouble with...or maybe I am
    > just not wrapping my mind around the problem well
    > enough...suggestions?


    Here is simple way:

    1. Write a is_leap? method.

    def is_leap?(y)
    return true if y % 400 == 0
    return false if y % 100 == 0
    return true if y % 4 == 0
    false
    end

    2. <range>.select { |y| is_leap?(y) }

    For example,

    (1895..1905).select { |y| is_leap?(y) } # => [1896, 1904]
    (1995..2005).select { |y| is_leap?(y) } # => [1996, 2000, 2004]

    Regards, Morton
    Morton Goldberg, Nov 7, 2006
    #2
    1. Advertising

  3. Shiloh Madsen

    Guest

    Hi --

    On Wed, 8 Nov 2006, Shiloh Madsen wrote:

    > So, I'm trying to go through the Teach Yourself Programming book by
    > Pragmatic Press and I am encountering a few hurdles. The chapter I am
    > working on now is asking me to create a program which will ask for a
    > start and end year and then calculate all leap years in that range.
    > The logic behind leap years (for those who need a refresher) is all
    > years divisible by for are leap years EXCEPT those that are divisible
    > by 100 UNLESS they are also divisible by 400. I am somewhat at a loss
    > for how to handle the logic for this...finding all numbers that are
    > divisible by 4 and removing those divisible by 100 is easy. Its adding
    > in that third condition which adds some of the removed numbers back
    > into the "true" group that I am having trouble with...or maybe I am
    > just not wrapping my mind around the problem well
    > enough...suggestions?


    require 'date'
    Date.leap?(year) # :)

    See Morton's implementation. Here, just for fun, is another:

    def leap?(year)
    year % 4 == 0 unless (year % 100 == 0 unless year % 400 == 0)
    end

    It returns nil/true rather than false/true, so it's a bit
    non-slick. But I thought the semantics might be interesting.


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
    , Nov 7, 2006
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    * Morton Goldberg, 11/07/2006 09:09 PM:
    > 1. Write a is_leap? method.
    >
    > def is_leap?(y)
    > return true if y % 400 == 0
    > return false if y % 100 == 0
    > return true if y % 4 == 0
    > false
    > end


    YAWOFOIAYIALY:

    def is_leap?(y)
    return true if y % 400 == 0
    return false if y % 100 == 0
    return y % 4 == 0
    end

    Jupp
    - --
    YAWOFOIAYIALY: Yet another way of finding out if a year is a leap year.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (GNU/Linux)

    iD8DBQFFUPEXrhv7B2zGV08RAg/HAJ9GqtURKVlnjqFwH7UzRo2X+LMnXgCgoIG5
    XFMK3EpMrOjM05IAD8UfHTE=
    =N0Le
    -----END PGP SIGNATURE-----
    Josef 'Jupp' Schugt, Nov 7, 2006
    #4
  5. On 11/7/06, <> wrote:
    > Hi --
    >
    > On Wed, 8 Nov 2006, Shiloh Madsen wrote:
    >
    > > So, I'm trying to go through the Teach Yourself Programming book by
    > > Pragmatic Press and I am encountering a few hurdles. The chapter I am
    > > working on now is asking me to create a program which will ask for a
    > > start and end year and then calculate all leap years in that range.
    > > The logic behind leap years (for those who need a refresher) is all
    > > years divisible by for are leap years EXCEPT those that are divisible
    > > by 100 UNLESS they are also divisible by 400. I am somewhat at a loss
    > > for how to handle the logic for this...finding all numbers that are
    > > divisible by 4 and removing those divisible by 100 is easy. Its adding
    > > in that third condition which adds some of the removed numbers back
    > > into the "true" group that I am having trouble with...or maybe I am
    > > just not wrapping my mind around the problem well
    > > enough...suggestions?

    >
    > require 'date'
    > Date.leap?(year) # :)
    >
    > See Morton's implementation. Here, just for fun, is another:
    >
    > def leap?(year)
    > year % 4 == 0 unless (year % 100 == 0 unless year % 400 == 0)
    > end
    >
    > It returns nil/true rather than false/true, so it's a bit
    > non-slick. But I thought the semantics might be interesting.
    >


    You can fix that with the fun !! trick to convert nils back to false:
    def leap?(year)
    !!(year % 4 == 0 unless (year % 100 == 0 unless year % 400 == 0))
    end
    Wilson Bilkovich, Nov 7, 2006
    #5
  6. On Nov 7, 2006, at 12:15 PM, Shiloh Madsen wrote:

    > So, I'm trying to go through the Teach Yourself Programming book by
    > Pragmatic Press and I am encountering a few hurdles. The chapter I am
    > working on now is asking me to create a program which will ask for a
    > start and end year and then calculate all leap years in that range.
    > The logic behind leap years (for those who need a refresher) is all
    > years divisible by for are leap years EXCEPT those that are divisible
    > by 100 UNLESS they are also divisible by 400. I am somewhat at a loss
    > for how to handle the logic for this...finding all numbers that are
    > divisible by 4 and removing those divisible by 100 is easy. Its adding
    > in that third condition which adds some of the removed numbers back
    > into the "true" group that I am having trouble with...or maybe I am
    > just not wrapping my mind around the problem well
    > enough...suggestions?


    The key point of all the methods proposed in this thread is: deal
    with the years divisible by 400 first, the years divisible by 100
    second, and the years divisible by 4 last of all.

    Regards, Morton
    Morton Goldberg, Nov 7, 2006
    #6
  7. AH! i was looking at it wrong...startin from validating divisible by 4
    is the wrong direction...thank you all. I think I can make it work now
    (many of you posted rather more advanced ways to do it. I am trying to
    stick within the confines of the chapters ive done so far). Basically
    I needed the way to approach the problem. Thanks all!

    S.

    On 11/7/06, Morton Goldberg <> wrote:
    > On Nov 7, 2006, at 12:15 PM, Shiloh Madsen wrote:
    >
    > > So, I'm trying to go through the Teach Yourself Programming book by
    > > Pragmatic Press and I am encountering a few hurdles. The chapter I am
    > > working on now is asking me to create a program which will ask for a
    > > start and end year and then calculate all leap years in that range.
    > > The logic behind leap years (for those who need a refresher) is all
    > > years divisible by for are leap years EXCEPT those that are divisible
    > > by 100 UNLESS they are also divisible by 400. I am somewhat at a loss
    > > for how to handle the logic for this...finding all numbers that are
    > > divisible by 4 and removing those divisible by 100 is easy. Its adding
    > > in that third condition which adds some of the removed numbers back
    > > into the "true" group that I am having trouble with...or maybe I am
    > > just not wrapping my mind around the problem well
    > > enough...suggestions?

    >
    > The key point of all the methods proposed in this thread is: deal
    > with the years divisible by 400 first, the years divisible by 100
    > second, and the years divisible by 4 last of all.
    >
    > Regards, Morton
    >
    >
    Shiloh Madsen, Nov 7, 2006
    #7
  8. ----- Original Message -----
    From: <>
    To: "ruby-talk ML" <>
    Sent: Tuesday, November 07, 2006 3:33 PM
    Subject: Re: Help with a program to determin leap years.


    > Hi --
    >
    > On Wed, 8 Nov 2006, Shiloh Madsen wrote:
    >
    >> So, I'm trying to go through the Teach Yourself Programming book by
    >> Pragmatic Press and I am encountering a few hurdles. The chapter I am
    >> working on now is asking me to create a program which will ask for a
    >> start and end year and then calculate all leap years in that range.
    >> The logic behind leap years (for those who need a refresher) is all
    >> years divisible by for are leap years EXCEPT those that are divisible
    >> by 100 UNLESS they are also divisible by 400. I am somewhat at a loss
    >> for how to handle the logic for this...finding all numbers that are
    >> divisible by 4 and removing those divisible by 100 is easy. Its adding
    >> in that third condition which adds some of the removed numbers back
    >> into the "true" group that I am having trouble with...or maybe I am
    >> just not wrapping my mind around the problem well
    >> enough...suggestions?

    >
    > require 'date'
    > Date.leap?(year) # :)
    >
    > See Morton's implementation. Here, just for fun, is another:
    >
    > def leap?(year)
    > year % 4 == 0 unless (year % 100 == 0 unless year % 400 == 0)
    > end
    >
    > It returns nil/true rather than false/true, so it's a bit
    > non-slick. But I thought the semantics might be interesting.
    >
    >
    > David
    >
    > --
    > David A. Black |
    > Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    > DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    > [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    > [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
    >


    what about small optimization (just for fun);
    it works faster in most cases - non leap year happens more often:

    def leap?(y)
    (y%4).zero? && !(y%100).zero? || (y%400).zero?
    end

    sergey
    Sergey Volkov, Nov 8, 2006
    #8
  9. Shiloh Madsen

    Peña, Botp Guest

    [mailto:]:
    # year % 4 =3D=3D 0 unless (year % 100 =3D=3D 0 unless year % 400 =
    =3D=3D 0)

    heheh thanks, this one should go to my bag of tricks, a (first) example =
    of cascading unlesseses :)

    kind regards -botp
    Peña, Botp, Nov 8, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. feng
    Replies:
    0
    Views:
    316
  2. Brian Henry
    Replies:
    0
    Views:
    357
    Brian Henry
    Jul 28, 2005
  3. Roy G. Vervoort

    determin color of a specific hyperlink

    Roy G. Vervoort, Feb 2, 2004, in forum: HTML
    Replies:
    31
    Views:
    2,494
    Mark Parnell
    Feb 9, 2004
  4. crjunk
    Replies:
    1
    Views:
    392
    crjunk
    Sep 25, 2006
  5. Replies:
    4
    Views:
    547
    Lasse Reichstein Nielsen
    Jun 20, 2006
Loading...

Share This Page