Abbreviations, automatic: Difference between revisions

No edit summary
Line 3,637:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>
(*This function gives all 'heads' of str of length 1 to maxLength. Since the input data was formatted to use underscores in place of spaces, there is an edge case where distinct abbreviations derived from the input data would actually not be distinct in the 'real' abbreviations. See further comments below regarding the difference between my output and the output of other solutions.*)
Abbreviations[maxLength_Integer][str_String]:=Array[StringTake[StringPadRight[str,maxLength,"_"],#]&,maxLength];
 
(*Given a list of words (weekday names), this function expands each to its abbreviations and then picks the first 'column' that contains no duplicates. The failure case isn't really needed for this specific data, but I included it for completeness.*)
ShortestUniqueAbbreviations[list:{__String}]:=
With[
{maxLength=Max[StringLength/@list]},
SelectFirst[Transpose[Abbreviations[maxLength]/@list],DuplicateFreeQ,"no unique abbreviations possible"]
];
 
(*This function gives the minimum number of characters to distinguish the words (weekday names). It assumes data is a space separated list of weekday names (i.e. one line of the input data). I would prefer to ignore blank lines, but the spec says to return an empty string. Since all of the abbreviations should be the same length because of the padding done by Abbreviations, I could have just selected the first item rather than taking the Max of the StringLength. However, the implmenetation of Abbreviations was very specific to the format of the input data, and so using Max seemed a bit safer and more general.*)
RequiredAbbreviationLength[""]="";
RequiredAbbreviationLength[input_String]:=Max[StringLength/@ShortestUniqueAbbreviations[StringSplit[input]]];
</lang>
 
=={{header|Nim}}==
23

edits