# Day of the Programmer

# Day of the Programmer

albiewalbie + 9 comments Python 2

def solve(year): if (year == 1918): return '26.09.1918' elif ((year <= 1917) & (year%4 == 0)) or ((year > 1918) & (year%400 == 0 or ((year%4 == 0) & (year%100 != 0)))): return '12.09.%s' %year else: return '13.09.%s' %year

ScienceD + 7 comments Good idea, but I think my solution is better and not much slower.

**C++**sulition that can count any holyday, by changing 256 (day of holyday). It can be variable entered by input too, easy.P.S. Well, at least holidays held in 9th month :D For ALL hilidays need to implement a way to count months too!

const int mon7 = 215; //7 months' days string solve(int year) { short int feb; //days in february if(year < 1918) //julian feb = year%4 ? 28 : 29; else if(year > 1918) //gregorian feb = !(year%400) || year%100&&!(year%4) ? 29 : 28; else //dreaded 1918 feb = 15; feb = 256 - (feb + mon7); return to_string(feb) + ".09." + to_string(year); }

Henne + 1 comment why do you think this solution is better for the solution of this specific problem if it is slower?!

ScienceD + 1 comment Cos its more straightforward and maintanable (+ it can be modifed to find any holyday, not just Programmer Day, easily).

Think about this this way: is difference in 1 microsecond (milisecond, etc.) is really worth to say that solution is better?

OFC, if we would try to find day of programmer in 1 billion years time period, yea, then faster solution is better and worth it.

Henne + 1 comment I understand what you mean, but the question is not to write code for any other problem but for this one and therefore I dont think you can say that your solution is better, because here, it is not (even if it is non significantly worse). Totally get your point though... If this wasnt a challenge for a specific problem you were right...

mindofmateo + 1 comment Dude (or dudette), this isn't code golf. There is more involved in programming than simply the fewest lines or number of bytes. Like he said above, readability, maintainability, etc. matter

*in addition to*time/space complexity.The entire point of HackerRank challenges is to develop the craft and skill of programmers, not solve abitrary problems with arbitrary paramenters and constraints for no reason, and this seems lost on you.

rohitkrishna094 + 1 comment I am not sure if that's 100% true though. I mean if you look at most of competitive programmers code, you will see that they use short variables etc which may not be readable in day to day life. Also the idea to write code for this very problem is extremely common these days in many coding challenges. There maybe something in the question that you may "exploit". You may not be able to do so, if you are trying to say generalize your problem. I mean since this is a practice problem, I agree with you, but in a real competition, I think one should TOTALLY write code only for that problem in mind, nothing else. Also competitive programming mostly teaches you algorithmic solving techniques. It doesn't teach you how to write good code. If you really wanted to write good production level code, this is not the place for it. If you think about it, most of these problems involve less lines of code. It won't teach you how to maintain huge code bases etc. :D

carlthronson + 0 comments If speed is all that matters, then you pre-calculate every year and store them in an array, where the year is the index into the array. Pre-calculate means you use pen and paper and figure it out and then you hard code it.

positivedeist_07 + 1 comment Can u pls tell me what u did in the else part??

feb = 15; feb = 256 - (feb + mon7);

DJAvanci + 0 comments feb = 15;

it was because february had only 15 days on 1918

feb = 256 - (feb + mon7);

He just used the same variable. For readability purpose, it should be better to change the name for something else, like "day" or "september_day".

devanshu_awasth1 + 1 comment in julian part you have swapped the days

positivedeist_07 + 0 comments Got it, thankyou! :)

nishant_1996 + 0 comments Your code looks good. What about my code. https://codeshattered.blogspot.in/2017/06/day-of-programmer.html

This time i haven't used to much #define :)

mindofmateo + 0 comments Your thought process is good, and it shows that you're actually trying to develop your skills. Don't worry about the people trying to crap on you; take the helpful parts of their feedback for what they're worth and ignore the rest.

somnathgsh81 + 0 comments its wrong

drewcifer + 1 comment If we're going to be like that, then:

`static string solve(int year) { //Initialize DOY, month, and day ints for return string int dayOfYear = 256; int resultMonth = 0; int resultDay = 0; //intialize array to hold # of days in each month int[] daysPerMonth = new int[13]{0,31,28,31,30,31,30,31,31,30,31,30,31}; //conditions to determine months in Feb (if not 28) if(year == 1918){ daysPerMonth[2] -= 13; } else if((year < 1918 && year % 4 == 0) || (year > 1918 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))){ daysPerMonth[2]++; } //Loop thru DPM to find date for DOY by subtracting days in each month until DOY < DPM for(int m = 0; m < daysPerMonth.Length; m++){ if(dayOfYear <= daysPerMonth[m]){ resultMonth = m; if(m == 2 && year == 1918){ dayOfYear += 13; } resultDay = dayOfYear; break; } else{ dayOfYear -= daysPerMonth[m]; } } DateTime resultDate = new DateTime(year, resultMonth, resultDay); return String.Format(resultDate.ToString("dd.MM.yyyy")); }`

shuklashashank57 + 0 comments [deleted]

metta_crawler + 1 comment Great job! You don't need the (year > 1918) though.

def solve(year): if (year == 1918): return '26.09.1918' elif ((year <= 1917) & (year%4 == 0)) or ((year%400 == 0) or ((year%4 ==0) & (year%100 != 0))): return '12.09.%s' %year else: return '13.09.%s' %year

supaix + 0 comments Well... if you want a more clean version then:

def dayOfProgrammer(year): if year == 1918: return "26.09.1918" if year % 4 == 0 and (year < 1918 or year % 400 == 0 or year % 100 != 0): return "12.09.%s" % year return "13.09.%s" % year

NotAnAmbiTurner + 0 comments This is basically my solution too, except I sort of "un-nested" everything. Sometimes brute force is the way to go.

letsconnect + 0 comments This was exatly I was doing in my C++ solution and I was getting 3 test cases failed..

sharathkumar1361 + 1 comment if(year==1918) why did you return 26th day?

moazashraf007 + 0 comments Because if you calculate in pen and paper you will always get 26.09.1918 for the year 1918 The problem says that Russia swithched to Gregorian calender after 31st January So in the year 1918 feb 14th was the 32nd day of the year for that calculating you'll always get 26.09.1918 for 1918

Wolverine8 + 0 comments how you calculate this :

if (year == 1918): return '26.09.1918'

Wolverine8 + 0 comments how you calculate this :

if (year == 1918): return '26.09.1918'

padigampavankal1 + 1 comment Even I did the same conditions as yours but executed in c. But not all test cases are cleared(19 failed). Could you please explain!

moazashraf007 + 0 comments In C you can't just return in the above way Remember that C does'nt supports String data type You have to use char arrays in place for String you can try this

char date[] = "26.09.1918"; return date;

n140625 + 2 comments Hello! could you explain me why the program return 26.09.1918 . if (year == 1918): return '26.09.1918'

WSNJY + 0 comments Directly to look at the calendar at that year. No need calculation because in that year the calendar system was changed. Easier than to calculate and more efisien. Even my answer was different with this solution, but i agree this one.

moazashraf007 + 0 comments Because if you calculate in pen and paper you will always get 26.09.1918 for the year 1918 The problem says that Russia swithched to Gregorian calender after 31st January So in the year 1918 feb 14th was the 32nd day of the year for that calculating you'll always get 26.09.1918 for 1918

axel4u + 1 comment Hi (java) people. Hope this simple logic will be useful.

`static String solve( int year ) { if( year == 1918 ) return "26.09.1918"; if( isLeap( year ) ) return "12.09." + Integer.toString( year ); else return "13.09." + Integer.toString( year ); } static boolean isLeap( int year ) { if( year % 4 != 0 ) return false; if( year > 1918 && year % 100 == 0 && year % 400 != 0 ) return false; return true; }`

arnav_banerji + 0 comments try to simplify it even more by removing Integer.toString()

Sujit07 + 6 comments I think testcase#48 is wrong because 1900 is not leap year so it should have answer 13.09.1900 but it has 12.09.1900.

Heptan + 2 comments My #48 was 1700, but the problem is the same. 1800 too. 1900 too. But 2100 is a leap year, and the expected output was 13th... It seems the test writer swapped the tow calendar usage period...

Mattia2700 + 0 comments Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the year 2000 is. Looks at: https://en.wikipedia.org/wiki/Gregorian_calendar

moazashraf007 + 0 comments Every year before 1918 is suppose to be in Jullian Calender. In the Julian Calender if the year is divisible by 4 then its a leap year. It is after 1918 that you will use the gregorian calender leap year check.

apghr + 2 comments Unlike the Gregorian calendar, in Julian calendar every year multiple of 100 is a leap year.

Heptan + 1 comment EDIT: gosh... i just noticed it was a repl for the original post, not mine. sry >_<

And that's exactly what i said. The problem is, if a year is a leap year, the 256th day should be 12th of September. And when a year is not a leap year, it should be the 13th. The year (ie 2100), what was given is a leap year, but the expected result is "13.09.2100". The correct answer should be "12.09.2100".

apghr + 0 comments You're still missing the point. 2100 will not be a leap year because modern Russia uses the Gregorian calendar (where all years multiple of 100 except multiples of 400 are NOT leap years), while 1800 was a leap year because back then they were using the Julian calendar (where all years multiple of 100 ARE leap years, none excluded).

Orleanth + 0 comments This detail must be in the explanations..

abhiram_nataraj1 + 0 comments [deleted]saurabhgangwar61 + 0 comments read the question statement carefully , before 1917 julian calendar was followed according to which every year divisible by 4 is a leap year irrespective of divisibility by 100 so 1900 is a leap year according to julian calendar.

dkch_deepak420 + 0 comments i also has the same problem like that

moazashraf007 + 0 comments 1900 is a leap year in the jullian calender. The transition look place in the year 1918 so the test case is correct my friend :)

hello_abhi + 0 comments how is year 1800 a leap year ? acc to the rules mentioned in the problem.

paniroid + 1 comment 1800 is not a leap year.

ashutoshgarg3211 + 0 comments yes

apghr + 0 comments C++

string solve(int y){ int d = 13; if(!(y%4) && (y < 1918 || y%100 || !(y%400))) d--; if(y == 1918) d = 26; return to_string(d) + ".09." + to_string(y); }

sudmong + 1 comment I could solve it be leveraing Java8 date API and just adjusting the days difference for 1918 year(13 days difference) and pre 1918 year (leap year as my base calendar in geogrian)

public static String solve(int year) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); // for all cases after 1918 as I am starting from 1st day of year int daysToAdjust = 255; if (year == 1918) { // this is the year they turned to gregorian calendar daysToAdjust = daysToAdjust + 13; } else if (year < 1918) { // jullian calendar only divide by 4 for leap year, where as gregorian excludes divide by 100 case if (year % 4 == 0 && year % 100 == 0) { daysToAdjust = daysToAdjust - 1; } } LocalDate date = LocalDate.of(year, 1, 1); LocalDate dayOfProgrammer = date.plusDays(daysToAdjust); return dayOfProgrammer.format(formatter); }

benjaminhmlucas + 0 comments wouldn't it be -13 in 1918 if days were skipped? Or did I read the question wrong?

asksmruti + 1 comment Last test case for year 1800, its NOT a leap year but as per your expected answer it's 12.09.1800 which does not seem correct to me. It should be 13.09.1800

sandeepmishra_s2 + 0 comments My code also fails for year 1700,1800. It is incorrect since both these yares are not leap year.

kbenriquez + 0 comments Python 3 solution

def solve(year): sum = 243 if year in range(1919, 2701): #gregorian if year % 400 == 0 or year % 4 == 0 and year % 100 != 0: sum += 1 elif year in range(1700, 1918): #julian if year % 4 == 0: sum += 1 else: #year 1918 sum -= 13 date = 256 - sum return "{0}.09.{1}".format(date, year)

krashnburnz + 0 comments A solution with many comments explaining the logic behind the code.

Does not use any Java Libraries like Date, Calendars, or SimpleDataFormat

Can set the day of the year to find besides 256, which increases code re-usability.

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static String solve(int year){ //Change this to find any day of the year. int dayToFind = 256; //Store days of month in a collection we can use for calculations later int [] daysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31}; //If year is greater then 1918, lets use gregorian calendar if(year > 1918){ // Gregorian: Is leap year if divisible by 400, or divisible by 4 but not 100 if( (year % 400 == 0) || ( year % 4== 0 && year % 100 != 0 )) { daysInMonths[1] = 29; } } else if(year <= 1917) { //Use Julian calendar instead //Julian: Is leap year if divisible by 4 if( year % 4 == 0) { daysInMonths[1] = 29; } } else {//is 1918, do special case for the switch in calendars from Julian to Gregorian //Feb started on day 14, meaning there were 29 - 14 = 15 days in this month for this year daysInMonths[1] = 15; } //keep memory of the sum of the days in each month int runningTotal = 0; //keep memory of which month we are in, not 0 indexing int currentMonth = 0; /*Loop through the months, and sum the days for each, checking if we exceeded the day we are trying to find. If we did, we know that day is in the current month. Then we just do some subtractions to get the actual day */ for(int i = 0; i < 13; i++) { //Add the running total with this current month runningTotal += daysInMonths[i]; currentMonth += 1; //If we exceeded the day count, we know its in this month so break. if(runningTotal > dayToFind) { break; } } //Now lets subtract to get the result, with an offset of 1 int dayOfMonth = (daysInMonths[currentMonth] - (runningTotal - dayToFind)) - 1; //Format Day and month String day = (dayOfMonth < 10) ? "0"+dayOfMonth : ""+dayOfMonth; String month = (currentMonth < 10) ? "0"+currentMonth : ""+currentMonth; return day +"." + month + "." + year; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int year = in.nextInt(); String result = solve(year); System.out.println(result); } }

Sort 339 Discussions, By:

Please Login in order to post a comment