Abbreviations, easy: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: OK lets try that again)
(→‎{{header|zkl}}: added code)
Line 135: Line 135:


=={{header|zkl}}==
=={{header|zkl}}==
The description does not specify what a match is so I'm using "the first
<lang zkl></lang>
match of all input characters, regardless of case, starting at first character".
<lang zkl></lang>
<lang zkl>var commands=Data(0,String,
#<<<
"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"
.toUpper().split());
#<<<

testText:=" riG rePEAT copies put mo rest "
"types fup. 6 poweRin";
testText.split().apply(fcn(w){
n:=commands.find(w.toUpper());
if(Void==n) "*error*" else commands.readString(n) })
.concat(" ").println();</lang>
{{out}}
{{out}}
<pre>
<pre>
RIGHT REPEAT *error* PUT MODIFY RESTORE *error* *error* *error* POWERINPUT
</pre>
</pre>

Revision as of 17:37, 17 September 2017

Abbreviations, easy is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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:

   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


Notes concerning the above   command table:

  •   it can be thought of as one long literal string   (with blanks at end-of-lines)
  •   it may have superfluous blanks
  •   it may be in any order   (not sorted),   but the order (as shown) must be preserved
  •   it must be in a specific upper─ and mixed case sequence
  •   the user input(s) may be in any case (upper/lower/mixed)
  •   commands will be restricted to the Latin alphabet   (A ──► Z,   a ──► z)
  •   o,   ov,   ove,   over,   overL,   overla   are all acceptable abbreviations for   overlay
  •   if there isn't a number after the command,   then there isn't an abbreviation permitted


Task
  •   The command table needn't be verified/validated.
  •   Write a function to validate if the user "words"   (given as input)   are valid   (in the command table).
  •   If the word   is   valid,   then return the full uppercase version of that "word".
  •   If the word isn't valid,   then return the lowercase string:   *error*       (7 characters).
  •   A blank input   (or a null input)   should return a null string.
  •   Show all output here.


An example test case to be used for this task

For a user string of:

 riG   rePEAT copies  put mo   rest    types   fup.    6       poweRin

the computer program should return the string:

 RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT


Related tasks



Perl 6

Works with: Rakudo version 2017.08

This strictly follows the task requirements specifications including the one stating: "if there isn't a number after the command, then there isn't an abbreviation permitted". Its output is different than the example given but I would argue this is more correct.

<lang perl6>my @commands = < 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 >;

put <riG rePEAT copies put mo rest types fup. 6 poweRin>».fc.map(

 {(my $command = @commands».fc.grep( $_ eq * )) ?? $command.uc !! '*error*' }
 ).join: ' ';</lang>
Output:
*error* REPEAT *error* PUT *error* *error* *error* *error* *error* *error*

REXX

<lang>/*REXX program validates a user "word" against a "command table" with abbreviations.*/ parse arg uw /*obtain optional arguments from the CL*/ if uw= then uw= 'riG rePEAT copies put mo rest types fup. 6 poweRin' say 'user words: ' uw

@= '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 MOve MODify 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'

say 'full words: ' validate(uw) /*display the result(s) to the terminal*/ exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ validate: procedure expose @; arg x; upper @ /*ARG capitalizes all the X words. */

         $=                                     /*initialize the return string to null.*/
            do j=1  to words(x);   _=word(x, j) /*obtain a word from the     X  list.  */
              do k=1  to words(@); a=word(@, k) /*get a legitimate command name from @.*/
              L=verify(_, 'abcdefghijklmnopqrstuvwxyz', "M")  /*maybe get abbrev's len.*/
              if L==0  then L=length(_)         /*0?  Command name can't be abbreviated*/
              if abbrev(a, _, L)   then do; $=$ a;  iterate j;  end  /*is valid abbrev?*/
              end   /*k*/
            $=$ '*error*'                       /*processed the whole list, not valid. */
            end     /*j*/
         return strip($)                        /*elide the superfluous leading blank. */</lang>
output   when using the default input:
user words:  riG   rePEAT copies  put mo   rest    types   fup.    6       poweRin
full words:  RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT

zkl

The description does not specify what a match is so I'm using "the first match of all input characters, regardless of case, starting at first character". <lang zkl>var commands=Data(0,String,

  1. <<<

"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"

.toUpper().split());

  1. <<<

testText:=" riG rePEAT copies put mo rest " "types fup. 6 poweRin"; testText.split().apply(fcn(w){

  n:=commands.find(w.toUpper());
  if(Void==n) "*error*" else commands.readString(n) })

.concat(" ").println();</lang>

Output:
RIGHT REPEAT *error* PUT MODIFY RESTORE *error* *error* *error* POWERINPUT