How to resolve the algorithm Abbreviations, simple step by step in the Go programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Abbreviations, simple step by step in the Go 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 Go programming language
** Overview of the Code ** The provided Go code performs a command validation on a given sentence based on a provided command table and outputs the results.
** Import Statements **
- "io" for input and output operations.
- "os" for file and I/O operations.
- "strconv" for converting strings to integers.
- "strings" for string manipulation.
- "text/tabwriter" for tab-aligned text output.
** Functions ** 1. readTable
- Takes a string containing a command table as input.
- Parses the table into a slice of commands (e.g., "add", "alter") and a slice of minimum command lengths (e.g., 1, 3).
- Returns the two slices.
2. validateCommands
- Takes the commands slice, minimum command lengths slice, and a slice of words (from a sentence) as input.
- Iterates through the words in the sentence.
- For each word, checks if it matches any of the commands.
- If a match is found, adds the full command to the results slice.
- Otherwise, adds "error" to the results slice.
- Returns the results slice.
3. printResults
- Takes the words slice (from the sentence) and the results slice as input.
- Prints a table with the original words and the corresponding full commands or error messages.
** Main Function **
- Defines a constant
table
containing a list of commands and their minimum lengths. - Defines a constant
sentence
containing a user-input sentence. - Calls
readTable
to read the command table and get commands and minimum lengths. - Calls
validateCommands
to validate the words in the sentence and get results. - Calls
printResults
to display the original words and the validated results in a table.
Source code in the go programming language
package main
import (
"io"
"os"
"strconv"
"strings"
"text/tabwriter"
)
func readTable(table string) ([]string, []int) {
fields := strings.Fields(table)
var commands []string
var minLens []int
for i, max := 0, len(fields); i < max; {
cmd := fields[i]
cmdLen := len(cmd)
i++
if i < max {
num, err := strconv.Atoi(fields[i])
if err == nil && 1 <= num && num < cmdLen {
cmdLen = num
i++
}
}
commands = append(commands, cmd)
minLens = append(minLens, cmdLen)
}
return commands, minLens
}
func validateCommands(commands []string, minLens []int, words []string) []string {
var results []string
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 printResults(words []string, results []string) {
wr := tabwriter.NewWriter(os.Stdout, 0, 1, 1, ' ', 0)
io.WriteString(wr, "user words:")
for _, word := range words {
io.WriteString(wr, "\t"+word)
}
io.WriteString(wr, "\n")
io.WriteString(wr, "full words:\t"+strings.Join(results, "\t")+"\n")
wr.Flush()
}
func main() {
const table = "" +
"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 "
const sentence = "riG rePEAT copies put mo rest types fup. 6 poweRin"
commands, minLens := readTable(table)
words := strings.Fields(sentence)
results := validateCommands(commands, minLens, words)
printResults(words, results)
}
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Raku programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the C# programming language
You may also check:How to resolve the algorithm Input loop step by step in the Logo programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Elixir programming language
You may also check:How to resolve the algorithm Hash join step by step in the AWK programming language