I have a function that extracts longest common prefix of strings rounded on the last whole word. For example, if I have list:
BRIGGS & STRATTON 290400
BRIGGS & STRATTON 290700
BRIGGS & STRATTON 294400
BRIGGS & STRATTON 294700
and I call longestPrefix(array); it will return BRIGGS & STRATTON.
I am trying to chunk list of strings into sublists, grouped by the nonempty common prefix, but I can’t get any my thought to work.
Edit:
Sample list is generated in that way that for two successive elements, my function either returns empty or nonempty string:
ALLIS-CHALMERS 816
ALLIS-CHALMERS 818
ALLIS-CHALMERS 917 AND 919
ARIENS GT-17
ARIENS GT-18 AND GT-19
BOLENS 1600
BOLENS 1700
BOLENS 1900 HT-18
BOLENS HT-20 AND HT-23
BOLENS QT-16
BRIGGS & STRATTON 290400
BRIGGS & STRATTON 290700
The value returned by
longestPrefix(array)could potentially be different for each non-empty subset of your set. Additionally, each string may belong to multiple “groups”, depending on what other items are there in a group: for example, if the original list contained someBRIGGS & SONitems, theBRIGGS & STRATTONitems could have been grouped withBRIGGS &items as well.If you redefine the requirement as grouping by the longest common prefix, you could compute pairwise common prefixes, get the longest one, and group by it, like this: