How to resolve the algorithm Abbreviations, easy step by step in the Go programming language
How to resolve the algorithm Abbreviations, easy step by step in the Go 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 Go programming language
The provided Go code reads a table of words representing valid commands and checks whether each word in a given sentence matches any of the commands based on its length and prefix. If a match is found, the full command is returned; otherwise, an error message is displayed. Here's a detailed explanation:
-
Constant Definition:
- The
table
variable stores a space-separated list of commands.
- The
-
Function
validate
:- This function takes three arguments:
commands
is the slice of valid commands.words
is the slice of words from a sentence that needs to be validated.minLens
is a slice of minimum lengths for each command.
- It returns a slice of strings where each element represents the validated result for the corresponding word in the sentence.
- The function iterates over each word and checks if it matches a valid command based on its length and prefix:
- If a word's length is less than the minimum length of a command or greater than the length of the command, it is skipped.
- Words are converted to uppercase for case-insensitive matching.
- If a word's prefix matches a valid command, the full command is returned as the result.
- If no match is found, an error message "error" is returned.
- This function takes three arguments:
-
Main Function:
table
is trimmed and converted into a slice of commands usingstrings.Fields
.minLens
is initialized to store the minimum length of alphabetic characters for each command.- It reads a sentence from the
sentence
variable and splits it into words usingstrings.Fields
. - The
validate
function is called to validate each word in the sentence. - The results are printed to the standard output:
- The initial sentence, where words are left-padded to align with the validated results.
- The full validated commands, separated by spaces.
In summary, this code validates a list of words against a table of valid commands based on their prefix and length, and provides a list of matched commands or error messages for the given user words.
Source code in the go programming language
package main
import (
"fmt"
"strings"
)
var 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 "
func validate(commands, words []string, minLens []int) []string {
results := make([]string, 0)
if len(words) == 0 {
return results
}
for _, word := range words {
matchFound := false
wlen := len(word)
for i, command := range commands {
if minLens[i] == 0 || wlen < minLens[i] || wlen > len(command) {
continue
}
c := strings.ToUpper(command)
w := strings.ToUpper(word)
if strings.HasPrefix(c, w) {
results = append(results, c)
matchFound = true
break
}
}
if !matchFound {
results = append(results, "*error*")
}
}
return results
}
func main() {
table = strings.TrimSpace(table)
commands := strings.Fields(table)
clen := len(commands)
minLens := make([]int, clen)
for i := 0; i < clen; i++ {
count := 0
for _, c := range commands[i] {
if c >= 'A' && c <= 'Z' {
count++
}
}
minLens[i] = count
}
sentence := "riG rePEAT copies put mo rest types fup. 6 poweRin"
words := strings.Fields(sentence)
results := validate(commands, words, minLens)
fmt.Print("user words: ")
for j := 0; j < len(words); j++ {
fmt.Printf("%-*s ", len(results[j]), words[j])
}
fmt.Print("\nfull words: ")
fmt.Println(strings.Join(results, " "))
}
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Haskell programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the HicEst programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the Ada programming language