The real challenge here is to combine the various (quite trivial) parts of the problem in a byte-efficient way by reusing values and/or algorithms.
If you find this challenge interesting and you use Dyalog APL (free download here), you may also want to participate in the original competition, but note that the objective there is to submit quality code – not necessarily short code. If you are a student, you can win USD 100 for Phase I. Phase II (not related to this code golf challenge) can win you up to USD 2000 and a free trip to Scotland!
Take two strings (of any 7-bit ASCII characters) by any means and order (but please include input format in your answer). You may take them as separate arguments/inputs or as a single list of strings. One string contains the delimiters in the other. E.g. given
'hello,hello;there;my/yes/my/dear,blue,world', the sub-strings are
['hello', 'hello', 'there', 'my', 'yes', 'my', 'dear', 'blue', and 'world'].
You will do some statistics and ordering based on the lengths of those sub-strings that are not duplicated in the input. (You may chose to consider casing or not.) In the example given, those are
['there', 'yes', 'dear', 'blue', 'world'].
Give (by any means) a list of four elements in the overall structure
[Number_1, Number_2, [List_3], [[List_4_1], [List_4_2]]]:
- The mean of the lengths of the non-duplicated strings. In our example, that is
- The median of the lengths of the non-duplicated strings. In our example, that is
- The mode(s) of the lengths of the non-duplicated strings. In our example, that is
- The original sub-strings (including duplicates), divided into two lists; the first are those that are not one of the above mode lengths, and the second are those that are. In our example:
[['my', 'my', 'yes'], ['dear', 'blue', 'hello', 'hello', 'there', 'world']]
The combined final answer of our example case is:
[4.2, 4, [4, 5], [['my', 'my', 'yes'], ['dear', 'blue', 'hello', 'hello', 'there', 'world']]]
Don't forget to reuse code and values. E.g. the median is the average of the 1 or 2 middle elements, and you'll need the mode(s) to reorder the original list.
Watch out for division by zero; an empty input means one string with length zero, so the mean length is zero. If there are no non-unique sub-strings, return zero for both mean and median, an empty list for modes, and put all original sub-strings onto the non-mode list.
The list of modes, and the sub-lists of non-mode-length strings, and mode-length strings, do not need to be sorted in any way.
If all non-duplicated sub-strings have different lengths, then all the lengths are considered modes.
Wherever the term list is used, you may use any collection of multiple data pieces that your language supports, e.g. tuples, arrays, dictionaries, etc.
The numeric values only need to be within 1% of the actual value.
[4.2, 4, [4, 5], [['my', 'my', 'yes'], ['dear', 'blue', 'hello', 'hello', 'there', 'world']]
[0, 0, , [, ['']]]
Note: while there are two delimiter characters, none of them occur in the data string, so this is just one empty string. Mean, median, and mode are therefore zero There are no strings in the non-mode list and just the single empty string in the mode list.
[11, 11, , [, ['ppcg.se.com']]]
Note: no delimiters, so the entire input is one string.
[0, 0, , [['', 'math', '', 'math', ''], ]]
Note: there are three empty sub-strings, and since all sub-strings have at least one duplicate, the mean and median default to zero, as per Considerations, above.
[1.333, 1, [0,1,3], [['math', 'math'], ['new', '!', '']]]
Note: there is only one of each length among the non-duplicated sub-strings, so all three lengths are modes.
You can verify my explicit permission to post this challenge here by contacting the author of the original competition.