From abf392117a2e62864044a17c992dfbc7b608fc62 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 9 Feb 2023 19:25:58 +0800 Subject: [PATCH] feat: add IsAscending, Isdescending --- slice/slice.go | 24 ++++++++++++++++++++++++ slice/slice_example_test.go | 32 ++++++++++++++++++++++++++++++++ slice/slice_test.go | 16 ++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/slice/slice.go b/slice/slice.go index 04e712f..4f9c662 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -830,6 +830,30 @@ 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 +} + // 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 diff --git a/slice/slice_example_test.go b/slice/slice_example_test.go index faae325..87498c9 100644 --- a/slice/slice_example_test.go +++ b/slice/slice_example_test.go @@ -685,6 +685,38 @@ 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 ExampleSort() { nums := []int{1, 4, 3, 2, 5} diff --git a/slice/slice_test.go b/slice/slice_test.go index 33b1d11..d911464 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -603,6 +603,22 @@ 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 TestSort(t *testing.T) { assert := internal.NewAssert(t, "TestSort")