SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict silentcollision's Avatar
    Join Date
    Jun 2006
    Location
    New Zealand
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Newbie!] Ruby leap year problem.

    I'm trying to learn ruby, and everything is going great. I am enjoying it as a language, and I find using the command prompt far easier than having to upload every document, like I did with PHP. I'm using the tutorial on Pine.fm, and I've reached Chapter 6 without a problem. Chapter 6 is on flow control, and one of the ending exercises is:

    Leap Years. 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). (Yes, it's all pretty confusing, but not as confusing has having July in the middle of the winter, which is what would eventually happen.)
    And this is what I've done:

    Code:
    puts 'This program will determine what leap years are between two specific dates that you assign.'
    puts ''
    puts 'What would you like your starting year to be?'
    start = gets.chomp
    puts 'This program will start at ' + start + '.'
    puts ''
    puts 'What would you like your ending year to be?'
    close = gets.chomp
    puts 'This program will end at ' + close + '.'
    # Checks if the closing date is before the starting date
    if close < start
    	puts 'Your closing date is before your start date. Please try again'
    	else
    		puts 'Calculating...'
    	end
    # Is the starting date a leap year?
    if (start.to_i/4 or start.to_i/400)
    	puts 'Your starting date ' + start + ' is a leap year!'
    end
    To finish it, I'm guessing I need to loop with a while statement and add 4 to the first leap year inside the range, until it reaches the end date. Can anyone help? I've tried a few combinations, and read through looping again, but I can't quite grasp it.

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this would work:

    Code:
    puts 'This program will determine what leap years are between two specific dates that you assign.'
    puts ''
    puts 'What would you like your starting year to be?'
    start_year = gets.chomp.to_i
    puts ''
    puts 'What would you like your ending year to be?'
    end_year = gets.chomp.to_i
    puts "Leap years between #{start_year} and #{end_year}"
    
    if start_year > end_year
      puts 'Your closing date is before your start date. Please try again'
      return
    end
    
    leap_years = []
    start_year.upto(end_year) do |year|
      if (year % 4 == 0)
        leap_years << year unless (year % 100 == 0) and (year % 400 != 0)
      end
    end
    
    puts leap_years.join(', ')
    Some notes:
    - you can use the Integer#upto method to step through each year (see http://www.ruby-doc.org/core/ for API docs)
    - remember to cast the user input into integers (and add error-handling, which I have left out in the code above)
    - the % operator returns the remainder... E.g. 10 % 3 gives you 1.

  3. #3
    SitePoint Addict silentcollision's Avatar
    Join Date
    Jun 2006
    Location
    New Zealand
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works perfectly, and I think I understand it. Thank you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •