[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

loops and branching

ishamid

11/30/2006 9:47:00 PM

[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

10 Answers

Christoffer Sawicki

11/30/2006 10:11:00 PM

0

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

ishamid

12/1/2006 1:13:00 AM

0

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

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

M. Edward (Ed) Borasky

12/1/2006 3:00:00 AM

0

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

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


ishamid

12/2/2006 2:50:00 AM

0



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

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

Best
Idris

M. Edward (Ed) Borasky

12/2/2006 3:38:00 AM

0

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

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


wmwilson01

12/2/2006 3:53:00 AM

0

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

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

Pete Yandell

12/2/2006 11:49:00 PM

0


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

dblack

12/3/2006 12:07:00 AM

0

Christoffer Sawicki

12/4/2006 10:49:00 PM

0

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 }

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

Cheers,

--
Christoffer Sawicki
http://...

NigamaX

12/15/2006 7:04:00 PM

0



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

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