Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
40
Task/Filter/Go/filter.go
Normal file
40
Task/Filter/Go/filter.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := rand.Perm(20)
|
||||
fmt.Println(a) // show array to filter
|
||||
fmt.Println(even(a)) // show result of non-destructive filter
|
||||
fmt.Println(a) // show that original array is unchanged
|
||||
reduceToEven(&a) // destructive filter
|
||||
fmt.Println(a) // show that a is now changed
|
||||
// a is not only changed, it is changed in place. length and capacity
|
||||
// show that it still has its original allocated capacity but has now
|
||||
// been reduced in length.
|
||||
fmt.Println("a len:", len(a), "cap:", cap(a))
|
||||
}
|
||||
|
||||
func even(a []int) (r []int) {
|
||||
for _, e := range a {
|
||||
if e%2 == 0 {
|
||||
r = append(r, e)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func reduceToEven(pa *[]int) {
|
||||
a := *pa
|
||||
var last int
|
||||
for _, e := range a {
|
||||
if e%2 == 0 {
|
||||
a[last] = e
|
||||
last++
|
||||
}
|
||||
}
|
||||
*pa = a[:last]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue