How to resolve the algorithm Abbreviations, automatic step by step in the Perl programming language
How to resolve the algorithm Abbreviations, automatic step by step in the Perl programming language
Table of Contents
Problem Statement
The use of abbreviations (also sometimes called synonyms, nicknames, AKAs, or aliases) can be an easy way to add flexibility when specifying or using commands, sub─commands, options, etc.
It would make a list of words easier to maintain (as words are added, changed, and/or deleted) if the minimum abbreviation length of that list could be automatically (programmatically) determined.
For this task, use the list (below) of the days-of-the-week names that are expressed in about a hundred languages (note that there is a blank line in the list). Caveat: The list (above) most surely contains errors (or, at the least, differences) of what the actual (or true) names for the days-of-the-week.
To make this Rosetta Code task page as small as possible, if processing the complete list, read the days-of-the-week from a file (that is created from the above list).
Notes concerning the above list of words
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abbreviations, automatic step by step in the Perl programming language
Source code in the perl programming language
use strict;
use utf8;
binmode STDOUT, ":utf8";
sub auto_abbreviate {
my($string) = @_;
my @words = split ' ', $string;
my $max = 0;
return '' unless @words;
map { $max = length($_) if length($_) > $max } @words;
for $i (1..$max) {
my %seen;
return $i if @words == grep {!$seen{substr($_,0,$i)}++} @words;
}
return '∞';
}
open $fh, '<:encoding(UTF-8)', 'DoWAKA.txt';
while ($_ = <$fh>) {
print "$.) " . auto_abbreviate($_) . ' ' . $_;
}
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the C++ programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Stata programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Euphoria programming language