How to resolve the algorithm Teacup rim text step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Teacup rim text step by step in the Go programming language

Table of Contents

Problem Statement

On a set of coasters we have, there's a picture of a teacup.   On the rim of the teacup the word   TEA   appears a number of times separated by bullet characters   (•). It occurred to me that if the bullet were removed and the words run together,   you could start at any letter and still end up with a meaningful three-letter word. So start at the   T   and read   TEA.   Start at the   E   and read   EAT,   or start at the   A   and read   ATE. That got me thinking that maybe there are other words that could be used rather that   TEA.   And that's just English.   What about Italian or Greek or ... um ... Telugu. For English, we will use the unixdict (now) located at:   unixdict.txt. (This will maintain continuity with other Rosetta Code tasks that also use it.)

Search for a set of words that could be printed around the edge of a teacup.   The words in each set are to be of the same length, that length being greater than two (thus precluding   AH   and   HA,   for example.) Having listed a set, for example   [ate tea eat],   refrain from displaying permutations of that set, e.g.:   [eat tea ate]   etc. The words should also be made of more than one letter   (thus precluding   III   and   OOO   etc.) The relationship between these words is (using ATE as an example) that the first letter of the first becomes the last letter of the second.   The first letter of the second becomes the last letter of the third.   So   ATE   becomes   TEA   and   TEA   becomes   EAT. All of the possible permutations, using this particular permutation technique, must be words in the list. The set you generate for   ATE   will never included the word   ETA   as that cannot be reached via the first-to-last movement method. Display one line for each set of teacup rim words.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Teacup rim text step by step in the Go programming language

This Go program finds and prints word variants that are formed by cyclically rotating the letters of a word. It uses two dictionaries, mit_10000.txt and unixdict.txt, to find word variants.

The program starts by reading the words from the specified dictionary file into a slice of strings. It then iterates over the words in the slice, excluding words with less than three letters.

For each word, the program creates a slice of runes (Unicode code points) to represent the word's letters. It then creates a slice of strings to hold the variants of the word.

The program then iterates over the runes in the word's slice, rotating the runes one position to the left at each iteration. For each rotated slice of runes, the program creates a string and checks if it is equal to the original word or if it has already been added to the slice of variants. If either of these conditions is true, the program continues to the next iteration of the outer loop.

If the string is not equal to the original word and has not already been added to the slice of variants, the program uses the sort.SearchStrings function to find the index of the string in the slice of words. If the index is equal to the length of the slice or the string at that index is not equal to the rotated string, the program continues to the next iteration of the outer loop.

If the string is equal to the rotated string, the program adds it to the slice of variants and continues to the next iteration of the inner loop.

After all possible variants of the word have been found, the program prints the slice of variants.

The program repeats this process for all words in the dictionary file, and then it prints a new line to separate the results for the two dictionaries.

Source code in the go programming language

package main

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

func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func readWords(fileName string) []string {
    file, err := os.Open(fileName)
    check(err)
    defer file.Close()
    var words []string
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        word := strings.ToLower(strings.TrimSpace(scanner.Text()))
        if len(word) >= 3 {
            words = append(words, word)
        }
    }
    check(scanner.Err())
    return words
}

func rotate(runes []rune) {
    first := runes[0]
    copy(runes, runes[1:])
    runes[len(runes)-1] = first
}

func main() {
    dicts := []string{"mit_10000.txt", "unixdict.txt"} // local copies
    for _, dict := range dicts {
        fmt.Printf("Using %s:\n\n", dict)
        words := readWords(dict)
        n := len(words)
        used := make(map[string]bool)
    outer:
        for _, word := range words {
            runes := []rune(word)
            variants := []string{word}
            for i := 0; i < len(runes)-1; i++ {
                rotate(runes)
                word2 := string(runes)
                if word == word2 || used[word2] {
                    continue outer
                }
                ix := sort.SearchStrings(words, word2)
                if ix == n || words[ix] != word2 {
                    continue outer
                }
                variants = append(variants, word2)
            }
            for _, variant := range variants {
                used[variant] = true
            }
            fmt.Println(variants)
        }
        fmt.Println()
    }
}


  

You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Scala programming language
You may also check:How to resolve the algorithm 100 prisoners step by step in the MiniScript programming language
You may also check:How to resolve the algorithm FASTA format step by step in the Ada programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the Ada programming language