How to resolve the algorithm Abbreviations, automatic step by step in the AWK programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abbreviations, automatic step by step in the AWK 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 AWK programming language

Source code in the awk programming language

# syntax: GAWK -f ABBREVIATIONS_AUTOMATIC.AWK ABBREVIATIONS_AUTOMATIC.TXT
{ dow_arr[NR] = $0 }
END {
    for (i=1; i<=NR; i++) {
      if (split(dow_arr[i],arr1,FS) != 7) {
        printf("NG %s\n",dow_arr[i])
        continue
      }
      col_width = 0
      for (j=1; j<=7; j++) {
        col_width = max(col_width,length(arr1[j]))
      }
      for (col=1; col<=col_width; col++) {
        delete arr2
        for (j=1; j<=7; j++) {
          arr2[toupper(substr(arr1[j],1,col))]
        }
        if (length(arr2) == 7) {
          break
        }
        if (col >= col_width) { # catches duplicate day names
          col = "NG"
          break
        }
      }
      printf("%2s %s\n",col,dow_arr[i])
    }
    exit(0)
}
function max(x,y) { return((x > y) ? x : y) }


  

You may also check:How to resolve the algorithm Program termination step by step in the PureBasic programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Neko programming language
You may also check:How to resolve the algorithm Church numerals step by step in the Erlang programming language
You may also check:How to resolve the algorithm Magic squares of odd order step by step in the Ruby programming language
You may also check:How to resolve the algorithm Break OO privacy step by step in the ABAP programming language