r/dailyprogrammer • u/Steve132 0 1 • Aug 09 '12
[8/8/2012] Challenge #86 [intermediate] (Weekday calculations)
Today's intermediate challenge comes from user nagasgura
Calculate the day of the week on any date in history
You could use the Doomsday rule to program it. It should take in a day, month, and year as input, and return the day of the week for that date.
10
Upvotes
5
u/[deleted] Aug 09 '12
Haskell
2
u/cdelahousse Aug 10 '12
This is correct, but too easy. I'll implement an actual algorithm tomorrow.
Javascript
1
u/mathiasbynens 0 0 Aug 09 '12 edited Aug 09 '12
Does anyone have reliable test data to confirm our methods work correctly? I can’t seem to get Wolfram Alpha to calculate the day of the week for negative years (AD), for example.
JavaScript:
Example output:
P.S. This algorithm seems interesting, but I couldn’t get it to work correctly in JavaScript (even after wrapping each division in a
Math.floor()).1
u/skeeto -9 8 Aug 09 '12 edited Aug 09 '12
Using the Gaussian algorithm, in Elisp / Common Lisp.
Using a circular list instead of
modjust for kicks. :-)1
u/Eddonarth Aug 10 '12
In Java:
Input:
Output:
Using Gaussian Algorithm.
1
u/[deleted] Aug 10 '12
Java using Gaussian Algorithm
it somehow throws an out-of-bounds exception when the day is less than five and the year is 2000 afterwards. I'll look at it further
1
u/compmstr Aug 10 '12
Clojure, using gaussian algorithm:
1
u/ae7c Aug 11 '12
Python
Only works with the Gregorian calendar.
1
u/[deleted] Aug 11 '12
Python
1
u/[deleted] Aug 12 '12
AS3 (kind of cheating):
1
u/jkoers29 0 0 Dec 29 '12
}
1
u/daveasaurus Aug 09 '12 edited Aug 11 '12
Python
Sample Input
The gist of it: Python's date functionality doesn't seem to have support (or has limited support) for BC dates, these dates are pre-gregorian so there is a separate calculation performed if is_gregorian is false.
The last sample input I included only because the solution is different from mathiasbynens solution and I have no idea which one is correct.
Run code on ideone.com, Github gist
-1
u/EvanHahn Aug 09 '12
CoffeeScript is very easy because JavaScript's Date object has a nice getDay() method. It's kind of cheating.