如何获得一个切片中的值的差异? [英] how to get the differences of values that are in one slice?

查看:53
本文介绍了如何获得一个切片中的值的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从文件中检索单词,而不是切片,而只需要输出单词,其中字母是连续字母(例如feed和bcd)

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

所以我想出了这个函数,其中r [] rune是字母的int代表,而line是文件中的单词.该函数应该减去位置k的值和位置m的值,并且如果输出为0或1,它将返回bool.但是如果l [k] -l [m] = 0 |我将无法得到这个|l [k] -l [m] = 1 {工作

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
}

推荐答案

不确定我是否完全理解想要的东西(bdc似乎不是一个流行的词),但是我的解释是这样的:

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"))
}

和游乐场链接 https://play.golang.org/p/A44Xed6Naa6

关于您的代码的注释:

  1. 或为||

  1. or is ||

我认为您想访问行符文.l是整数,因此不会编译l [k]或l [m].我认为您想要r [k]或l [m]

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.

好运

这篇关于如何获得一个切片中的值的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