loops and branching

I

ishamid

[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
 
C

Christoffer Sawicki

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
 
I

ishamid

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 ;-)

Best and Thnx
Idris
 
M

M. Edward (Ed) Borasky

ishamid said:
[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!
 
I

ishamid

Actually, that would make you a day early ;-)

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

Best
Idris
 
M

M. Edward (Ed) Borasky

ishamid said:
Actually, that would make you a day early ;-)
Oh, yeah ... but that still doesn't explain why Daylight Savings Time
pisses off the cows.
 
E

El Gato

M. Edward (Ed) Borasky said:
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 :)
 
P

Pete Yandell

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
 
D

dblack

Hi --

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 }

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

puts leap_years

which will call puts on each element in the array.


David

--
David A. Black | (e-mail address removed)
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
 
C

Christoffer Sawicki

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

Cheers,
 
N

NigamaX

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top