Here I am trying to write a function FindMajorDifference(words) where it returns words that are 10-, 11- and 12-string from the file "words.txt". A k-string is a string B where the distance between any pair of distinct letters (within a circular arrangement of the alphabet) is larger than k. For example,
- "silk" is a 1-string
- "oaks" is a 3-string, 2-string and 1-string.
In my code below, I tried to put all 10-, 11- and 12-string into an array but I think there's something wrong with it. I've been trying to figure out if I processed the file line by line correctly or not.
package main
import (
"fmt"
"io/ioutil"
)
func findMajorDifference(words []byte) []string
{
alpha := "abcdefghijklmnopqrstuvwxyz"
major := []string{}
B := string(words)
distance := 0 // current distance between 2 distinct letters (pos1 - pos2)
min := 26 // smallest distance between 2 distinct letters
pos1 := 0 // position of first letter in the alpha array
pos2 := 0 // position of second letter in the alpha array
for i := 0; i < len(B); i++ {
current := B[i] // current letter
for x := 1; x < len(B); x++ {
next := B[x] // next distinct letter
if current != next {
// find position of letters
for j := 0; j < len(alpha); j++ {
if current == alpha[j] {
pos1 = j
}
}
for k := 0; k < len(alpha); k++ {
if next == alpha[k] {
pos2 = k
}
}
// find distance
if pos1 > pos2 {
distance = pos1 - pos2
} else {
distance = pos2 - pos1
}
if distance < min {
min = distance
}
}
}
if min == 11 || min == 12 || min == 13 {
major = append(major, string(B[i]))
}
}
return major
} // end of findMajorBinjai
func main()
{
words, err := ioutil.ReadFile("words.txt")
if err != nil {
fmt.Println("File reading error", err)
return
}
fmt.Println("test") // This line is printed
fmt.Println("%s", findMajorDifference(words)) // Gives no output
}
My code didn't give out any errors but it didn't print the output I wanted either.