Skip to content

Commit

Permalink
Add another solution for yr-monotony-slice
Browse files Browse the repository at this point in the history
  • Loading branch information
skosovsky committed Dec 11, 2023
1 parent 121b543 commit 5205928
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 16 additions & 3 deletions yr-monotony-slice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func main() {
userSlice = append(userSlice, valInt)
}

fmt.Println(checkMonotopy(userSlice))
fmt.Println(checkMonotonic(userSlice))
}

func checkMonotopy(userSlice []int) bool {
func checkMonotonic(userSlice []int) bool {
if len(userSlice) <= 2 {
return true
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func checkSlice(slice []int) bool {
return true
}

func isMonotonic(in []int) bool {
func isMonotonicA(in []int) bool {
less := false
great := true

Expand All @@ -94,3 +94,16 @@ func isMonotonic(in []int) bool {
}
return true
}

func isMonotonic(arr []int) bool {
isUp, isDown := true, true
for i := 1; i < len(arr); i++ {
if arr[i-1] > arr[i] {
isUp = false
}
if arr[i-1] < arr[i] {
isDown = false
}
}
return isUp || isDown
}
14 changes: 12 additions & 2 deletions yr-monotony-slice/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Test_checkMonotopy(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := checkMonotopy(tt.args.userSlice); gotResult != tt.wantResult {
if gotResult := checkMonotonic(tt.args.userSlice); gotResult != tt.wantResult {
t.Errorf("checkMonotopy() = %v, want %v", gotResult, tt.wantResult)
}
})
Expand All @@ -68,18 +68,28 @@ func Test_checkMonotopy(t *testing.T) {

func BenchmarkCheckMonotopy(b *testing.B) {
for i := 0; i < b.N; i++ {
checkMonotopy([]int{5, 6, 7, 6, 11, 12})
checkMonotonic([]int{5, 6, 7, 6, 11, 12})
checkMonotonic([]int{5, 6, 7, 9, 11, 12})
}
}

func BenchmarkCheckSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
checkSlice([]int{5, 6, 7, 6, 11, 12})
checkSlice([]int{5, 6, 7, 9, 11, 12})
}
}

func BenchmarkIsMonotonicA(b *testing.B) {
for i := 0; i < b.N; i++ {
isMonotonicA([]int{5, 6, 7, 6, 11, 12})
isMonotonicA([]int{5, 6, 7, 9, 11, 12})
}
}

func BenchmarkIsMonotonic(b *testing.B) {
for i := 0; i < b.N; i++ {
isMonotonic([]int{5, 6, 7, 6, 11, 12})
isMonotonic([]int{5, 6, 7, 9, 11, 12})
}
}

0 comments on commit 5205928

Please sign in to comment.