7
The task is to group adjacent values from collection and output it as a new aggregated collection.
Consider we have following input:
Sequence Number Value ======== ===== 1 9 2 9 3 15 4 15 5 15 6 30 7 9
The aim is to enumerate over Sequence Numbers and check if the next element has the same value. If yes, values are aggregated and so, desired output is as following:
Sequence Sequence Number Number From To Value ======== ======== ===== 1 2 9 3 5 15 6 6 30 7 7 9
Build a code with following assumptions:
- Sequence Numbers are already ordered in input table.
- There can be gaps in Sequence Numbers. If input contains gaps, output should contain gaps too. For example input
1 99\n2 99\n4 99\n6 99
, output will be1 2 99\n4 4 99\n6 6 99
. - Assume Value is type of integer.
- It can be a full program or just a function.
- This cruft
(Sequence Sequence\n
...======== ======== =====\n)
is not a part of output. Output format is not defined; there should be a delimeter between values and between rows, so anyone can distinguish values and rows.
It's an extend of my StackOverflow question: https://stackoverflow.com/questions/14879197/linq-query-data-aggregation.
1Should it be a full program with input/output or just a function? – grc – 2013-02-28T01:39:23.400
1Is all that cruft at the top (
Sequence Sequence\n
...======== ======== =====\n
) part of the expected input and output? If not, can you please specify the expected formats? – dmckee --- ex-moderator kitten – 2013-02-28T01:50:08.067@grc: I precised rules, thank you. – Dariusz Woźniak – 2013-02-28T09:50:58.360
@dmckee: As above, I precised question, thx. – Dariusz Woźniak – 2013-02-28T09:51:58.467
Regarding possible gaps in the sequence numbers: should the output also contain gaps? What would the desired output for "1 99\n2 99\n4 99\n6 99" be? – Howard – 2013-02-28T10:36:28.430
@Howard: Yes, output should also contain gaps as there must be continuity on Sequence Number; so for your input it will be:
1 2 99\n4 4 99\n6 6 99
. – Dariusz Woźniak – 2013-02-28T11:54:58.583What is the format of the input? If we're talking about a function, then can it take a data structure directly (in languages where that is applicable)? Can it also return a data structure? – aditsu quit because SE is EVIL – 2013-02-28T12:23:33.237
1@aditsu: There are no restrictions oninput/output format – Dariusz Woźniak – 2013-02-28T20:26:27.627