Write a function or program that, when given a list of names, outputs or returns a list where duplicates of given names have a unique shortened version of their surname.
Input:
A list of names, where a name is defined by a given name and a last name separated by a space. Names are non-empty strings containing only uppercase and lowercase letters. The list can be an array of strings, or the names separated by a constant non-alpha, non-space character, but the output must be in the same format as the input.
Output:
A list of the inputted names in the same order and format as the input that have been modified by these rules:
- For unique given names, output just the given name
- For names that share the same given name:
- As well as their given name, add the shortest unique version of their surname that that is not shared by another name, followed by a period. For instance:
John Clancy, John Smith
becomesJohn C.,John S.
andJames Brown, James Bratte
becomesJames Bro.,James Bra.
- If one surname is a subset of another, such as
Julian King,Julian Kingsley
, return the full surname of the smaller one without a period. The example would becomeJulian King,Julian King.
- As well as their given name, add the shortest unique version of their surname that that is not shared by another name, followed by a period. For instance:
- Basically a period represents the regex
.+
, where only one name should match it. - You may assume that no-one will share both the same given name and the same surname
- Names are case-sensitive
Test Cases:
John Clancy,Julie Walker,John Walker,Julie Clancy
->John C.,Julie W.,John W.,Julie C.
Julian King,Jack Johnson,Julian Kingsley
>Julian King,Jack,Julian King.
Jack Brown,Jack Black,Jack Blue
>Jack Br.,Jack Bla.,Jack Blu.
John Storm,Jon Snow,Johnny Storm
>John,Jon,Johnny
Jill DeSoma,Jill Desmond
>Jill DeS.,Jill Des.
XxXnO sCOppeXxX,XxXNO MERCYXxX
>XxXnO,XxXNO
This is code-golf, so the lowest byte count for each language wins.
Angela C.
come from the in the test case? \$\endgroup\$ – caird coinheringaahing Jan 20 '18 at 13:16Julie
oops, thanks. Fixed \$\endgroup\$ – Jo King♦ Jan 20 '18 at 13:25