Loops and branching

[progrmming novice]

Hi,

The following solution appears to work, but I would like to make it
more efficient and "Rubyish" (still trying to figure out what that
means...).

PROBLEM (from Chris Pine -- Learn to Program): "Write a program which
will ask for a starting year and an ending year, and then puts all of
the leap years between them (and including them, if they are also leap
years). Leap years are years divisible by four (like 1984 and 2004).
However, years divisible by 100 are not leap years (such as 1800 and
1900) unless they are divisible by 400 (like 1600 and 2000, which were
in fact leap years)."

MY SOLUTION (tested for the different scenarios):

···

===============
puts 'Type initial year:'
leapi = gets.chomp.to_i
puts 'Type final year:'
leapf = gets.chomp.to_i

puts ''

if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
  puts leapi
else
end

while (leapi + (4 - leapi%4)) <= leapf
  leapi = (leapi + (4 - leapi%4))
  if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
    puts leapi
  else
  end
end

Is there a nicer way to do this? I can't but feel that the first
conditional is superflous and that I should be able to do it all within
a single "while" loop.

Best
Idris

if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
        puts leapi
else
end

while (leapi + (4 - leapi%4)) <= leapf
        leapi = (leapi + (4 - leapi%4))
        if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
                puts leapi
        else
        end
end

(leapi..leapf).select do |x|
  (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
end

···

--
Christoffer Sawicki
http://vemod.net/

ishamid wrote:

[progrmming novice]

Hi,

The following solution appears to work, but I would like to make it
more efficient and "Rubyish" (still trying to figure out what that
means...).

PROBLEM (from Chris Pine -- Learn to Program): "Write a program which
will ask for a starting year and an ending year, and then puts all of
the leap years between them (and including them, if they are also leap
years). Leap years are years divisible by four (like 1984 and 2004).
However, years divisible by 100 are not leap years (such as 1800 and
1900) unless they are divisible by 400 (like 1600 and 2000, which were
in fact leap years)."
  

2000 was a Leap Year? No wonder I'm a day late!

···

--
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)
http://borasky-research.blogspot.com/

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.

I am only as far as this problem in the book, and this was my solution
(after I saw a few ways to clean it up after looking at yours :slight_smile: ).

···

On Nov 30, 4:46 pm, "ishamid" <isha...@colostate.edu> wrote:

[progrmming novice]

Is there a nicer way to do this? I can't but feel that the first
conditional is superflous and that I should be able to do it all within
a single "while" loop.

Best
Idris

---------------------------------------
puts 'Start year:'
ly = gets.chomp.to_i
puts 'End year:'
lye = gets.chomp.to_i
puts ''
puts 'Leap years between and including ' + ly.to_s + ' and ' + lye.to_s
+ ':'
puts '-----------------------------------------------'
puts ''

while ly <= lye
  if ly%4 == 0 && (ly%100 != 0 || ly%400 == 0)
    puts ly
    ly = (ly + 1).to_i
  else
    ly = (ly + 1).to_i
  end
end
-----------------------------------------

Hope that helps. I was looking around for help originally and all the
postings I had seen used coding that was more advanced than this
chapter of the book, so they weren't much help to check/refine my
answer. This is a great book so far, tho, I'm really enjoying it.

Winter

Thank you, but the following does not work properly:

···

================
puts 'Type initial year:'
leapi = gets.chomp.to_i
puts 'Type final year:'
leapf = gets.chomp.to_i

puts ''

