![]() This was an exercise of mine to see if I could do so, and nothing more.įurther, be aware that ZeroDivisionErrors are a consequence of the input year equaling 0, and must be accounted for.įor example, a VERY basic timeit comparison of 1000 executions shows that, when compared against an equivalent codeblock utilizing simple if-statements and the modulus operator, this one-liner is roughly 5 times slower than its if-block equivalent. Please note: this code is horribly inefficient, incredibly unreadable, and a detriment to any code attempting to follow proper practices. If the year is a leap year this will result in True (or 1, if used in C), otherwise it will return False (or 0, if used in C). This value is compared against 0 with the less-than operator. This value is added to (y % 4), which will only be equal to 0 if the year is a leap year after accounting for the edge-cases.įinally, 1 is subtracted from this remaining value, resulting in -1 if the year is a leap year, and either 0, 1, or 2 if it is not. If it is divisible by both 100 and 400, it will result in 0. If the year is not divisible by 100, this will always equal 0, otherwise if it is divisible by 100, but not by 400, it will result in 1, 2, or 3. ((y % 400) / 100))) >Next, the year is divided by 400 (and subsequently 100, to return 1, 2, or 3 if it is not.Īre then multiplied together. If the year is evenly divisible by 100, this will result in a value of 1, otherwise it will result in a value of 0. (int((y - (y % 100)) / y) >It then accounts for those years divisible by 100. In this case the result is computed in the function add2_New() and no print statement, and returned to the caller who then prints it in turn. ![]() Incidentally, if I had just called the add2() function simply with (note, no print statement): add2() The None from the print statement when I call the function add2() which does not have a return statement, causing the None to be printed. The first line comes form the print statement inside of add2(). Return n1 + n2 # returns the result to caller Print 'the result is:', n1 + n2 # prints but uses no *return* statement Some short examples regarding the above: def add2(n1, n2): Note: This does not address any possible problems you have with your leap year computation, but ANSWERS YOUR SPECIFIC QUESTION as to why you are getting None as a result of your function call in conjunction with your print. Or modify your function to return a value (by using the return statement), which then would be printed by your print statement. So either just call your function like this: leapyr(1900) Which is accurate to about one day in 3000 years.Your function doesn't return anything, so that's why when you use it with the print statement you get None. The average length of a year in the Gregorian Calendar is: So, 1600, 20 are leap years, but 19 are not. A year is a leap year if it is evenly divisible by four (4), unless it is a century year (eg. A leap year is a year containing one additional day and has 366 days. 2000 is divisible by 400, whereas 1900 is not. ![]() 1600, 1700, 1800) which are exactly divisible by 400 are leap years. Please enter the year number to calculate if it is a leap year or not. ![]() In the Gregorian calander, only century years (e.g. To stem the drift even further, Pope Gregory XIII modified the definition of leap years in 1582. The average length of the year is thenĬloser to the tropical year, but still with a drift of about 8 days every 1000 years. According to the Julian Calendar, a standard year has 365 days, but every fourth year there is a leap year of 366 days. In 46 BC, the Roman astronomer Sisogenes proposed a modification that would bring calendar dates and observations back into step. With a standard calendar year lasting for 365 days, leap years of 366 days are used in both the Julian Calendar and the Gregorian Calendar in order to better approximate the true length of the tropical year. Over time, the dates of the solstices, equinoxes and the seasons drift out of step with the expected calendar dates. Unfortunately, the orbital period of the Earth is not an exact multiple of days: one tropical year = 365.24219 days. In most calendar systems, the length of the year is given in multiples of 24-hour days. ![]()
0 Comments
Leave a Reply. |