doufei7464 2019-03-26 14:57 采纳率: 0%
浏览 7

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

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
}
  • 写回答

1条回答 默认 最新

  • doushu8260 2019-03-26 22:33
    关注

    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:

    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. i think you wanted r[k] or l[m]

    3. if it is a long text you need to consider newlines, special characters, punctuation and several other issues.

    Best of luck

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch