1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

Compare commits

...

5 Commits

Author SHA1 Message Date
dudaodong
a7e77fa98d feat: add IsSorted 2023-02-09 19:30:11 +08:00
dudaodong
abf392117a feat: add IsAscending, Isdescending 2023-02-09 19:25:58 +08:00
dudaodong
d231d9f572 feat: add DropRightWhile 2023-02-09 17:53:26 +08:00
dudaodong
97447d058e test: add test and example for DropWhile 2023-02-09 17:46:40 +08:00
dudaodong
040e112aa6 feat: add DropRight and DropWhile 2023-02-09 17:38:31 +08:00
3 changed files with 283 additions and 15 deletions

View File

@@ -6,7 +6,6 @@ package slice
import (
"fmt"
"math"
"math/rand"
"reflect"
"sort"
@@ -559,24 +558,72 @@ func DeleteAt[T any](slice []T, start int, end ...int) []T {
return slice
}
// Drop creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.
// Drop drop n elements from the start of a slice.
// Play: https://go.dev/play/p/pJ-d6MUWcvK
func Drop[T any](slice []T, n int) []T {
size := len(slice)
if size == 0 || n == 0 {
return slice
}
if math.Abs(float64(n)) >= float64(size) {
if size <= n {
return []T{}
}
if n < 0 {
return slice[0 : size+n]
if n <= 0 {
return slice
}
return slice[n:size]
result := make([]T, 0, size-n)
return append(result, slice[n:]...)
}
// DropRight drop n elements from the end of a slice.
// Play: todo
func DropRight[T any](slice []T, n int) []T {
size := len(slice)
if size <= n {
return []T{}
}
if n <= 0 {
return slice
}
result := make([]T, 0, size-n)
return append(result, slice[:size-n]...)
}
// DropWhile drop n elements from the start of a slice while predicate function returns true.
// Play: todo
func DropWhile[T any](slice []T, predicate func(item T) bool) []T {
i := 0
for ; i < len(slice); i++ {
if !predicate(slice[i]) {
break
}
}
result := make([]T, 0, len(slice)-i)
return append(result, slice[i:]...)
}
// DropRightWhile drop n elements from the end of a slice while predicate function returns true.
// Play: todo
func DropRightWhile[T any](slice []T, predicate func(item T) bool) []T {
i := len(slice) - 1
for ; i >= 0; i-- {
if !predicate(slice[i]) {
break
}
}
result := make([]T, 0, i+1)
return append(result, slice[:i+1]...)
}
// InsertAt insert the value or other slice into slice at index.
@@ -783,6 +830,36 @@ func Shuffle[T any](slice []T) []T {
return slice
}
// IsAscending checks if a slice is ascending order.
// Play: todo
func IsAscending[T constraints.Ordered](slice []T) bool {
for i := 1; i < len(slice); i++ {
if slice[i-1] > slice[i] {
return false
}
}
return true
}
// IsDescending checks if a slice is descending order.
// Play: todo
func IsDescending[T constraints.Ordered](slice []T) bool {
for i := 1; i < len(slice); i++ {
if slice[i-1] < slice[i] {
return false
}
}
return true
}
// IsSorted checks if a slice is sorted(ascending or descending).
// Play: todo
func IsSorted[T constraints.Ordered](slice []T) bool {
return IsAscending(slice) || IsDescending(slice)
}
// Sort sorts a slice of any ordered type(number or string), use quick sort algrithm.
// default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`.
// Play: https://go.dev/play/p/V9AVjzf_4Fk

View File

@@ -482,10 +482,74 @@ func ExampleDrop() {
// Output:
// [a b c]
// [b c]
// [a b]
// [a b c]
// []
}
func ExampleDropRight() {
result1 := DropRight([]string{"a", "b", "c"}, 0)
result2 := DropRight([]string{"a", "b", "c"}, 1)
result3 := DropRight([]string{"a", "b", "c"}, -1)
result4 := DropRight([]string{"a", "b", "c"}, 4)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// [a b c]
// [a b]
// [a b c]
// []
}
func ExampleDropWhile() {
numbers := []int{1, 2, 3, 4, 5}
result1 := DropWhile(numbers, func(n int) bool {
return n != 2
})
result2 := DropWhile(numbers, func(n int) bool {
return true
})
result3 := DropWhile(numbers, func(n int) bool {
return n == 0
})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// [2 3 4 5]
// []
// [1 2 3 4 5]
}
func ExampleDropRightWhile() {
numbers := []int{1, 2, 3, 4, 5}
result1 := DropRightWhile(numbers, func(n int) bool {
return n != 2
})
result2 := DropRightWhile(numbers, func(n int) bool {
return true
})
result3 := DropRightWhile(numbers, func(n int) bool {
return n == 0
})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// [1 2]
// []
// [1 2 3 4 5]
}
func ExampleInsertAt() {
result1 := InsertAt([]string{"a", "b", "c"}, 0, "1")
result2 := InsertAt([]string{"a", "b", "c"}, 1, "1")
@@ -621,6 +685,54 @@ func ExampleReverse() {
// [d c b a]
}
func ExampleIsAscending() {
result1 := IsAscending([]int{1, 2, 3, 4, 5})
result2 := IsAscending([]int{5, 4, 3, 2, 1})
result3 := IsAscending([]int{2, 1, 3, 4, 5})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// false
// false
}
func ExampleIsDescending() {
result1 := IsDescending([]int{5, 4, 3, 2, 1})
result2 := IsDescending([]int{1, 2, 3, 4, 5})
result3 := IsDescending([]int{2, 1, 3, 4, 5})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// false
// false
}
func ExampleIsSorted() {
result1 := IsSorted([]int{1, 2, 3, 4, 5})
result2 := IsSorted([]int{5, 4, 3, 2, 1})
result3 := IsSorted([]int{2, 1, 3, 4, 5})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// true
// false
}
func ExampleSort() {
nums := []int{1, 4, 3, 2, 5}

View File

@@ -377,7 +377,7 @@ func TestDeleteAt(t *testing.T) {
}
func TestDrop(t *testing.T) {
assert := internal.NewAssert(t, "TestInterfaceSlice")
assert := internal.NewAssert(t, "TestDrop")
assert.Equal([]int{}, Drop([]int{}, 0))
assert.Equal([]int{}, Drop([]int{}, 1))
@@ -388,9 +388,64 @@ func TestDrop(t *testing.T) {
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, 5))
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, 6))
assert.Equal([]int{1, 2, 3, 4}, Drop([]int{1, 2, 3, 4, 5}, -1))
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, -6))
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, -6))
assert.Equal([]int{1, 2, 3, 4, 5}, Drop([]int{1, 2, 3, 4, 5}, -1))
}
func TestDropRight(t *testing.T) {
assert := internal.NewAssert(t, "TestDropRight")
assert.Equal([]int{}, DropRight([]int{}, 0))
assert.Equal([]int{}, DropRight([]int{}, 1))
assert.Equal([]int{}, DropRight([]int{}, -1))
assert.Equal([]int{1, 2, 3, 4, 5}, DropRight([]int{1, 2, 3, 4, 5}, 0))
assert.Equal([]int{1, 2, 3, 4}, DropRight([]int{1, 2, 3, 4, 5}, 1))
assert.Equal([]int{}, DropRight([]int{1, 2, 3, 4, 5}, 5))
assert.Equal([]int{}, DropRight([]int{1, 2, 3, 4, 5}, 6))
assert.Equal([]int{1, 2, 3, 4, 5}, DropRight([]int{1, 2, 3, 4, 5}, -1))
}
func TestDropWhile(t *testing.T) {
assert := internal.NewAssert(t, "TestDropWhile")
numbers := []int{1, 2, 3, 4, 5}
r1 := DropWhile(numbers, func(n int) bool {
return n != 2
})
assert.Equal([]int{2, 3, 4, 5}, r1)
r2 := DropWhile(numbers, func(n int) bool {
return true
})
assert.Equal([]int{}, r2)
r3 := DropWhile(numbers, func(n int) bool {
return n == 0
})
assert.Equal([]int{1, 2, 3, 4, 5}, r3)
}
func TestDropRightWhile(t *testing.T) {
assert := internal.NewAssert(t, "TestDropRightWhile")
numbers := []int{1, 2, 3, 4, 5}
r1 := DropRightWhile(numbers, func(n int) bool {
return n != 2
})
assert.Equal([]int{1, 2}, r1)
r2 := DropRightWhile(numbers, func(n int) bool {
return true
})
assert.Equal([]int{}, r2)
r3 := DropRightWhile(numbers, func(n int) bool {
return n == 0
})
assert.Equal([]int{1, 2, 3, 4, 5}, r3)
}
func TestInsertAt(t *testing.T) {
@@ -548,6 +603,30 @@ func TestDifferenceBy(t *testing.T) {
assert.Equal([]int{1, 2}, DifferenceBy(s1, s2, addOne))
}
func TestIsAscending(t *testing.T) {
assert := internal.NewAssert(t, "TestIsAscending")
assert.Equal(true, IsAscending([]int{1, 2, 3, 4, 5}))
assert.Equal(false, IsAscending([]int{5, 4, 3, 2, 1}))
assert.Equal(false, IsAscending([]int{2, 1, 3, 4, 5}))
}
func TestIsDescending(t *testing.T) {
assert := internal.NewAssert(t, "TestIsDescending")
assert.Equal(true, IsDescending([]int{5, 4, 3, 2, 1}))
assert.Equal(false, IsDescending([]int{1, 2, 3, 4, 5}))
assert.Equal(false, IsDescending([]int{2, 1, 3, 4, 5}))
}
func TestIsSorted(t *testing.T) {
assert := internal.NewAssert(t, "TestIsSorted")
assert.Equal(true, IsSorted([]int{5, 4, 3, 2, 1}))
assert.Equal(true, IsSorted([]int{1, 2, 3, 4, 5}))
assert.Equal(false, IsSorted([]int{2, 1, 3, 4, 5}))
}
func TestSort(t *testing.T) {
assert := internal.NewAssert(t, "TestSort")