github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgSlice/ArithmeticSequence.go (about) 1 package kmgSlice 2 3 import ( 4 "fmt" 5 ) 6 7 /* 8 等差数列 9 print from start to end ,not include end. 10 it is valid input ,if step <0 11 it will panic will invalid input 12 if start==end,it will output []int{} 13 */ 14 func ArithmeticSequence(start int, end int, step int) (output []int) { 15 switch { 16 case start == end: 17 return []int{} 18 case step == 0: 19 panic("[ArithmeticSequence] step==0") 20 case step > 0 && start < end: 21 panic(fmt.Errorf("[ArithmeticSequence] start:%d<end:%d step:%d>0", start, end, step)) 22 case step < 0 && start > end: 23 panic(fmt.Errorf("[ArithmeticSequence] start:%d>end:%d step:%d<0", start, end, step)) 24 } 25 for i := start; i < end; i += step { 26 output = append(output, i) 27 } 28 return 29 } 30 31 // a slice with content 0->n-1 32 func IntRangeSlice(n int) []int { 33 if n <= 0 { 34 panic(fmt.Errorf("[IntNSlice] n:%d<=0", n)) 35 } 36 output := make([]int, n) 37 for i := 0; i < n; i++ { 38 output[i] = i 39 } 40 return output 41 }