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

Published on 12 May 2024 09:40 PM

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

Source code in the scala programming language

name         := "Abbreviations-automatic"
scalaVersion := "2.13.0"
version      := "0.1"

homepage     := Some(url("http://rosettacode.org/wiki/Abbreviations,_automatic#Scala"))

libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.3.0"


object AbbreviationsAuto extends App {
  private val wd = os.pwd

  def processLine(line: String): String = {
    if (line.nonEmpty) {
      val days = line.split(' ')
      val maxL = days.map(_.length).max
      val paddedNames = days.map(s => s.padTo(maxL, ' '))

      def uniqueN = (1 to maxL) // distinct filters uniques
        .map(n => paddedNames.map(_.substring(0, n)).distinct)
        .filter(_.size == paddedNames.length)  // `.distinct` filters uniques
        .head.head.length

      f"$uniqueN%3d $line" // Return by means of String Interpolation
    } else "    \"\""
  }

  os.read.lines(wd / "days_of_week.txt")
    .distinct
    .foreach(line => println(processLine(line)))

}


  

You may also check:How to resolve the algorithm Cantor set step by step in the AWK programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the GML programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Rust programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the jq programming language