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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abbreviations, simple 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.

For this task, the following   command table   will be used:

Notes concerning the above   command table:

For a user string of: the computer program should return the string:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abbreviations, simple step by step in the Scala programming language

Source code in the scala programming language

object Main extends App {
    implicit class StrOps(i: String) {
        def isAbbreviationOf(target: String, targetMinLength: Int): Boolean = {
            @scala.annotation.tailrec
            def checkPAsPrefixOfM(p: List[Char], m: List[Char]): Boolean = (p, m) match {
                case (Nil, _) => true //prefix empty
                case (_, Nil) => false //main string empty
                case (ph :: pt, mh :: mt) if ph.toUpper == mh.toUpper => checkPAsPrefixOfM(pt, mt) //case insensitive match of head characters
                case _ => false
            }
            i.length >= targetMinLength && checkPAsPrefixOfM(i.toList, target.toList)
        }
    }

    val commands = """
         add 1  alter 3  backup 2  bottom 1  Cappend 2  change 1  Schange  Cinsert 2  Clast 3
         compress 4 copy 2 count 3 Coverlay 3 cursor 3  delete 3 Cdelete 2  down 1  duplicate
         3 xEdit 1 expand 3 extract 3  find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2
         forward 2  get  help 1 hexType 4  input 1 powerInput 3  join 1 split 2 spltJOIN load
         locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2  macro  merge 2 modify 3 move 2
         msg  next 1 overlay 1 parse preserve 4 purge 3 put putD query 1 quit  read recover 3
         refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left
         2  save  set  shift 2  si  sort  sos  stack 3 status 4 top  transfer 3  type 1  up 1
      """.stripMargin.replace("\n", " ").trim.split(" ")

    val commandWithMinLengths = commands.sliding(2, 1)
                                       .filter{ window => 
                                           window.length > 1 && Try(window(0).toInt).toOption.isEmpty
                                       }
                                       .map{ w => 
                                           (
                                               w(0), 
                                               Try(w(1).toInt).toOption.getOrElse(0)
                                           )
                                       }
                                       .toList

    val input = "riG   rePEAT copies  put mo   rest    types   fup.    6       poweRin".split(" ").filter(!_.isEmpty)

    val resultLine = input.map{ i =>
        commandWithMinLengths.find{case (c, l) => i.isAbbreviationOf(c, l)}.map(_._1.toUpperCase).getOrElse("*error*")
    }.mkString(" ")

    println(resultLine)
}


  

You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the REXX programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Metafont programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Closest-pair problem step by step in the Haskell programming language
You may also check:How to resolve the algorithm Animation step by step in the Standard ML programming language