How to resolve the algorithm Abbreviations, easy step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abbreviations, easy step by step in the Picat programming language

Table of Contents

Problem Statement

This task is an easier (to code) variant of the Rosetta Code task:   Abbreviations, simple.

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, easy step by step in the Picat programming language

Source code in the picat programming language

import util.

command_table("Add ALTer  BAckup Bottom  CAppend Change SCHANGE  CInsert CLAst COMPress COpy
COUnt COVerlay CURsor DELete CDelete Down DUPlicate Xedit EXPand EXTract Find
NFind NFINDUp NFUp CFind FINdup FUp FOrward GET Help HEXType Input POWerinput
Join SPlit SPLTJOIN  LOAD  Locate CLocate  LOWercase UPPercase  LPrefix MACRO
MErge MODify MOve MSG Next Overlay PARSE PREServe PURge PUT PUTD  Query  QUIT
READ  RECover REFRESH RENum REPeat  Replace CReplace  RESet  RESTore  RGTLEFT
RIght LEft  SAVE  SET SHift SI  SORT  SOS  STAck STATus  TOP TRAnsfer Type Up").

validate("", _, Result) ?=>
    Result = "".

validate(Word, Commands, Result), Word \= "" ?=>
    member(Command, Commands),
    append(Prefix, Suffix, Command),
    Prefix == to_uppercase(Prefix),
    Suffix == to_lowercase(Suffix),
    LowWord = to_lowercase(Word),
    LowPrefix = to_lowercase(Prefix),
    append(LowPrefix, Other, LowWord),
    LowCommand = to_lowercase(Command),
    append(LowWord, _, LowCommand),
    Result = to_uppercase(Command).

validate(Word, _, Result), Word \= "" =>
    Result = "*error*".

main(Args) =>
    command_table(Table),
    Commands = split(Table),
    foreach (Word in Args)
        validate(Word, Commands, Result),
        printf("%w ", Result)
    end,
    nl.

  

You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Logtalk programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the C# programming language
You may also check:How to resolve the algorithm Bernstein basis polynomials step by step in the Raku programming language
You may also check:How to resolve the algorithm Count in factors step by step in the Nim programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the JavaScript programming language