while (leapi + (4 - leapi%4)) <= leapf
  leapi = (leapi + (4 - leapi%4))
  (leapi..leapf).select do |x|
    (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
    puts leapi
  end
end

Please advise :wink:

Best and Thnx
Idris

On Nov 30, 3:11 pm, "Christoffer Sawicki" <christoffer.sawi...@gmail.com> wrote:

> if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
> puts leapi
> else
> end

> while (leapi + (4 - leapi%4)) <= leapf
> leapi = (leapi + (4 - leapi%4))
> if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
> puts leapi
> else
> end
> end(leapi..leapf).select do |x|
  (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
end

--
Christoffer Sawickihttp://vemod.net/

Actually, that would make you a day early :wink:

Still awaiting advice on the original or on getting Chris' suggestion
to work...

Best
Idris

···

On Nov 30, 7:59 pm, "M. Edward (Ed) Borasky" <z...@cesmail.net> wrote:

> 1900) unless they are divisible by 400 (like 1600 and 2000, which were
> in fact leap years)."2000 was a Leap Year? No wonder I'm a day late!

puts 'Enter a starting year'
num1 = gets.chomp.to_i
puts 'Enter an ending year'
num2 = gets.chomp.to_i
puts 'The leap years between ' + num1.to_s + ' and ' + num2.to_s + ' are: '
(num1..num2).select do |x|
  if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
    puts x
  end
end

···

On 11/30/06, ishamid <ishamid@colostate.edu> wrote:

Thank you, but the following does not work properly:

================
puts 'Type initial year:'
leapi = gets.chomp.to_i
puts 'Type final year:'
leapf = gets.chomp.to_i

puts ''

while (leapi + (4 - leapi%4)) <= leapf
       leapi = (leapi + (4 - leapi%4))
       (leapi..leapf).select do |x|
         (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
         puts leapi
       end
end

Please advise :wink:

Best and Thnx
Idris

On Nov 30, 3:11 pm, "Christoffer Sawicki" > <christoffer.sawi...@gmail.com> wrote:
> > if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
> > puts leapi
> > else
> > end
>
> > while (leapi + (4 - leapi%4)) <= leapf
> > leapi = (leapi + (4 - leapi%4))
> > if leapi%4 == 0 && (leapi%400 == 0 || leapi%100 != 0)
> > puts leapi
> > else
> > end
> > end(leapi..leapf).select do |x|
> (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
> end
>
> --
> Christoffer Sawickihttp://vemod.net/

ishamid wrote:

···

On Nov 30, 7:59 pm, "M. Edward (Ed) Borasky" <z...@cesmail.net> wrote:
  

1900) unless they are divisible by 400 (like 1600 and 2000, which were
in fact leap years)."2000 was a Leap Year? No wonder I'm a day late!
      
Actually, that would make you a day early :wink:
  

Oh, yeah ... but that still doesn't explain why Daylight Savings Time pisses off the cows.

--
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)
http://borasky-research.blogspot.com/

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.

M. Edward (Ed) Borasky wrote:

Oh, yeah ... but that still doesn't explain why Daylight Savings Time
pisses off the cows.

You try waking up to someone's hands on your teets and you haven't even
had your first cup of coffee and you'll know :slight_smile:

···

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

puts 'Enter a starting year'
num1 = gets.chomp.to_i
puts 'Enter an ending year'
num2 = gets.chomp.to_i
puts 'The leap years between ' + num1.to_s + ' and ' + num2.to_s + ' are: '
(num1..num2).select do |x|
if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
   puts x
end
end

Errr...that's not really a good use of select. You probably meant:

(num1..num2).each do |x|
   if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
     puts x
   end
end

Although I prefer separating the calculation and the output:

leap_years = (num1..num2).select {|x| (x % 4 == 0) && (x % 100 != 0)

(x % 400 == 0) }

leay_years.each do {|x| puts x }

Pete Yandell

···

On 02/12/2006, at 3:02 AM, Jason Mayer wrote:

Hi --

···

On Sun, 3 Dec 2006, Pete Yandell wrote:

On 02/12/2006, at 3:02 AM, Jason Mayer wrote:

puts 'Enter a starting year'
num1 = gets.chomp.to_i
puts 'Enter an ending year'
num2 = gets.chomp.to_i
puts 'The leap years between ' + num1.to_s + ' and ' + num2.to_s + ' are: '
(num1..num2).select do |x|
if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
  puts x
end

Errr...that's not really a good use of select. You probably meant:

(num1..num2).each do |x|
if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
  puts x
end

Although I prefer separating the calculation and the output:

leap_years = (num1..num2).select {|x| (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0) }
leay_years.each do {|x| puts x }

s/leay/leap/ :slight_smile: Also you can just do:

   puts leap_years

which will call puts on each element in the array.

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

I'm a bit late, but yes, that's how you would use my solution. :slight_smile:

Cheers,

···

On 12/3/06, Pete Yandell <pete.yandell@gmail.com> wrote:

Although I prefer separating the calculation and the output:

leap_years = (num1..num2).select {|x| (x % 4 == 0) && (x % 100 != 0)
>> (x % 400 == 0) }
leay_years.each do {|x| puts x }

--
Christoffer Sawicki
http://vemod.net/