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

Published on 12 May 2024 09:40 PM
#R

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

Source code in the r programming language

library(stringi)

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

cmds <- cmds_block %>% trimws() %>% stri_split_regex("\\s+") %>% unlist()

check_word <- function(inputw,comw) {
  inputl  <- nchar(inputw)
  coml    <- nchar(comw)
  cap_cnt <- stri_count_regex(comw,"[A-Z]")
  
  ifelse(cap_cnt != 0 && inputl >= cap_cnt && inputl <= coml && 
           stri_startswith_fixed(toupper(comw),toupper(inputw)),T,F) 
}

# Inputs
intstr_list <- "riG   rePEAT copies  put mo   rest    types   fup.    6       poweRin" %>%
  stri_split_regex("\\s+") %>% unlist()

# Results
results <- sapply(intstr_list,\(y) {
  matc <- cmds[sapply(cmds,\(x) check_word(y,x))]
  ifelse(length(matc) != 0,toupper(matc[1]),"*error*")
})

print(results)


  

You may also check:How to resolve the algorithm Cuban primes step by step in the BASIC programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Racket programming language
You may also check:How to resolve the algorithm String matching step by step in the Python programming language
You may also check:How to resolve the algorithm Multiplicative order step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Set of real numbers step by step in the Haskell programming language