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 P. – 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

    end

end

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

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 S.”

On 11/30/06, ishamid [email protected] wrote:

while (leapi + (4 - leapi%4)) <= leapf
Christoffer S.http://vemod.net/

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 Nov 30, 7:59 pm, “M. Edward (Ed) Borasky” [email protected] wrote:

  1. 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:

Still awaiting advice on the original or on getting Chris’ suggestion
to work…

Best
Idris

ishamid wrote:

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.

ishamid wrote:

On Nov 30, 7:59 pm, “M. Edward (Ed) Borasky” [email protected] wrote:

  1. 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:

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

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 Y.

On 12/3/06, Pete Y. [email protected] 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 }

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

Cheers,

Hi –

On Sun, 3 Dec 2006, Pete Y. wrote:

if (x % 4 == 0) && (x % 100 != 0) || (x % 400 == 0)
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

On Nov 30, 4:46 pm, “ishamid” [email protected] 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

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: ).


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