6
\$\begingroup\$

Consider a slice with some floating point numbers:

nums := []float64{0.17898, 0.25512, 1.98123, 1.35902, 0.97642}

Suppose I want to extract the numbers lying in between the range from 1 to 1.99 and ones lying in between the range from 0.0 to 0.99 (inclusive of both limits) from this slice and append it into two new separate slices each, then what would be the best possible way to do so?

I have the following code:

package main

import (
    "fmt"
)

func main() {
    nums := []float64{0.17, 0.25, 2.19, 0.98, 1.35, 5.97, 1.21}
    var oneSlice, zeroSlice []float64
    for _, num := range nums {
        if num <= 1.99 && num >= 1.0 {
            oneSlice = append(oneSlice, num)    
        } else if num <= 0.99 && num >= 0.0 {
            zeroSlice = append(zeroSlice, num)
            } 
    }   
    fmt.Print("1.0-1.99 values:", oneSlice, "\n0.0-0.99 values:", zeroSlice)
}

1.0-1.99 values:[1.35 1.21]
0.0-0.99 values:[0.17 0.25 0.98]

Are there any better approaches than this? (such as avoiding the if & else, or a function that can be used, which I'm not aware of? - for e.g.: a function which can compare slice elements with a given value or range of values?)

I'm new to Go, so am just wondering if there could be a possibly better approach to this! Any help is appreciated :)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Review where we review working code from your project and make suggestions on how to improve that code. Hypothetical code is considered off-topic and the question may be closed by the community. The question currently appears to be too hypothetical. If you reword the question so that it doesn't appear to be hypothetical and put real working code in the question this will become a good question. Take a look at our guidelines for asking a good question. You have a good start already. \$\endgroup\$
    – pacmaninbw
    Oct 11, 2020 at 15:21
  • 1
    \$\begingroup\$ @pacmaninbw I was able to run the code online and see output that, while not declared explicitly in the post, matched the description. I am going to update the wording in the description here \$\endgroup\$ Oct 12, 2020 at 15:40
  • \$\begingroup\$ @SᴀᴍOnᴇᴌᴀ Thanks for commenting that. In both ways, I thought the question was self-explanatory with the wording, so I didn't see a reason to edit it. (also, I obviously wouldn't throw in code that doesn't work!) \$\endgroup\$
    – Anirban166
    Oct 13, 2020 at 7:30

1 Answer 1

1
\$\begingroup\$

Note that your current code (and problem description) will filter out numbers between 0.99 and 1.0 (such as 0.995), which is almost certainly not what you want. I'm guessing the intent is the ranges 0<=num<1 and 1<=num<2.

Apart from that question of endpoints, your code is a very reasonable way to do it. Sometimes for range testing people like to write 1.0 <= num && num < 2 so that's it's kind of like the mathematical notation 1<=num<2.

In any case, a slightly tidier way would be to use switch, and order them from smallest to largest so that you only need to test the max. For example (Go Playground link):

func main() {
    nums := []float64{0.17, 0.25, 2.19, 0.98, 1.35, 5.97, 1.21}
    var ones, zeros []float64
    for _, num := range nums {
        switch {
        case num < 0:
            // skip negative numbers
        case num < 1:
            zeros = append(zeros, num)
        case num < 2:
            ones = append(ones, num)
        }
    }
    fmt.Println("1.0-1.99 values:", ones)
    fmt.Println("0.0-0.99 values:", zeros)
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.