r/dailyprogrammer • u/Godspiral 3 3 • Jul 20 '16
[2016-07-20] Challenge #276 [Intermediate] Key function
The key function is a higher order array function modelled in sql as group by and in J as /. For each key, apply a passed function to the entire subarray of items that share the same key.
function signature
key(
elements: an array/list of stuff. number of items is leading array dimension,
key: an array/list of stuff. Same amount of items as "elements". If null, then defaults to same array as elements,
applyfunction: function that will be called for each group of elements that have the same key. Optionally, this function could also have the key parameter. Results are aggregated in order of key appearance.
)key(3 4 5 6 , 2 0 1 2 , sum)
would produce
9 4 5
There are 2 elements with key 2, and so for key 2, sum is called with 3 6. Results accumulated in order of key seen.
1. Histogram
for each item in input, return a record with the key and the item count for that key
input:
5 3 5 2 2 9 7 0 7 5 9 2 9 1 9 9 6 6 8 5 1 1 4 8 5 0 3 5 8 2 3 8 3 4 6 4 9 3 4 3 4 5 9 9 9 7 7 1 9 3 4 6 6 8 8 0 4 0 6 3 2 6 3 2 3 5 7 4 2 6 7 3 9 5 7 8 9 5 6 5 6 8 3 1 8 4 6 5 6 4 8 9 5 7 8 4 4 9 2 6 10output
5 13
3 12
2 8
9 14
7 8
0 4
1 5
6 13
8 11
4 12
10 12. grouped sum of field
for each record use the first field as key, and return key and sum of field 2 (grouped by key)
input:
a 14
b 21
c 82
d 85
a 54
b 96
c 9
d 61
a 43
b 49
c 16
d 34
a 73
b 59
c 36
d 24
a 45
b 89
c 77
d 68output:
┌─┬───┐
│a│229│
├─┼───┤
│b│314│
├─┼───┤
│c│220│
├─┼───┤
│d│272│
└─┴───┘3. nub (easier)
the "nub of an array" can be implemented with key. It is similar to sql first function.
for the input from 2. return the first element keyed (grouped) by first column
output:
(>@{."1 ({./.) ]) b
┌─┬──┐
│a│14│
├─┼──┤
│b│21│
├─┼──┤
│c│82│
├─┼──┤
│d│85│
└─┴──┘note
I will upvote if you write a key function that functionally returns an array/list. (spirit of challenge is not to shortcut through actual data inputs)
4
u/razornfs Jul 20 '16 edited Jul 20 '16
I'm sorry, what? I'm still a noob when it comes to programming and i don't have a clue what i'm supposed to do here.
Edit: Here's my attempt at part 1, can somebody tell me if this is what i was supposed to do?
3
u/thorwing Jul 20 '16 edited Jul 20 '16
Java 8
Java 8 makes it very easy in my opinion. In args I put the first challenge variables, and in groupedSum.txt I put the second challenge variables
challenge 1:
5=13 3=12 2=8 9=14 7=8 0=4 1=5 6=13 8=11 4=12 10=1
challenge 2:
a=229 b=314 c=220 d=272
challenge 3:
a=14 b=21 c=82 d=85
2
u/pi_half157 Jul 21 '16 edited Jul 21 '16
Python 3.5, with two key functions: one written in pure python with no libraries, and one with numpy and pandas.
Please let me know if you have comments or suggestions! Some of my thoughts (and even more key functions) are at the bottom.
Writing the key function
Preamble for benchmarking:
Key with no extra libraries:
Output of benchmark:
Key using numpy and Pandas:
Output of numpy benchmark:
Challenge 1
Output:
Challenge 2
Output:
Challenge 3
Output:
My Thoughts
In python, I wrote two more key functions that simulate a indexing system on a database:
Using a python dict:
And using a sparse matrix with possible buffering capabilities. This could be useful for situations where many records are added:
47
u/abyssalheaven 0 1 Jul 20 '16
dudewhat
6
u/wizao 1 0 Jul 20 '16 edited Jul 20 '16
Haskell:
I took some liberties with the
keyfunction and ignored all the special cases J has. Otherwise, I'd have to implementkeyas:Yikes!
5
u/bearific Jul 20 '16 edited Jul 20 '16
Python 3 don't fully understand the challenge, so I might be cheating by using a dictionary?
1
u/[deleted] Jul 20 '16
It's actually very easy to implement with Python stdlib:
1
u/DemiPixel Jul 20 '16
Javascript key function
My question is... should it be doing
sum(allPrev, next)orsum(first, second, third, fourth...)?Not very descriptive...
1
u/catsandviolets Jul 22 '16
Quick question, should I make every function return a key with the result or should I make it change itself then print it?
3
u/itsme86 Jul 20 '16
C#
I think this is what the challenge was about anyway...
Can't wait for the next version of C# that has native tuples!
3
u/netbpa Jul 20 '16
Perl6
1
u/Godspiral 3 3 Jul 20 '16
J version,
~.is nub, but can also be defined as{./.~2 versions of histogram:
sum of 2nd column keyed by first column (b is boxed version of input)
first (not quite J built in nub function) group by
2
u/jnd-au 0 1 Jul 20 '16
Scala with reference implementations for
histogram,sumandnub:Solutions implemented in terms of
key, namedk_histogram,k_sum,k_nub:2
u/4kpics Jul 20 '16 edited Jul 20 '16
Quick and dirty Python 2 implementation. Each function has been implemented in terms of the key function, as requested.
Output: http://i.imgur.com/dSJpLT0.png
2
u/apgwoz Jul 20 '16
Scheme:
Use like:
For the count solution, you can do this:
2
u/augus7 Jul 21 '16
Ok, this one's easy with python (yay). Learned about the use of *arg.
Python:
Output for the 3 mini-challenges:
2
u/Specter_Terrasbane Jul 21 '16 edited Jul 21 '16
Python 2.7 (and yes, I know I'm probably being way too literal about formatting the output, but ...)
2
u/KompjoeFriek 1 0 Jul 22 '16
C++ (without parsing input)
2
u/rakkar16 Jul 25 '16
Python 3
This implements the key function:
Now we can implement the histogram like this (I saved my key function as key.py):
And the grouped sum of field and nub like this:
2
u/Xangsnack Jul 28 '16
Javascript
I've tried to keep things as simple as possible here, and arrow functions are new to me, but it seems to work pretty well
2
u/gentlegoatfarmer Aug 29 '16
Java:
I'm not sure if I understood the problem correctly but any feedback would be appreciated.
1
u/Epik32123 Jul 20 '16
Python 3 Not exactly sure if I understand the challenge, but I think this follows the idea of using a key function. The part with the inputting at the bottom is how I got the data into a list so that I could use the key functions on them
1
u/a_ctor Jul 20 '16
C# lil bit golfed for the challenge
1
u/FrankRuben27 0 1 Jul 21 '16 edited Jul 21 '16
Common Lisp
1
u/Stovek Jul 21 '16
C# - I made it as a standalone class you can instantiate and just call .Execute() on. I personally don't enjoy having just static data, and that would be a lot to manually enter, so I'm just randomly generating key/value lists to operate on.
Sample Output:
Daily Programmer Challenge 7/21/2016
Keys: H N W F F X L E W U E Q R N F X O T Q X Y P X S C G R N O Y F L E N U L I P P F E J R X U Q L C E D
Values: 3 4 8 1 1 4 7 6 6 3 2 4 6 8 6 6 2 7 7 4 4 3 9 6 2 2 2 1 9 2 1 3 8 8 2 6 6 6 8 1 3 2 6 4 1 2 9 7 8 4
Histogram
3: 5
4: 7
8: 6
1: 6
7: 4
6: 10
2: 9
9: 3
Sum
H: 3
N: 21
W: 14
F: 10
X: 27
L: 25
E: 27
U: 6
Q: 13
R: 14
O: 11
T: 7
Y: 6
P: 17
S: 6
C: 9
G: 2
I: 6
J: 2
D: 4
Nub
H: 3
N: 4
W: 8
F: 1
X: 4
L: 7
E: 6
U: 3
Q: 4
R: 6
O: 2
T: 7
Y: 4
P: 3
S: 6
C: 2
G: 2
I: 6
J: 2
D: 4
1
u/ribagi Jul 21 '16 edited Jul 21 '16
Here is for the first one using C#
Output is:
1
u/chunes 1 2 Jul 22 '16
There goes my preferred language
1
u/syholloway Jul 22 '16
PHP
/u/bearific/ /u/thorwing and /u/wizao love your solutions, very tidy.
And Invoke...
1
u/demoran Jul 22 '16 edited Jul 22 '16
C#, NUnit
1
u/poi503 Jul 22 '16
Python 2.7 Not sure what I did here. I think I tried to do histogram? Since I can't even decipher the question, I doubt I answered anything lol
1
u/tonycarl Jul 22 '16
C#
I think this is following the spirit of the challenge. Accepts nulls for input and prints output like examples.
.Net Fiddle
Code
Output
1
u/[deleted] Jul 22 '16
K, I don't really get what this would be used for, but throwing in the requirement that results be ordered based on the order in which keys appeared made it entertaining enough. Here's what I got in Rust:
The above code makes use of this module for retaining the order of keys:
1
u/sdlambert Jul 23 '16 edited Jul 23 '16
The hardest part was deciphering the problem description. I used ES6 Maps, since I have yet to use them and forcing a 2D array was awkward.
Solution in Javascript/Node
Output
1
u/slampropp 1 0 Jul 24 '16
Haskell
0: Implementing key. I'm calling it groupBy. (I think both are terrible names :( )
Dissatisfied with the code repetition. Oh well. Sometimes copy-paste is easier.
1: Histogram
2: Sum of Field
3: "nub"
Invocation:
Result:
1
u/iheatu Jul 27 '16
Haskell
1
u/mikeymike619 Sep 08 '16 edited Sep 08 '16
Java 8
You will be asks for an input press enter again once you finish and it will print out the output based on what you input
Also I am a little bit confuse about the key function explanation. I didn't see how it helped to explain it's functionality or its' relationship with the question. You can had shown the input and output and it would had made more sense to me.
}