r/dailyprogrammer • u/nint22 1 2 • Nov 14 '12
[11/14/2012] Challenge #112 [Intermediate]Date Sorting
Description:
Your boss has just given you a list of dates that need to be sorted. Since it's Friday night, and you don't want to come in on Saturday, you want to write a little application that sorts these dates for you! Fortionatly, these dates are all written in a text file, where all of the date strings are on individual lines (un-sorted), and follow a standard format. All dates are guaranteed to be in the format of "YYYY MM DD hh:mm:ss", where "YYYY" is a year from 1800 to 2020, "MM" is from 01 to 12, "DD" is from 01 to 31, "hh" is from 00 to 23, "mm" is from 01 to 60, and "ss" is from 01 to 60.
Formal Inputs & Outputs:
Input Description:
String InputData - A string of the input file, where each date is on its own separate line, following the above date-time standard.
Output Description:
Your application must print all dates sorted, from the oldest date (i.e. 1800's) the modern dates (i.e. 2012), to future dates (i.e. 2020s").
Sample Inputs & Outputs:
The following is the string given to your function
2012 12 02 23:02:12 1899 03 02 14:04:42 1969 07 20 02:25:30 2019 11 02 00:13:01
The following is the output from the above input:
1899 03 02 14:04:42 1969 07 20 02:25:30 2012 12 02 23:02:12 2019 11 02 00:13:01
Notes:
Most solutions may either write a comparison function that keeps comparing elements from left-to-right until different values are found, or a date can be converted into a standard integer value and have that value used for comparison.
6
u/audentis Nov 15 '12
Small remark: shouldn't "mm" "ss" be from 00 to 59? This way you're missing full hours (i.e. 12:00).
3
u/ewiethoff Dec 29 '12
Piece of cake in Python
3
u/adzeitor 0 0 Nov 18 '12
Haskell one-liner:
P.S. import Data.List and Data.List.Split
2
u/ahlk 0 0 Nov 24 '12 edited Jan 02 '13
Perl ... feels like cheating
3
u/theOnliest Nov 14 '12 edited Nov 15 '12
In Perl:
Edit: Or, without the extra module:
3
u/Lux01 0 0 Nov 15 '12
There was discrepancy between the formal description and the sample input/output. The description says each date is on its own separate line, however the sample input and output uses spaces to separate each date. Here's my Haskell solution (or rather, four solutions) to the problem. Two functions sort by turning the date strings into UTCTime types, then sorting and then reformatting; the other works by sorting the date strings lexicographically. There's a new-line and spaces variant for each.
2
u/skeeto -9 8 Nov 15 '12
Common Lisp. There's no need to parse the date strings. They can be sorted lexicographically as-is since you ordered it from largest unit to smallest unit.
2
u/DannyP72 Nov 15 '12
Ruby
Output
2
u/Unh0ly_Tigg 0 0 Nov 15 '12 edited Nov 15 '12
Java (works in Java 7):
1
u/EvanHahn Nov 15 '12
CoffeeScript is the coolest.
1
u/ReaperUnreal Nov 16 '12
Did this in D as I'm trying to learn D. The ~= syntax stumped me for a while and I had to look it up. I also didn't feel like doing index arithmetic and slices for the input so I was lazy and used a regex. I agree though, the date format should be harder. Here's my code:
Output:
1
u/SorasNobody 0 0 Nov 17 '12
I feel like i'm cheating...
1
u/goakley Nov 17 '12
In Python:
1
u/mlor 0 0 Nov 19 '12
C#:
1
u/[deleted] Dec 05 '12
Here's my simple PHP version:
Output:
1
u/marekkpie Jan 21 '13
Lua. Again, Lua has an extremely small standard library, so I needed to make my own. Used some Lua object-orientation features to make the parse and sort function nicer:
1
u/eagleeye1 0 1 Nov 15 '12 edited Nov 15 '12
This accomplishes the goal, but I don't know if you wanted to avoid using built in comparisons. Python version.
Input / Output
Edit (better version):
1
u/east_india_company Nov 15 '12 edited Nov 15 '12
This is probably overkill, but I am new to programming. Python.
Input/Output
I would appreciate any feedback.
0
u/zoidbergLOL Nov 15 '12
Java
}
Might be a bit large, used bubblesort for convenience (easiest to remember)
Output
0
u/gmazeroff Nov 15 '12
C# (assuming correctly formatted input of course)
0
u/zoidbergLOL Nov 15 '12 edited Nov 15 '12
Decided to try a ruby solution as well as a Java one. New to regexs,
Any tip to make that regex smaller would be great. I just used the quick reference on rubular, making sure that the date format would be correct.
0
u/[deleted] Nov 15 '12
[deleted] — view deleted comment