简体   繁体   English

如何获得一个切片中的值的差异?

[英]how to get the differences of values that are in one slice?

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 而不是切片,我必须从文件中检索单词,我必须只输出单词,其中字母是运行字母,如feed,和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. 所以我提出了这个函数,其中r []符文是字母表的int代表,而行是文件中的单词。 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. 这个函数应该减去位置k的值和位置m的值,如果输出是0或1,它将返回bool。 But i can't get this if l[k]-l[m]=0 | 但是如果l [k] -l [m] = 0 |,我就无法得到这个 l[k]-l[m]=1{ to work l [k] -l [m] = 1 {开始工作

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: 不确定我是否完全理解了什么是想要的(bdc似乎不是一个运行的词),但这里是我的解释:

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 和游乐场链接https://play.golang.org/p/A44Xed6Naa6

Notes about your code: 关于你的代码的注释:

  1. or is || 或者是||

  2. i think you want to access the rune of line. 我想你想要获得符文线。 l is an integer so l[k] or l[m] wont compile. l是一个整数,所以l [k]或l [m]不会编译。 i think you wanted r[k] or l[m] 我想你想要r [k]或l [m]

  3. if it is a long text you need to consider newlines, special characters, punctuation and several other issues. 如果它是一个长文本,你需要考虑换行符,特殊字符,标点符号和其他几个问题。

Best of luck 祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM