Day of the Programmer : HackerRank Solution in Python

Shounak Lohokare
3 min readDec 28, 2020

Solution in Python 3

Link:- DOFTP (github.com)

def dayOfProgrammer(year):
if year < 1700 or year > 2700:
return

elif year==1918:
return '26.09.1918'

elif year < 1918:
if year%4==0:
return '12.09.'+str(year)

else:
return '13.09.'+str(year)

elif year > 1918:
if year%400==0 or year%4==0 and year%100!=0:
return '12.09.'+str(year)

else:
return '13.09.'+str(year)

Explanation

First, we will check the constraint, if the year is in the inclusive range of 1700 and 2700.

if year is less than 1700 or it is greater than 2700 then it doesn’t go along with our constraint and we return from the function.

def dayOfProgrammer(year):
if year < 1700 or year > 2700:
return

Next, we check if the year equals to 1918, that is the year in which Russia transitioned from the Julian calendar system to the Gregorian calendar system.

In 1918, the next day after 31st January was 14th February which means 14th February was the 32nd day in year 1918 (for Russia). Since, 1918 is a leap year (according to Gregorian calendar system), we will add 15 days of February from 14th February. 32+15 = 47. Now, we will add 31, 30, 31, 30, 31 and 31 days of March, April, May, June, July, and August respectively, which sums up to 231 days. Which means 31st August was the 231st day of 1918 in Russia. Hence, 26th of September was the 256th day of the year 1918 in Russia.

Hence, if year equals to 1918 we return ‘26.09.1918' adhering to dd.mm.yyyy format asked by HackerRank.

elif year==1918:
return '26.09.1918'

In the Julian as well as the Gregorian calendar system the 256th day falls on 12th of September on a leap year and on 13th of September on a non-leap year.

Now, we check whether the year is less than 1918. If the year is less than 1918 then Russia follows the Julian calendar system. We use an if block to check that.

if year < 1918:

In the Julian calendar system, if the year is divisible by 4 then it’s a leap year else it’s a non-leap year.

In the above if block we check if the year is divisible by 4 then it’s a leap year and we return ‘12.09.’+str(year). else, we return ‘13.09.’+str(year).

elif year < 1918:
if year%4==0:
return '12.09.'+str(year)

else:
return '13.09.'+str(year)

Now, we check whether the year is greater than 1918. If the year is greater than 1918 then Russia follows the Gregorian calendar system. We use an if block to check that.

if year>1918:

In the Gregorian calendar system, the year is a leap year if it is:-

  • Divisible by 400.
  • Divisible by 4 and not divisible by 100.
elif year > 1918:
if year%400==0 or year%4==0 and year%100!=0:
return '12.09.'+str(year)
else:
return '13.09.'+str(year)

In the above if block we check if year is divisible by 400 or divisible 4 but not divisible by 100. If it is then it’s a leap year and we return ‘12.09.’+str(year). else it’s a non-leap year and hence we return ‘13.09.’+str(year).

--

--

Shounak Lohokare

pretending to teach is one of the best way to learn.