How to resolve the algorithm Abbreviations, automatic step by step in the Red programming language
How to resolve the algorithm Abbreviations, automatic step by step in the Red 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 Red programming language
Source code in the red programming language
Red []
;; read and convert data to a string - to char conversion is neccessary to avoid
;; illegal utf8 encoding error....
data: collect/into [foreach b read/binary %abbrev.txt [keep to-char b ]] ""
;; data: read %abbrev.txt - would work, if file was utf-8 encoded ...
foreach line split data newline [ ;; split data in lines at carriage return & line feed:
if empty? trim line [ continue ] ;; continues at head of loop
arr: split line space ;; now split line in words ; accumulate in array / series
min: 1 ;; preset min length
until [
;; head is the first position of series
if head? arr [m: make map! [] ] ;; define an empty map (key -value store)
abbr: copy/part first arr min ;; copy/part ~ leftstr of first word with length min
arr: either m/:abbr [ ;; abbreviation already exists ?
min: min + 1
head arr ;; reset series position to head
][ ;; otherwise ....
m/:abbr: true ;; mark abreviation in map as existent
next arr ;; set series position to next word
]
tail? arr ;; this is the until condition , end /tail of series reached ?
]
print [min line] ;; print automatically reduces all words in block
]
You may also check:How to resolve the algorithm Color quantization step by step in the Racket programming language
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the D programming language
You may also check:How to resolve the algorithm Quine step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Penney's game step by step in the Perl programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Icon and Unicon programming language