r/dailyprogrammer • u/Coder_d00d 1 3 • Aug 22 '14
[8/22/2014] Challenge #176 [Easy] Pivot Table
Description:
An interesting way to represent data is a pivot table. If you use spreadsheet programs like Excel you might have seen these before. If not then you are about to enjoy it.
Say you have data that is related in three parts. We can field this in a table with column and rows and the middle intersection is a related field. For this challenge you will need to make a pivot table for a wind energy farm. These farms of wind mills run several windmills with tower numbers. They generate energy measured in kilowatt hours (kWh).
You will need to read in raw data from the field computers that collect readings throughout the week. The data is not sorted very well. You will need to display it all in a nice pivot table.
Top Columns should be the days of the week. Side Rows should be the tower numbers and the data in the middle the total kWh hours produced for that tower on that day of the week.
input:
The challenge input is 1000 lines of the computer logs. You will find it HERE - gist of it
The log data is in the format:
(tower #) (day of the week) (kWh)
output:
A nicely formatted pivot table to report to management of the weekly kilowatt hours of the wind farm by day of the week.
Code Solutions:
I am sure a clever user will simply put the data in Excel and make a pivot table. We are looking for a coded solution. :)
5
u/wadehn Aug 22 '14 edited Aug 22 '14
rust 0.12.0-pre-nightly a6758e344: After reading the
rusttutorial yesterday, I tried solving the problem withrust.I quite like the extensive static analysis, but I didn't enjoy solving this problem in
rust: From aC++perspective dealing with casts between&strandStringas well as&[]andVecseems pretty tedious. Also, using return values for error handling forces you to deal with errors at a low granularity, which isn't all that appropriate for such a toy problem. (There also does not seem to be something similar todonotation in Haskell for monadic sequencing.)I would also appreciate a better I/O library that I can ask for the next int instead of needing to split lines manually.
Note: I may have sounded too negative in the last paragraphs and you have to take into account that rust is still very much a work in progress. Some parts of the language are very nice: Like destructuring in a declaration (unlike
std::tie), better type-inference and simpler lambda/closure syntax.I'd be happy for suggestions. In particular, is my code good/canonical
rust?Output:
5
u/nullmove 1 0 Aug 22 '14
Did it in Nimrod. I think it's pretty expressive and powerful.
Output:
5
u/99AFCC Aug 22 '14
Python 2.7.8
Output
4
u/papabalyo Aug 23 '14
Clojure baby steps. Uses http-kit to fetch data from GitHub.
I decided not to bother with usable CLI and made really dirty formatting (without bothering to dynamically recalculate cell's width based on data).
Solution:
Averages Reports:
Totals Report:
3
u/adrian17 1 4 Aug 22 '14 edited Aug 22 '14
C++. Two similar solutions, one with a map of towers, the second one with a struct for keeping data. I couldn't decide between these two - the first is much simpler, but I thought that a struct would be closer to a real-world situation, and it would be easier to expand if the input had more columns (...although I have no idea how I would make the lambdas scale too).
First:
Second:
The same result for both:
3
u/aZeex2ai Aug 22 '14
My solution in Ruby. Suggestions are always appreciated!
3
u/skeeto -9 8 Aug 22 '14 edited Aug 23 '14
SQL, using SQLite again. Pretend the data as presented is a table called
logswith the following schema.It's trivial to import into a database, so there's no need to talk about that. I did it with a keyboard macro in Emacs.
Unfortunately this isn't normalized because there will be repeat rows, but there's nothing we can do about this since we're only told the day of week. Full date information would normalize it and I would make the tuple
(id, date)the primary key in that case.Here's a query that generates the report.
And here's the output (
.mode column,.width 4,.headers on).3
u/skeeto -9 8 Aug 23 '14
C, using a perfect hash function. The hash function converts the keys "Mon", "Tue", and so on into numbers 0 through 6, but in a random order. It's perfect because each of the day-of-week tokens maps to a unique digit from 0 to 6. I designed it through simple trial-and-error, combining terms and operators until I hit something that worked..
And output:
3
u/akkartik Aug 23 '14 edited Aug 23 '14
A solution in wart, my toy language:
How it looks with a little syntax highlighting.
It may look like a lot of keywords, but in fact all of the keywords are first-class and can be overridden by the user using lisp macros. More information on wart: http://akkartik.name/post/wart. More code samples: http://rosettacode.org/wiki/Category:Wart.
Output:
3
u/marchelzo Aug 23 '14 edited Aug 23 '14
Here's a solution in Haskell:
I couldn't think of a very nice way to do this so it's not the most elegant code by any means.
Output:
3
u/kirsybuu 0 1 Aug 23 '14
D language
Challenge Output:
2
u/Metroids_ Aug 22 '14
Here's my java...
Output looks like:
2
u/hutsboR 3 0 Aug 22 '14 edited Aug 22 '14
Dart:
Output:
*Days and towers are flipped, I don't think it matters too much though. Minor formatting issue and inclusion of commas/parentheses because of the way I solved the problem.
2
u/jeaton Aug 22 '14 edited Aug 23 '14
C:
and in impossible-to-read JavaScript Harmony:
2
u/MaximaxII Aug 22 '14
Feedback and criticism are always welcome :)
Challenge #176 Easy 2 - Python 3.4
Output
2
u/ooesili Aug 23 '14
C++98 solution. Please let me know if you have any tips/improvements.
2
u/[deleted] Aug 23 '14 edited Jan 10 '15
2
u/[deleted] Aug 23 '14 edited Aug 23 '14
Prolog (SWI-Prolog 7). I used DCGs for parsing, which I think overkill. I think this is substantially longer than it needs to be. Feedback or questions welcome. I'll update if I get a more succinct solution.
Edit: Forgot to post the usage/output! It looks like this:
2
u/Adiq Aug 23 '14
C# and LINQ. I'm mediocre programmer at best and with this kind of programs I always feel like my code is not neat enough :)
Output:
2
u/ddsnowboard Aug 23 '14 edited Aug 23 '14
Python 3.4. I thought it would look better to have perfectly straight lines separating rows, so I made a tkinter GUI. It logs to a file too, but I just wanted to do that little extra bit.
Any advice is welcome.
3
u/Godspiral 3 3 Aug 22 '14 edited Aug 22 '14
in J, with data on clipboard. First 30 data elements with count and average of kwh in bottom row.
misunderstood challenge :(
2
u/ENoether Aug 22 '14 edited Aug 22 '14
Python 3.4.1; as always, feedback and criticism welcome. (Now fixed because I apparently can't read today.)
Code:
Output:
2
u/SuperHolySheep Aug 22 '14
Java:
Output:
1
u/nicholascross Aug 23 '14
Python C programmer by trade, learning python via dailyprogrammer. I'll be reading other python examples to learn and definitely appreciate feedback. It felt very 'C' to me.
Code:
Output:
1
u/[deleted] Aug 23 '14
Java. Used a nested HashMap. In my mind it was the easiest way to keep track of everything, rather than using arrays.
I spent longer than I care to admit on the formatting.
Output:
1
u/ctln_mrn Aug 23 '14 edited Aug 23 '14
My solution is in Ruby:
Output:
1
u/completejoker Aug 24 '14
Using Python 2.7.
Code
Output
My first time doing this. Any feedback is appreciated.
1
u/funny_games Aug 24 '14 edited Aug 25 '14
In R.
Code
Output
1
u/datgohan Aug 24 '14 edited Aug 25 '14
This is my first attempt at any of the daily programmer problems so any comments and advice would be greatly welcomed.
I've chosen to do this in PHP as it's my most comfortable language but I will try to do the next one in Java (or I may try this again but in Java).
[Edit] - Found a bug.
Output:
1
u/regul Aug 25 '14
Go:
So I'm still very much working on my Go. I feel like there are a lot of places where this code could have been more idiomatic, so I'm welcome to suggestions. Also, I did all of this coding on a Chromebook running Chrome OS, so I think I get extra Google points or whatever.
Output:
1
u/[deleted] Aug 25 '14
C#
1
u/[deleted] Aug 25 '14 edited Aug 25 '14
Here's my D solution
1
u/shandow0 Aug 25 '14 edited Aug 25 '14
Well it aint pretty, but here's one in c#.
}
1
u/[deleted] Aug 25 '14
I'm not generally a big fan of formatting output, so I cut just about as many corners as I could think of while still printing something readable to my console. My solution is in C#.
Output:
1
u/hphpize Aug 25 '14
PHP! PHP's array/map functionality makes it trivial to aggregate / rollup this dataset.
1
u/egportal2002 Aug 26 '14 edited Aug 26 '14
AWK:
which yields:
1
u/urubu Aug 26 '14 edited Aug 26 '14
In R:
Output:
1
u/cooper6581 Aug 27 '14 edited Aug 27 '14
Erlang:
I'm not happy with this solution at all, should I have used a map or dict instead of tuples?
1
u/adamm9 Aug 29 '14
Here's some c++. Hopefully I formatted this correctly. Feedback/comments welcome.
output:
1
u/fgsguedes 0 0 Sep 13 '14 edited Sep 13 '14
A little late for the party but still.
Java 8 solution, feedback welcome. :)
edit: Included output. My week starts on Sunday.
Output:
1
u/[deleted] Oct 28 '14
Python. I don't think the results are correct but the output is as stated
Ouput:
1
u/sid_hottnutz Dec 05 '14
C# Having fun going back and trying some older challenges. This was made easier by Linq's built-in GroupBy method, which allows for aggregations after grouping.
And the output:
1
u/The-Mathematician Jan 01 '15 edited Jan 01 '15
My solution in Python 2.7. Not sure how elegant it is, but I'm still new. I'm not sure exactly the best way to format the output without making it very complicated so I just did a rudimentary one. Comments welcome.
Output: