mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.1 KiB
69 lines
1.1 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
|
||
5 years ago
|
student "student"
|
||
|
|
||
|
"lib"
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
func compact(slice *[]string) int {
|
||
|
count := 0
|
||
|
var compacted []string
|
||
|
for _, v := range *slice {
|
||
|
if v != "" {
|
||
|
count++
|
||
|
compacted = append(compacted, v)
|
||
|
}
|
||
|
}
|
||
|
*slice = compacted
|
||
|
return count
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
|
arg := [][]string{{"hello", "", "hi", "", "salut", "", ""}}
|
||
5 years ago
|
|
||
|
for i := 0; i < 20; i++ {
|
||
5 years ago
|
n := lib.RandIntBetween(5, 20)
|
||
5 years ago
|
|
||
5 years ago
|
orig := make([]string, n)
|
||
5 years ago
|
|
||
5 years ago
|
numPos := lib.RandIntBetween(1, n-1)
|
||
5 years ago
|
|
||
5 years ago
|
for i := 0; i < numPos; i++ {
|
||
|
word := lib.RandWords()
|
||
|
randPos := lib.RandIntBetween(0, n-1)
|
||
|
orig[randPos] = word
|
||
5 years ago
|
}
|
||
|
arg = append(arg, orig)
|
||
|
}
|
||
|
|
||
|
for _, v := range arg {
|
||
5 years ago
|
solSlice := make([]string, len(arg))
|
||
|
stuSlice := make([]string, len(arg))
|
||
5 years ago
|
|
||
5 years ago
|
copy(solSlice, v)
|
||
|
copy(stuSlice, v)
|
||
5 years ago
|
|
||
5 years ago
|
solSize := compact(&solSlice)
|
||
|
stuSize := student.Compact(&stuSlice)
|
||
5 years ago
|
|
||
5 years ago
|
if !reflect.DeepEqual(stuSlice, solSlice) {
|
||
5 years ago
|
lib.Fatalf("Produced slice: %v, instead of %v\n",
|
||
5 years ago
|
stuSlice,
|
||
|
solSlice,
|
||
5 years ago
|
)
|
||
|
}
|
||
|
|
||
5 years ago
|
if solSize != stuSize {
|
||
5 years ago
|
lib.Fatalf("%s(%v) == %v instead of %v\n",
|
||
5 years ago
|
"Compact",
|
||
|
v,
|
||
5 years ago
|
stuSlice,
|
||
|
solSlice,
|
||
5 years ago
|
)
|
||
|
}
|
||
|
}
|
||
|
}
|