新月子序列的切片

huangapple go评论82阅读模式
英文:

Slices of crescent subsequences

问题

我有一个包含一些值的float64切片和一个float值epsilon,我想要做的是:

假设切片已经排序好了,我想要遍历float64切片,并检查序列中的每个值是否比下一个值大至少epsilon。
如果它不大于epsilon的值,那么我们将在一个切片的切片上附加一个新的切片,其中包含所有读取的数字,并将下一个数字放入一个新的切片,直到满足相同的条件或者我们完成遍历切片。

输入:

Epsilon: 0.001
float64切片:[0.4351 0.455 0.4356 0.4359 0.4362]

期望输出:

返回的切片:[0.4351 0.4355] [0.4356 0.4359 0.4362]

这是我尝试实现的方式:

for i := 0; i < len(sliceFloat); i++ {
    for j := i + 1; j < len(sliceFloat); j++ {
        if sliceFloat[i] - sliceFloat[j] <= epsilon {
            sliceOfSlices = append(sliceOfSlices, sliceFloat[i:j])
        } else {
            continue
        }
    }
}
return sliceOfSlices

这是我得到的输出:

[[0.4351] [0.4351 0.4355] [0.4351 0.4355 0.4356] [0.4351 0.4355 0.4356 0.4359] [0.4355] [0.4355 0.4356] [0.4355 0.4356 0.4359] [0.4356] [0.4356 0.4359] [0.4359]]

我做错了什么,我该如何修复?

英文:

I have a slice of float64 containing some values and a float value epsilon, what I would like to do is:

assuming that the slice got already sorted I want to go through the slice of float64 and check that every value of the sequence is bigger than the next one of at least value epsilon.
If it’s not bigger than the value epsilon than we will append on a slice of slices a new slice containing all the numbers read and the next numbers will be put in a new slice until the same condition happens or we finish going through the slice.

INPUT:

Epsilon : 0,001 
Slice of floats64: [0,4351 0,455 0,4356 0,4359 0,4362]

DESIRED OUTPUT:

Returned slices: [ 0,4351 0,4355 ] [ 0,4356 0,4359 0,4362 ]

This is how I've tried to implement this:

for i := 0; i &lt; len(sliceFloat); i++ {
    for j := i + 1; j &lt; len(sliceFloat); j++ {
        if sliceFloat[i] - sliceFloat[j] &lt;= epsilon {
            sliceOfSlices = append(sliceOfSlices, sliceFloat[i:j])
        } else {
            continue
        }
    }
}
return sliceOfSlices

This is the output that I get:

[[0.4351] [0.4351 0.4355] [0.4351 0.4355 0.4356] [0.4351 0.4355 0.4356 0.4359] [0.4355] [0.4355 0.4356] [0.4355 0.4356 0.4359] [0.4356] [0.4356 0.4359] [0.4359]]

What am I doing wrong and how can I fix this?

答案1

得分: 1

你发布的测试输入明显是错误的:GIGO:垃圾进,垃圾出

Epsilon : 0.001 
浮点数切片:[0.4351 0.455 0.4356 0.4359 0.4362]

你的代码没有完全实现规范。

else {
    continue
}

修复所有错误后的代码:

package main

import "fmt"

func crescents(s []float64, epsilon float64) [][]float64 {
    var ss [][]float64
    for i, f := range s {
        if i == 0 || f <= s[i-1]+epsilon {
            ss = append(ss, []float64(nil))
        }
        ss[len(ss)-1] = append(ss[len(ss)-1], f)
    }
    return ss
}

func main() {
    s := []float64{0.4351, 0.4355, 0.4356, 0.4359, 0.4362}
    epsilon := 0.0001
    ss := crescents(s, epsilon)
    fmt.Println(s, epsilon)
    fmt.Println(ss)
}

https://go.dev/play/p/h-SxeIWPuu-

[0.4351 0.4355 0.4356 0.4359 0.4362] 0.0001
[[0.4351 0.4355] [0.4356 0.4359 0.4362]]
英文:

The test input you posted is clearly wrong: GIGO: Garbage in, garbage out.

Epsilon : 0,001 
Slice of floats64: [0,4351 0,455 0,4356 0,4359 0,4362]

Your code does not attempt to fully implement the specification.

else {
    continue
}

After fixing all the bugs:

package main

import &quot;fmt&quot;

func crescents(s []float64, epsilon float64) [][]float64 {
    var ss [][]float64
    for i, f := range s {
	    if i == 0 || f &lt;= s[i-1]+epsilon {
		    ss = append(ss, []float64(nil))
	    }
	    ss[len(ss)-1] = append(ss[len(ss)-1], f)
    }
    return ss
}

func main() {
    s := []float64{0.4351, 0.4355, 0.4356, 0.4359, 0.4362}
    epsilon := 0.0001
    ss := crescents(s, epsilon)
    fmt.Println(s, epsilon)
    fmt.Println(ss)
}

https://go.dev/play/p/h-SxeIWPuu-

[0.4351 0.4355 0.4356 0.4359 0.4362] 0.0001
[[0.4351 0.4355] [0.4356 0.4359 0.4362]]

huangapple
  • 本文由 发表于 2022年10月14日 20:55:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/74069571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定