Skip to content

Instantly share code, notes, and snippets.

@apg
Last active August 29, 2015 14:26
Show Gist options
  • Save apg/d548346a7009477ca88e to your computer and use it in GitHub Desktop.
Save apg/d548346a7009477ca88e to your computer and use it in GitHub Desktop.
$ go run score.go
Test: [1 2 3]
score=0
Test: [2 1 3]
score=1
Test: [3 2 1]
score=2
Test: [1 2 4 3 5]
score=1
Test: [1 2 4 3 5 6 8 9 7]
score=2
package main
import "fmt"
func sortScore(a []int) int {
var score int
for i := 0; i < len(a)-1; i++ {
if a[i] > a[i+1] {
score++
}
}
return score
}
func main() {
tests := [][]int{
[]int{1, 2, 3},
[]int{2, 1, 3},
[]int{3, 2, 1},
[]int{1, 2, 4, 3, 5},
[]int{1, 2, 4, 3, 5, 6, 8, 9, 7},
}
for _, t := range tests {
fmt.Println("Test:", t)
fmt.Printf("\tscore=%d\n", sortScore(t))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment