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

Published on 12 May 2024 09:40 PM
#Jq

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

Source code in the jq programming language

def trim: sub("^  *";"") | sub("  $";"");

# Each item in the stream should be a string with $n names
def minimum_abbreviation_lengths(stream; $n):
  foreach (stream|trim) as $line ({i: 0};
    .i+=1
    | if $line == "" then .emit = ""
      else [$line|splits("  *")] as $days
      | if ($days|length != $n) then .emit = "WARNING: line \(.i) does not have \($n) tokens"
        elif ($days|unique|length < $n)  # some days have the same name
        then .emit = "∞: \($line)"
        else .len = 1
	| .emit = false
        | until(.emit;
	    .len as $len
            | if ($days|map(.[:$len])|unique|length == $n) 
              then .emit = "\($len): \($line)"
              else .len += 1
	      end)
	end
      end;
      .emit) ;

minimum_abbreviation_lengths(inputs; 7)

  

You may also check:How to resolve the algorithm Loops/Do-while step by step in the Perl programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the J programming language
You may also check:How to resolve the algorithm Same fringe step by step in the C# programming language
You may also check:How to resolve the algorithm Zhang-Suen thinning algorithm step by step in the Groovy programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Slope programming language