You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in quest 09 of the piscine i encountered this problem.
the purpose of the exercise was to spot a sorted [][]ints either descending or asceding. and print true if it indeed is sorted.
in the case in which it is not sorted, neither ascending, nor descending, the function had to print false. and after submitting my code which is this one:
package piscine
func IsSorted(f func(a, b int) int, a []int) bool {
if len(a) > 1 {
if a[0] > a[1] {
for i := 0; i < len(a)-1; i++ {
if f(a[i], a[i+1]) < 0 {
return false
}
}
} else {
for i := 0; i < len(a)-1; i++ {
if f(a[i], a[i+1]) > 0 {
return false
}
}
}
return true
}
return true
}
the feedback that i had from intra after not accepting my code was the one below:
IsSorted(main.isSortedWrong, []int{637701, -359259, -521426, 690762, -653243, 771578, 382101, 681961}) == false instead of true
exit status 1.
in which, there is indeed a [][]int which is not sorted, so based on the purpose of the exercise the function should indeed print false, as did my function.
and the only way to get through this exercise was to change my code into this:
package piscine
func IsSorted(f func(a, b int) int, a []int) bool {
ascending := true
descending := true
for i := 1; i < len(a)-1; i++ {
if f(a[i-1], a[i]) > 0 {
ascending = false
}
if f(a[i-1], a[i]) < 0 {
descending = false
}
if !ascending && !descending {
return false
}
}
return ascending || descending
}
i thought that you would be interested to have a second look at this exercise, because one could argue that the first solution should also be accepted.
The text was updated successfully, but these errors were encountered:
issorted
in quest 09 of the piscine i encountered this problem.
the purpose of the exercise was to spot a sorted [][]ints either descending or asceding. and print true if it indeed is sorted.
in the case in which it is not sorted, neither ascending, nor descending, the function had to print false. and after submitting my code which is this one:
package piscine
func IsSorted(f func(a, b int) int, a []int) bool {
if len(a) > 1 {
if a[0] > a[1] {
for i := 0; i < len(a)-1; i++ {
if f(a[i], a[i+1]) < 0 {
return false
}
}
} else {
for i := 0; i < len(a)-1; i++ {
if f(a[i], a[i+1]) > 0 {
return false
}
}
}
return true
}
return true
}
the feedback that i had from intra after not accepting my code was the one below:
IsSorted(main.isSortedWrong, []int{637701, -359259, -521426, 690762, -653243, 771578, 382101, 681961}) == false instead of true
exit status 1.
in which, there is indeed a [][]int which is not sorted, so based on the purpose of the exercise the function should indeed print false, as did my function.
and the only way to get through this exercise was to change my code into this:
package piscine
func IsSorted(f func(a, b int) int, a []int) bool {
ascending := true
descending := true
}
i thought that you would be interested to have a second look at this exercise, because one could argue that the first solution should also be accepted.
The text was updated successfully, but these errors were encountered: