Skip to main content

Command Palette

Search for a command to run...

Day of the Programmer Algorithm💻

Published
6 min read
Day of the Programmer Algorithm💻
U

Writing about things I learn helps me understand better, and I hope reading them will help you too! 🙂

In this article, I'll be describing how I solved the Day of the Programmer Challenge on HackerRank using Python 3.

This challenge is part of the Implementation challenges in the Algorithm section on HackerRank.

If this is your first time reading my data structures and algorithms article, please consider reading more in my Algos in Plain English series.😃

Problem 🎲

Marie invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of the Programmer (the 256th day of the year) during a year in the inclusive range from 1700 to 2700.

From 1700 to 1917, Russia's official calendar was the Julian calendar; since 1919 they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918, when the next day after January 31st was February 14th. This means that in 1918, February 14th was the 32nd day of the year in Russia.

In both calendar systems, February is the only month with a variable amount of days; it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4; in the Gregorian calendar, leap years are either of the following:

Divisible by 400. Divisible by 4 and not divisible by 100. Given a year, y, find the date of the 256th day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

For example, the given year = 1984. 1984 is divisible by 4, so it is a leap year. The 256th day of a leap year after 1918 is September 12, so the answer is 12.09.1984.

Output Format

Print the full date of Day of the Programmer during year y in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

Samle Input 0:

image.png

Samle Output 0:

image.png

Explanation 0:

In the year y = 2017, January has 31 days, February has 28 days, March has 31 days, April has 30 days, May has 31 days, June has 30 days, July has 31 days, and August has 31 days. When we sum the total number of days in the first eight months, we get 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 = 243. Day of the Programmer is the 256th day, so then calculate 256 - 243 = 13 to determine that it falls on day 13 of the 9th month (September). We then print the full date in the specified format, which is 13.09.2017.

I'll get right into the algorithm. Please feel free to read the full question with more sample cases on HackerRank.

Side Note:

This question was different from other algo problems we've looked at in the past. For this question, we had to pay special attention to the constraints normally given with algorithms in general. Algorithms are designed to solve specific problems with special constraints. Keeping these constraints in mind, we made certain assumptions that simplified the code and only focused on test cases within these constraints.

  • The constraints for this problem are specified as any year in the inclusive range from 1700 to 2700.

Solution 🔎

In plain English, we were told that the Russians back in the day had one calendar system, and they changed it in 1918 to another calendar system. Now each of these calendar systems had a different way of checking for a leap year. We are asked to find the date when the Day of the Programmer (the 256th day of the year) occurs in any given year within the constraints specified earlier.

BradyBunchJanBradyGIF.gif

Hope this was clear😅. Now let's code! 🤓

Step 1️⃣: State all assumptions:

I mentioned earlier in the Side Note that the constraints determined the assumptions we used, which in turn affects our code scope. These assumptions are:

  1. The 256th day of the year falls in September for both calendar types.
  2. The number of days for all months (except February) is fixed.

With these assumptions, we can go ahead to solve the problem.

Step 2️⃣: Create variables to track our conditional logic 🛤️:

We'll create two variables:

  • febLeapYearDays: Integer representing the number of days in February in a leap year (set to 29).
  • month: Integer representing September which the 256th day falls in (set to 9).
  • monthDaystoAugust: A list of integers representing the number of days in months January to September.
      febLeapYearDays = 29
      month = 9
      monthDaystoAugust = [31, 28, 31, 30, 31, 30, 31, 31]
    

Step 3️⃣: Developing the conditional logic:

  1. We'll use if-else statements to determine three conditions.
  2. The number of days in February will change depending on these three conditions:
  • If the current year existed during the Julian calendar system,
  • If the current year existed during the Gregorian calendar system,
  • If the current year was the year in which the calendar system changed (i.e., 1918).
    if year <= 1917 :        # julian calendar system                                   
        if year % 4 == 0 :
            monthDaystoAugust[1] = febLeapYearDays        

    elif year >= 1919 :        # gregorian calendar system
        if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) :
            monthDaystoAugust[1] = febLeapYearDays

    else:
        monthDaystoAugust[1] = 15        # 1918 is not a leap year

In the final else block, 1918 is not a leap year, hence no extra checks are needed. We set the number of days in February to 15 because we were told that in 1918, the next day after January 31st was February 14th. Meaning February 1918 had a total number of 15 days.

Step 4️⃣: Return the date in the required format 📅:

Recall that we need two-digit string characters for the day and month, and also a string representation of the year, all separated by ..

  1. To get the day in September, we simply subtract the sum of all months up until September from 256.
  2. We use .zfill() and cast the integers to return two-digit strings days and months.
  3. We also cast the years to strings.
  4. Finally we interpolate the three strings to get the required output format.
    _day = str( 256 - sum(monthDaystoAugust) ).zfill(2)
    _year = str(year).zfill(2)
    _month = str(month)

    return _day + '.' + _month + '.' + _year

Step 5️⃣: Putting it all together:

Full code:

def dayOfProgrammer(year):
    # Write your code here
    febLeapYearDays = 29
    month = 9
    monthDaystoAugust = [31, 28, 31, 30, 31, 30, 31, 31]

    if year <= 1917 :
        if year % 4 == 0 :
            monthDaystoAugust[1] = febLeapYearDays


    elif year >= 1919 :
        if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) :
            monthDaystoAugust[1] = febLeapYearDays

    else:
        monthDaystoAugust[1] = 15

    _day = str( 256 - sum(monthDaystoAugust) ).zfill(2)
    _year = str(year).zfill(2)
    _month = str(month)

    return _day + '.' + _month + '.' + _year


dayOfProgrammer(2016)
>>"12.09.2016"

Hope this article helped with understanding this challenge. Happy Coding!👨🏽‍💻

Buy me a coffee.png