to be
a problem slover

go split number into intervals

package util

import "errors"

func ToIntervals(number int64, start int64, interval int64) (intervals [][2]int64, err error) {
    if !(number > 0 && interval > 0 && start <= number) {
        return nil, errors.New(fmt.Sprintf("invalid input %d %d %d",number,start,interval))
    }
    if start == number {
        return [][2]int64{{start,start}}, nil
    }
    var (
        prev = start
        next = start + interval - 1
        max  = number - 1
    )
    if next > max {
        return [][2]int64{{prev, max}}, nil
    }
    for {
        intervals = append(intervals, [2]int64{prev, next})
        prev = next + 1
        next = next + interval
        if next >= max {
            intervals = append(intervals, [2]int64{prev, max})
            break
        }
    }
    return intervals, nil
}

test

func TestToIntervals(t *testing.T) {
    intervals ,err:= toIntervals(10, 3)
    fmt.Println(intervals,err) // [[0 2] [3 5] [6 8] [9 9]] <nil>
}

context

split number to intervals for each go routine to parallel process part of job

if with a start point in the middle

func toIntervals(number int64, start int64, interval int64) (intervals [][2]int64, err error) {
    if !(number > 0 && interval > 0 && start < number) {
        return nil, errors.New("invalid input")
    }
    if start == number {
        return [][2]int64{}, nil
    }
    var (
        prev = start
        next = start + interval - 1
        max  = number - 1
    )
    if next > max {
        return [][2]int64{{prev, max}}, nil
    }
    for {
        intervals = append(intervals, [2]int64{prev, next})
        prev = next + 1
        next = next + interval
        if next >= max {
            intervals = append(intervals, [2]int64{prev, max})
            break
        }
    }
    return intervals, nil
}

test

func TestToIntervals(t *testing.T) {
    intervals ,err:= toIntervals(10, 3, 3)
    fmt.Println(intervals,err) // [[3 5] [6 8] [9 9]] <nil>
}
赞(0) 打赏
欢迎转载,注明出处:刘世明的博客 » go split number into intervals

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下作者

支付宝扫一扫打赏

微信扫一扫打赏