I am a beginner on programming now reading books by Chris Pine: Learn
to Program.
On chapter 6, I got same assignment. I guess that is a classic one.
I have tried to use what I have learned so far: The loop.
My code is as below. i have run several test without finding any
problem.
Can you help to have a look and throw some light on any possible
improvement? Thanks.
···
____________________
puts 'starting year:'
s = gets.chop
puts 'ending year:'
e = gets.chop
if s.to_i > e.to_i
puts 'ending year should be bigger than staring year'
else
puts 'leap year between ' + s + ' and '+ e + ' as below:'
end
while s.to_i < e.to_i
while ( (s.to_i % 4 == 0 and s.to_i % 100 != 0) or (s.to_i % 100 ==
0 and s.to_i % 400 == 0 ))
puts s
s = s.to_i + 1
end
s = s.to_i + 1
end
puts 'all done'
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.
I am a beginner on programming now reading books by Chris Pine: Learn
to Program.
On chapter 6, I got same assignment. I guess that is a classic one.
I have tried to use what I have learned so far: The loop.
My code is as below. i have run several test without finding any
problem.
Can you help to have a look and throw some light on any possible
improvement? Thanks.
The fist thing I'd change is to remove all the #to_i's. You should convert to integer just once, namely after reading user input. If you use Integer() for the conversion, then you also get automatic error checking, i.e., if the user enters "foo" no calculations will be done but he will see an error message instead.
Next, I am not sure what you are trying to achieve. As far as I can see there is no condition on the "puts s" but your print statement seems to indicate that you are interested in leap years only. If you want to print leap years only then you need to somehow put a condition around that output statement.
Normally you would need just a single loop as far as I understand the problem and what you are trying to do. So you could get rid of one of them.
I would also move the leap year output code inside the if-else. The code will likely work the way it is as well because if s>e the body of the while loop will never be executed. But from a control flow point of view the code becomes clearer when you nest the "activity" (leap year calculation and output in this case) in the proper branch of the conditional statement.
____________________
puts 'starting year:'
s = gets.chop
puts 'ending year:'
e = gets.chop
if s.to_i > e.to_i
puts 'ending year should be bigger than staring year'
else
puts 'leap year between ' + s + ' and '+ e + ' as below:'
end
while s.to_i < e.to_i
while ( (s.to_i % 4 == 0 and s.to_i % 100 != 0) or (s.to_i % 100 ==
0 and s.to_i % 400 == 0 ))
puts s
s = s.to_i + 1
end
s = s.to_i + 1
end
puts 'all done'
On chapter 6, I got same assignment. I guess that is a classic one.
So here is a nonclassic solution to this classic problem
If s % 4 differs from 0 we start with a year that cannot be a leap year.
If this occurs add 4 - (s % 4) to skip to the next year that is a leap
year unless century rules happen to apply. Now only every fourth year
needs to be considered. A year that can be divided by four is a leap
year under the condition that it can be divided by 400 or cannot be
divided by 100.
puts 'starting year:'
s = gets.chomp.to_i
puts 'ending year:'
e = gets.chomp.to_i
if s > e
puts 'ending year should be bigger than staring year'
else
puts "leap year between #{s} and #{e} as below:"
end
s += 4 - (s % 4) if s % 4 != 0
while s <= e
puts s if (s % 400 == 0 or s % 100 != 0)
s += 4
end
puts 'all done'
I am a beginner on programming now reading books by Chris Pine: Learn
to Program.
On chapter 6, I got same assignment. I guess that is a classic one.
I have tried to use what I have learned so far: The loop.
My code is as below. i have run several test without finding any
problem.
Can you help to have a look and throw some light on any possible
improvement? Thanks.
I think it's easier to use Date#leap? for this question.
puts 'Starting year:'
start = gets.chop
puts 'Ending year:'
ending = gets.chop
if start.to_i > ending.to_i
puts 'Ending year should be bigger than staring year'
else
puts 'Leap year between ' + start + ' and '+ ending + ' as below:'
while start.to_i <= ending.to_i
if Date.new(start.to_i).leap?
puts start
end
start = start.to_i + 1
end
end
puts 'all done'
I have a little modification, I included the minutes in a year.
#!/usr/bin/ruby
puts "please enter starting year:"
STDOUT.flush
starting = gets
puts "please enter end_year:"
STDOUT.flush
end_year = gets
year = starting.to_i
while year <= end_year.to_i
leapyear =
if year % 400 == 0
true
elsif year % 100 == 0
false
else
year % 4 == 0
end
if leapyear: puts "#{year}-> This year is a leapyear and #{366*60*24}
minutes"
else
puts "#{year}-> Not a leapyear #{365*60*24} minutes"
end
year += 1
end
The problem comes from a beginner´s tutorial. Using methods like .leap?
of course work just fine. However, a beginner is not familiar with them.
This a way to do it using the tools showed in the same tutorial up to
that level:
puts "Give me the two years"
year1 = gets.chomp.to_i
year2 = gets.chomp.to_i
puts "This is the list of years:"
if year1 > year2
puts "The second year has to be bigger than the first"
else
while (year1 <= year2)
if
(((year1 % 4 == 0) and (year1 %100 !=0)) or (year1 % 400 == 0))
puts year1.to_s
end
(year1 = year1.to_i + 1)
end
puts "Finished"
end
puts 'Begin year:'
beginyear = gets.chomp
puts 'End year:'
endyear = gets.chomp
puts 'The leap years between ' + beginyear + ' and ' + endyear + ':'
beginyear = beginyear.to_i
endyear = endyear.to_i
if endyear < beginyear
puts 'Note: Begin year < End year'
else
while (beginyear <= endyear)
if
(((beginyear % 4 == 0) and (beginyear %100 !=0)) or
(beginyear % 400 == 0))
puts beginyear.to_s
Thanks to all!
I have a lot to learn and it is fun!
···
On Sep 3, 12:24 am, Hu Yoi <kgo_...@hotmail.com> wrote:
HB wrote:
> Hi, All,
> I am a beginner on programming now reading books by Chris Pine: Learn
> to Program.
> On chapter 6, I got same assignment. I guess that is a classic one.
> I have tried to use what I have learned so far: The loop.
> My code is as below. i have run several test without finding any
> problem.
> Can you help to have a look and throw some light on any possible
> improvement? Thanks.
I think it's easier to use Date#leap? for this question.
puts 'Starting year:'
start = gets.chop
puts 'Ending year:'
ending = gets.chop
if start.to_i > ending.to_i
puts 'Ending year should be bigger than staring year'
else
puts 'Leap year between ' + start + ' and '+ ending + ' as below:'
while start.to_i <= ending.to_i
if Date.new(start.to_i).leap?
puts start
end
start = start.to_i + 1
end
end
puts 'all done'
That is what I am talking about... I just ran it from my e-mail... much
better for the assignment. You are greatly improving, by the way!
Warmest Regards,
Victor H. Goff III
Voice (218) 206-2470
···
On Thu, Oct 16, 2008 at 2:14 AM, Bernie Loriaga <stanbench@gmail.com> wrote:
I have a little modification, I included the minutes in a year.
#!/usr/bin/ruby
puts "please enter starting year:"
STDOUT.flush
starting = gets
puts "please enter end_year:"
STDOUT.flush
end_year = gets
year = starting.to_i
while year <= end_year.to_i
leapyear =
if year % 400 == 0
true
elsif year % 100 == 0
false
else
year % 4 == 0
end
if leapyear: puts "#{year}-> This year is a leapyear and #{366*60*24}
minutes"
else
puts "#{year}-> Not a leapyear #{365*60*24} minutes"
end
year += 1
end
puts "Leap years between #{beginyear} and #{endyear}:"
for y in beginyear..endyear
puts y if Date.new(y, 1, 1).leap?
end
Cheers
robert
···
On Tue, Dec 11, 2012 at 9:50 AM, Hieu Le <lists@ruby-forum.com> wrote:
This is my function:
puts 'Begin year:'
beginyear = gets.chomp
puts 'End year:'
endyear = gets.chomp
puts 'The leap years between ' + beginyear + ' and ' + endyear + ':'
beginyear = beginyear.to_i
endyear = endyear.to_i
if endyear < beginyear
puts 'Note: Begin year < End year'
else
while (beginyear <= endyear)
if
(((beginyear % 4 == 0) and (beginyear %100 !=0)) or
(beginyear % 400 == 0))
puts beginyear.to_s
Thanks Vic,
I just copy the code and modify it a little,is it mean that I'm
improving?
Right now,I'm reading some ruby book.I want to improve my ruby skill.
puts 'Begin year:'
beginyear = gets.chomp
puts 'End year:'
endyear = gets.chomp
puts 'The leap years between ' + beginyear + ' and ' + endyear + ':'
beginyear = beginyear.to_i
endyear = endyear.to_i
if endyear < beginyear
puts 'Note: Begin year < End year'
else
while (beginyear <= endyear)
if
(((beginyear % 4 == 0) and (beginyear %100 !=0)) or
(beginyear % 400 == 0))
puts beginyear.to_s
end
(beginyear = beginyear.to_i + 1)
end
end
Here's mine
require 'date'
puts 'Begin year:'
beginyear = Integer(gets)
puts 'End year:'
endyear = Integer(gets)
One minor tweak:
beginyear, endyear = endyear, beginyear if endyear < beginyear
···
On Tue, Dec 11, 2012 at 6:22 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Tue, Dec 11, 2012 at 9:50 AM, Hieu Le <lists@ruby-forum.com> wrote:
puts "Leap years between #{beginyear} and #{endyear}:"
for y in beginyear..endyear
puts y if Date.new(y, 1, 1).leap?
end
Hi, I'm also a newbie trying the Leap Year problem using while loops..
Can anyone advice why the following code doesn't work? Much thanks!
puts "Enter starting year:"
starting_year = gets.chomp.to_i
puts "Enter ending year:"
ending_year = gets.chomp.to_i
while true
year = starting_year
if (year%4==0)&& (year%100!=0)
puts year.to_s + ' is a Leap Year'
end
year = year +1
break if year >= ending_year
end
You are resetting year to starting_year every time the loop runs. Set that variable outside of the loop. Also, every 400 years, there is a leap year.
···
Date: Wed, 23 Jan 2013 13:32:20 +0900
From: lists@ruby-forum.com
Subject: Re: Leap year programing
To: ruby-talk@ruby-lang.org
Hi, I'm also a newbie trying the Leap Year problem using while loops..
Can anyone advice why the following code doesn't work? Much thanks!
puts "Enter starting year:"
starting_year = gets.chomp.to_i
puts "Enter ending year:"
ending_year = gets.chomp.to_i
while true
year = starting_year
if (year%4==0)&& (year%100!=0)
puts year.to_s + ' is a Leap Year'
end
year = year +1
break if year >= ending_year
end
Hi, I'm also a newbie trying the Leap Year problem using while loops..
Can anyone advice why the following code doesn't work? Much thanks!
puts "Enter starting year:"
starting_year = gets.chomp.to_i
puts "Enter ending year:"
ending_year = gets.chomp.to_i
while true
year = starting_year
if (year%4==0)&& (year%100!=0)
puts year.to_s + ' is a Leap Year'
end
year = year +1
break if year >= ending_year
end
You are resetting the variable "year" to "starting_year" every time to
loop runs. Set the variable "year" outside of the while loop. Also,
there is a leap year every 400 years.
puts "Enter starting year:"
starting_year = gets.chomp.to_i
puts "Enter ending year:"
ending_year = gets.chomp.to_i
year = starting_year
while true
if year%4==0
if year%100!=0 || year%400 ==0
puts year.to_s + ' is a Leap Year'
end
end
year = year +1
break if year >= ending_year
end
The new code here works.
---------------------------------------
puts "Enter starting year:"
starting_year = gets.chomp.to_i
puts "Enter ending year:"
ending_year = gets.chomp.to_i
year = starting_year
while true
if year%4==0
if year%100!=0 || year%400 ==0
puts year.to_s + ' is a Leap Year'
end
end
year = year +1
break if year >= ending_year
end
You can also use date standard library for detecting leap year like this:
starting_year.upto(ending_year) do |year|
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
puts "#{year} is a leap year"
end
end
which saves 3 lines of code and some possibilities for typos/bugs.
BTW, for an infinite loop there exists `loop do ... end'.
···
Am 24.01.2013 02:01, schrieb Kristine Lai:
Thanks for the pointers Eduardo!
The new code here works.
---------------------------------------
puts "Enter starting year:"
starting_year = gets.chomp.to_i
puts "Enter ending year:"
ending_year = gets.chomp.to_i
year = starting_year
while true
if year%4==0
if year%100!=0 || year%400 ==0
puts year.to_s + ' is a Leap Year'
end
end
year = year +1
break if year >= ending_year
end