How to resolve the algorithm Abbreviations, automatic step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Abbreviations, automatic 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.

It would make a list of words easier to maintain   (as words are added, changed, and/or deleted)   if the minimum abbreviation length of that list could be automatically (programmatically) determined.

For this task, use the list (below) of the days-of-the-week names that are expressed in about a hundred languages   (note that there is a blank line in the list). Caveat:   The list (above) most surely contains errors (or, at the least, differences) of what the actual (or true) names for the days-of-the-week.

To make this Rosetta Code task page as small as possible, if processing the complete list, read the days-of-the-week from a file (that is created from the above list).

Notes concerning the above list of words

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abbreviations, automatic step by step in the Go programming language

This program reads a text file containing a list of days of the week on each line and prints the minimum number of characters needed to uniquely identify each day.

  • The program uses the distinctStrings function to remove duplicate strings from a slice of strings.
  • It then uses the takeRunes function to take the first n characters of a string.
  • The main function reads the input file and loops through each line.
  • For each line, it splits the line into a slice of strings representing the days of the week.
  • It then calls the distinctStrings function to check if all the days have unique names.
  • If they do, it loops through the first n characters of each day until it finds a set of unique abbreviations for all the days.
  • The minimum number of characters needed to uniquely identify each day is then printed.

Source code in the go programming language

package main

import(
    "bufio"
    "fmt"
    "os"
    "strings"
)

func distinctStrings(strs []string) []string {
    len := len(strs)
    set := make(map[string]bool, len)
    distinct := make([]string, 0, len)
    for _, str := range strs {
        if !set[str] {
            distinct = append(distinct, str)
            set[str] = true
        }
    }
    return distinct
}

func takeRunes(s string, n int) string {
    i := 0
    for j := range s {
        if i == n {
            return s[:j]
        }
        i++
    }
    return s
}

func main() {
    file, err := os.Open("days_of_week.txt")
    if err != nil {
        fmt.Println("Unable to open file.")
        return
    }
    defer file.Close()
    reader := bufio.NewReader(file)
    lineCount := 0
    for {
        line, err := reader.ReadString('\n')
        if err != nil { // end of file reached
            return
        }
        line = strings.TrimSpace(line)
        lineCount++
        if line == "" {
            fmt.Println()
            continue
        }
        days := strings.Fields(line)
        daysLen := len(days)
        if (len(days) != 7) {
            fmt.Println("There aren't 7 days in line", lineCount)
            return
        }
        if len(distinctStrings(days)) != 7 { // implies some days have the same name
            fmt.Println(" ∞ ", line)
            continue
        }
        for abbrevLen := 1; ; abbrevLen++ {
            abbrevs := make([]string, daysLen)
            for i := 0; i < daysLen; i++ {
                abbrevs[i] = takeRunes(days[i], abbrevLen)
            }
            if len(distinctStrings(abbrevs)) == 7 {
                fmt.Printf("%2d  %s\n", abbrevLen, line)
                break
            }
        }
    }
}


  

You may also check:How to resolve the algorithm Two's complement step by step in the PL/M programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the Haskell programming language
You may also check:How to resolve the algorithm First-class functions step by step in the min programming language
You may also check:How to resolve the algorithm Summarize primes step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the COBOL programming language