HackerRank Day of the Programmer Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Day of the Programmer Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Day of the Programmer Solution
HackerRank Day of the Programmer Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Day of the Programmer Solution

Task

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

Function Description

Complete the dayOfProgrammer function in the editor below. It should return a string representing the date of the 256th day of the year given.

dayOfProgrammer has the following parameter(s):

  • year: an integer

Input Format

A single integer denoting year y.

Sample Input 0

2017

Sample Output 0

13.09.2017

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.

Sample Input 1

2016

Sample Output 1

12.09.2016

Explanation 1

Year y = 2016 is a leap year, so February has 29 days but all the other months have the same number of days as in 2017. When we sum the total number of days in the first eight months, we get 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 = 244. Day of the Programmer is the 256th day, so then calculate 256 244 = 12 to determine that it falls on day 12 of the 9th month (September). We then print the full date in the specified format, which is 12.09.2016.

Sample Input 2

1800

Sample Output 2

12.09.1800

Explanation 2

Since 1800 is leap year as per Julian calendar. Day lies on 12 September.

HackerRank Day of the Programmer Solution

Day of the Programmer Solution in C

#include <stdio.h>
int main(){
    int y; 
    scanf("%d",&y);
    if(y<1918){
        if(y%4){
            printf("13.09.%4d\n",y);
        }
        else{
            printf("12.09.%4d\n",y);
        }
    }
    else if(y== 1918){
        printf("26.09.1918\n");
    }
    else{
        if((y%400 == 0) || (y%4 == 0 && y%100)){
            printf("12.09.%4d\n",y);
        }
        else{
            printf("13.09.%4d\n",y);
        }
    }
    return 0;
}

Day of the Programmer Solution in Cpp

#include <bits/stdc++.h>
using namespace std;
int main(){
    int y;
    cin >> y;
    if(y<1918){
        if(y%4==0)cout<<"12.09."<<y<<endl;
        else cout<<"13.09."<<y<<endl;
    }
    else if(y==1918){
         cout<<"26.09."<<y<<endl;
    }
    else{
        if(y%400==0){
            cout<<"12.09."<<y<<endl;
        }
        else if(y%4==0&&y%100!=0){
            cout<<"12.09."<<y<<endl;
        }
        else cout<<"13.09."<<y<<endl;
    }
    // your code goes here
    return 0;
}

Day of the Programmer Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int y = in.nextInt();
        // your code goes here
        
        if(y>=1700 && y<=1917){
            
            if(y%4==0){
                System.out.println("12.09."+y);
            }
            else{
                System.out.println("13.09."+y);
            }
        }
        
        else{
            
            if(y==1918){
                System.out.println("26.09."+y);
            }
            else{
                if(y%400==0){
                    System.out.println("12.09."+y);
                }
                else if(y%4==0 && y%100!=0){
                    System.out.println("12.09."+y);
                }
                else{
                    System.out.println("13.09."+y);
                }
            }
        }
    }
}
Ezoicreport this ad

Day of the Programmer Solution in Python

#!/bin/python
import sys
y = int(raw_input().strip())
# your code goes here
months = {0:31,1:28,2:31,3:30,4:31,5:30,6:31,7:31,8:30,9:31,10:30,11:31}
if y <= 1917:
    tot = 0
    for i in range(12):
        if tot + months[i] > 256:
            break
        if y % 4 == 0 and i == 1:
            tot = tot + 29
        else:
            tot = tot + months[i]
    ans = ""
    day = 256 - tot
    month = i + 1
    ans = ans + str(day) + ".0" + str(month) + "." + str(y)
    print ans
elif y == 1918:
    tot = 0
    for i in range(12):
        if tot + months[i] > 256:
            break
        if i == 1:
            tot = tot + 15
        else:
            tot = tot + months[i]
    ans = ""
    day = 256 - tot
    month = i + 1
    ans = ans + str(day) + ".0" + str(month) + "." + str(y)
    print ans
else:
    tot = 0
    for i in range(12):
        if tot + months[i] > 256:
            break
        if (y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)) and i == 1:
            tot = tot + 29
        else:
            tot = tot + months[i]
    ans = ""
    day = 256 - tot
    month = i + 1
    ans = ans + str(day) + ".0" + str(month) + "." + str(y)
    print ans

Day of the Programmer Solution using JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
    input_stdin += data;
});
process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});
function readLine() {
    return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
    var y = parseInt(readLine());
    // your code goes here
    
    if(y == 1918){
        console.log('26.09.' + y)
    }else{
        
        var leapYear = false;
        
        if(y < 1918 && y % 4 == 0){
            leapYear = true;
        }else if(y > 1918 && y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)){
            leapYear = true;
        }
        
        if(leapYear){
           console.log('12.09.' + y); 
        }else{
            console.log('13.09.' + y);
        }
        
        
        
    }
}

Day of the Programmer Solution in Scala

object Solution {
    def main(args: Array[String]) {
      val year = scala.io.StdIn.readInt
        
      val leapYearDate = s"12.09.$year"
      val nonLeapYearDate = s"13.09.$year"
        
      println {year match {
        case _ if year < 1918 => if (year % 4 == 0) leapYearDate else nonLeapYearDate
        case 1918 => "26.09.1918"
        case _ => if (year % 400 == 0 || (year % 4 == 0 && !(year % 100 == 0))) leapYearDate else nonLeapYearDate
      }}
    }
}

Day of the Programmer Solution in Pascal

{$mode objfpc}
program A;
const
  JMonth: array [1..12] of Integer = 
  (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  {Thirty days hath September,
   April, June, and November.
   All the rest have thirty-one,}
  GMonth: array [1..12] of Integer = 
  (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var
  y: Integer;
  is_leap_year: Boolean;
  
begin
  ReadLn(y);
  
  if y = 1918 then
    WriteLn('26.09.1918')
  else if y < 1918 then
  begin
    is_leap_year := (y mod 4 = 0);
    if is_leap_year then
      WriteLn('12.09.', y)
    else
      WriteLn('13.09.', y);
  end
  else
  begin
    is_leap_year := (y mod 400 = 0) or ((y mod 100 <> 0) and (y mod 4 = 0));
    if is_leap_year then
      WriteLn('12.09.', y)
    else
      WriteLn('13.09.', y);
  end;
end.

Disclaimer: This problem (Day of the Programmer) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Bill Division Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad