Header Ad

HackerRank Day of the Programmer problem solution

In this Day of the Programmer problem you have 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.

HackerRank Day of the Programmer problem solution


Problem solution in Python programming.

#!/bin/python3

import sys


y = int(input().strip())
count=0
if(y>1918 and y<=2700):
    if(y%400==0 or (y%4==0 and y%100!=0)):
        count=1
    if(count==1):
        print('12.09.'+str(y))
    else:
        print('13.09.'+str(y))
if(y<1918 and y>=1700):
    if(y%4==0):
        count=1
    if(count==1):
        print('12.09.'+str(y))
    else:
        print('13.09.'+str(y))
if(y==1918):
    print('26.09.1918')





Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static int daysInFeb(int year){
        if(year == 1918){
            return 15;
        } else if (year < 1918) {
            if (year % 4 == 0) {
                return 29;
            } else{
                return 28;
            }
        } else { //year > 1918
            if (year % 400 == 0){
                return 29;
            } else if (year % 100 == 0){
                return 28;
            } else if (year % 4 == 0){
                return 29;
            } else {
                return 28;
            }
        }
    }
    
    
    public static int daysInMonth(int month, int year){
        if (month == 2) return daysInFeb(year);
        else if (Arrays.asList(1,3,5,7,8,10,12).contains(month)){
            return 31;
        } else return 30;
    }
    
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        // your code goes here
        int month = 0;
        int dayssofar = 0;
        while (dayssofar <= 256){
            month++;
            dayssofar += daysInMonth(month,year);
        }
        int day = daysInMonth(month,year);
        if (month == 2 && year == 1918) day += 13;
        while (dayssofar > 256) {
            day--;
            dayssofar--;
        }
        
        System.out.format("%02d.%02d.%04d",day,month,year);
        
    }
}


Problem solution in C++ programming.

#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;
}


Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main()
{
    int y,leap; 
    scanf("%d",&y);
    leap=0;
    if (y % 4 == 0) leap=1;
    if (y > 1918)
    {
    if (y % 100 == 0) leap=0;
    if (y % 400 == 0) leap=1;
    }
    if (y != 1918) 
    {
        if (leap == 0) printf("13.09.%d\n",y);
        else printf("12.09.%d\n",y);
    }
    else printf("26.09.%d\n",y);
    // your code goes here
    return 0;
}


Problem solution in JavaScript programming.

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());
    var leap = 0; //make 1 if it is a leap year...remove a day
    if(y < 1918){
        if(y%4 === 0){leap = -1}
        console.log(13 + leap + ".09." + y);
    }else if(y > 1918){
        if(y%4 === 0 && y%100 !== 0 || y%400 === 0){leap = -1};
        console.log(13 + leap + ".09." + y);
    }else{ //case for 1918
        console.log("26.09.1918");
    }
}


Post a Comment

1 Comments