Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # Find out days count in a year

• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 155
Comment on it

### Description:

A variation of determining leap years, assuming only integers are used and years can be negative and positive.

Write a function which will return the days in the year and the year entered in a string. For example, 2000, entered as an integer, will return as a string 2000 has 366 days

There are a few assumptions we will accept the year 0, even though there is no year 0 in the Gregorian Calendar.

Also, the basic rule for validating a leap year are as follows

Most years that can be divided evenly by 4 are leap years.

Exception: Century years are NOT leap years UNLESS they can be evenly divided by 400.

So the years 0, -64 and 2016 will return 366 days. Whilst 1974, -10 and 666 will return 365 days.

### Test Case:

Test.describe("Basic tests") do
Test.assert_equals(year_days(0), '0 has 366 days')
Test.assert_equals(year_days(-64), '-64 has 366 days')
Test.assert_equals(year_days(2016), '2016 has 366 days')
Test.assert_equals(year_days(1974), '1974 has 365 days')
Test.assert_equals(year_days(-10), '-10 has 365 days')
Test.assert_equals(year_days(666), '666 has 365 days')
Test.assert_equals(year_days(1857), '1857 has 365 days')
Test.assert_equals(year_days(2000), '2000 has 366 days')
Test.assert_equals(year_days(-300), '-300 has 365 days')
Test.assert_equals(year_days(-1), '-1 has 365 days')
end

### Solution :

``````def year_days(year)
if year % 4 == 0
if year % 100 == 0 && year % 400 != 0
num_of_days = 365
else
num_of_days = 366
end
else
num_of_days = 365
end
"#{year} has #{num_of_days} days"
end``````

It gets better with a single line solution

``````def year_days(year)
Date.leap?(year) ? "#{year} has 366 days" : "#{year} has 365 days"
end``````

## 0 Comment(s)

Sign In
OR
OR
Register

Sign up using

OR
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Business
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Use Online Project Management Tools for Free
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Business
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Business
• Record & Share

### Desktop Recording Tool

• All at 0 Cost ....
• Online tool for Recording Desktop Activity and Audio
• Store and Share from the Cloud
• Creating Software Product Demos
• Creating video tutorials
• Recording and Reporting Software and Project issues as videos
• Ask Tech Query
• Post Blogs