Instead of a slice, i have to retrieve words from a file and i have to only output words whereby the letters are of running letters like feed, and bcd
So i came up with this func, whereby r []rune is the int representative of the alphabets and line is the words from the file. This function supposedly minus the value in position k and the value in position m, and if the output is either 0 or 1, it will return bool. But i can't get this if l[k]-l[m]=0 | l[k]-l[m]=1{ to work
func differenceofint(r []rune, line string) bool{
for i, j := range r{
k := int(i) //position
l := int(j) //the int representative
m:=int (i+1)
fmt.Println(k, l,m)
if l[k]-l[m]=0 | l[k]-l[m]=1{
return true
}
}
return false
}
Not sure if i understood exactly what want (bdc does not seem to be a running word) but here goes my interpretation:
package main
import (
"fmt"
"strings"
)
func running(word string) bool {
evaluator := int(word[0])
for _, v := range word {
if int(v) != evaluator-1 && int(v) != evaluator+1 && int(v) != evaluator {
return false
}
evaluator = int(v)
}
return true
}
func runningWords(line string) (result []string) {
words := strings.Split(line, " ")
for _, aword := range words {
if running(aword) {
result = append(result, aword)
}
}
return
}
func main() {
fmt.Println(runningWords("algo atum feed bcd abcdef"))
}
and a playground link https://play.golang.org/p/A44Xed6Naa6
Notes about your code:
or is ||
i think you want to access the rune of line. l is an integer so l[k] or l[m] wont compile. i think you wanted r[k] or l[m]
if it is a long text you need to consider newlines, special characters, punctuation and several other issues.
Best of luck