14
1
Inspired by this StackOverflow post.
Introduction
Bob's job is to create spreadsheets and organize them. The way he organizes them is known to very few except for Bob, but he creates a list of each of the spreadsheets that fall under the same group. There's a bunch of data in the spreadsheet he creates, but there's only one piece of data that we're looking at right now: The number of days between the day he started this job and the day he made the spreadsheet. The first day he created two spreadsheets, noted them both as 0
and sorted them in to their proper locations.
Now, his boss is asking for a review of what kinds of spreadsheet happened each day, and it's your job to write some code that will figure that out for Bob; he has far too many spreadsheets to do it by hand.
Input
Bob's info that he gives you comes in the form of a (0-or-1 indexed) jagged array where each datum is of the form x = a[i][j]
. a
is what I'm calling the jagged array itself, i
is the type of spreadsheet, and x
is the date the array was created. j
is unimportant.
The task
Given a jagged array of spreadsheet creation days organized by their type, return a jagged array of spreadsheet types organized by spreadsheet creation day.
Examples
Bob isn't going to just leave you with this abstract data. He's given me a subset of some of his spreadsheets to help you out with figuring out what everything is supposed to be.
Example input (0-indexed):
a = [
[3,2,5,0], # Bob doesn't necessarily sort his lists
[1,3],
[2,1,0,4],
[4,5,3],
[6,6]
]
Example output (with commentary, which of course is not required):
output = [
[0,2] # On day 0, Bob made one type 0 and one type 2 spreadsheet
[1,2] # On day 1, Bob made one type 1 and one type 2 spreadsheet
[0,2] # On day 2, Bob made one type 0 and one type 2 spreadsheet
[0,1,3] # On day 3, Bob made one type 0, one type 1, and one type 3 spreadsheet
[2,3] # On day 4, Bob made one type 2 and one type 3 spreadsheet
[0,3] # On day 5, Bob made one type 0 and one type 3 spreadsheet
[4,4] # On day 6, Bob made two type 4 spreadsheets
]
Note that Bob doesn't always make two spreadsheets every day, and so the output may be jagged as well. But he always makes at least one spreadsheet every day, so the output will never need to contain empty arrays - although if your output has empty arrays at the end, you don't need to remove them.
More test cases:
[[3,5,6,2],[0,0,0],[1,0,3,4]] -> [[1,1,1,2],[2],[0],[0,2],[2],[0],[0]]
[[-1]] -> Undefined behavior, as all input numbers will be non-negative integers.
[[0],[0],[],[0]] -> [[0,1,3]]
The output's inner lists do not need to be sorted.
As always, no standard loopholes, and of course shortest code wins.
(As this is my first question, please let me know of anything I can do to improve it.)
Might Bob make no spreadsheets of some type? – xnor – 2016-07-19T23:46:30.060
@xnor Bob definitely sometimes has no spreadsheets of some type (see the last test case, where he doesn't have any type 2 spreadsheets). – Steven H. – 2016-07-19T23:49:47.717
Oops, what I meant is, does Bob ever create no spreadsheets on some day, causing there to be an empty list in the output? – xnor – 2016-07-20T00:05:10.087
1@xnor No, Bob will always create a spreadsheet every day. These spreadsheets are so crucial to the operation of the company that if Bob has to call in sick, another person is temporarily posted to create that day's spreadsheets and Bob organizes them the next morning before creating his own spreadsheets. – Steven H. – 2016-07-20T00:16:51.080
A few questions/comments: 1. The example output doesn't match my implementation. On day 2, the comment say
0, 2
, but the output[2,2]
. Also, day 3 seems to be missing the3
. 2. Can both input and output be 1-based, i.e., would input[[1],[1],[],[1]]
and output[[1,2,4]]
be acceptable for the last test case? 3.-1
is a whole number. It's just negative. – Dennis – 2016-07-20T02:33:52.847@Dennis 2. "in the form of a (0-or-1 indexed) jagged array" – Leaky Nun – 2016-07-20T03:23:23.690
@LeakyNun Yes, but does that apply to the input, the output or both? (I'm assuming both for now, but I want to make sure I understood correctly.) – Dennis – 2016-07-20T03:27:53.420
It applies to both. – Steven H. – 2016-07-20T03:35:56.793
1@Dennis I was a little tired when putting that example together, and I guess it showed. :P Fixed (both)! – Steven H. – 2016-07-20T05:52:55.397
6
Looking good. This is a very nice challenge, especially considering that it's your first. :) Btw, in case you're not aware, we have a sandbox where the community can provide feedback before "going live".
– Dennis – 2016-07-20T05:57:45.7401I edited in a statement based on your comment "No, Bob will always create a spreadsheet every day", but now that I've tried optimising my own answer I've realised that it's possibly more restrictive than you intended. Are trailing empty arrays permitted? E.g. can
[[0 0]]
give output[[0 0] []]
? – Peter Taylor – 2016-07-20T08:36:51.517@PeterTaylor You can have trailing empty arrays, but they are most certainly not required. I'll edit that part in as well. – Steven H. – 2016-07-20T15:50:29.400
Somewhat related. – jimmy23013 – 2019-05-29T00:48:30.147
Related. – jimmy23013 – 2019-05-29T00:50:09.517