Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,20 +1,19 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func uniq(list []int) []int {
|
||||
unique_set := make(map[int] bool, len(list))
|
||||
for _, x := range list {
|
||||
unique_set[x] = true
|
||||
}
|
||||
result := make([]int, len(unique_set))
|
||||
i := 0
|
||||
for x := range unique_set {
|
||||
result[i] = x
|
||||
i++
|
||||
}
|
||||
return result
|
||||
unique_set := make(map[int]bool, len(list))
|
||||
for _, x := range list {
|
||||
unique_set[x] = true
|
||||
}
|
||||
result := make([]int, 0, len(unique_set))
|
||||
for x := range unique_set {
|
||||
result = append(result, x)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(uniq([]int {1,2,3,2,3,4})) // prints: [3 1 4 2]
|
||||
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [3 4 1 2] (but in a semi-random order)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,21 +3,21 @@ package main
|
|||
import "fmt"
|
||||
|
||||
func uniq(list []int) []int {
|
||||
unique_set := make(map[int]int, len(list))
|
||||
i := 0
|
||||
for _, x := range list {
|
||||
if _, there := unique_set[x]; !there {
|
||||
unique_set[x] = i
|
||||
i++
|
||||
}
|
||||
}
|
||||
result := make([]int, len(unique_set))
|
||||
for x, i := range unique_set {
|
||||
result[i] = x
|
||||
}
|
||||
return result
|
||||
unique_set := make(map[int]int, len(list))
|
||||
i := 0
|
||||
for _, x := range list {
|
||||
if _, there := unique_set[x]; !there {
|
||||
unique_set[x] = i
|
||||
i++
|
||||
}
|
||||
}
|
||||
result := make([]int, len(unique_set))
|
||||
for x, i := range unique_set {
|
||||
result[i] = x
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [1 2 3 4]
|
||||
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [1 2 3 4]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func uniq(list []float64) []float64 {
|
||||
unique_set := map[float64]int{}
|
||||
i := 0
|
||||
nan := false
|
||||
for _, x := range list {
|
||||
if _, exists := unique_set[x]; exists {
|
||||
continue
|
||||
}
|
||||
if math.IsNaN(x) {
|
||||
if nan {
|
||||
continue
|
||||
} else {
|
||||
nan = true
|
||||
}
|
||||
}
|
||||
unique_set[x] = i
|
||||
i++
|
||||
}
|
||||
result := make([]float64, len(unique_set))
|
||||
for x, i := range unique_set {
|
||||
result[i] = x
|
||||
}
|
||||
return result
|
||||
unique_set := map[float64]int{}
|
||||
i := 0
|
||||
nan := false
|
||||
for _, x := range list {
|
||||
if _, exists := unique_set[x]; exists {
|
||||
continue
|
||||
}
|
||||
if math.IsNaN(x) {
|
||||
if nan {
|
||||
continue
|
||||
} else {
|
||||
nan = true
|
||||
}
|
||||
}
|
||||
unique_set[x] = i
|
||||
i++
|
||||
}
|
||||
result := make([]float64, len(unique_set))
|
||||
for x, i := range unique_set {
|
||||
result[i] = x
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(uniq([]float64{1, 2, math.NaN(), 2, math.NaN(), 4}))
|
||||
fmt.Println(uniq([]float64{1, 2, math.NaN(), 2, math.NaN(), 4})) // Prints [1 2 NaN 4]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,90 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func uniq(x interface{}) (interface{}, bool) {
|
||||
v := reflect.ValueOf(x)
|
||||
if !v.IsValid() {
|
||||
panic("uniq: invalid argument")
|
||||
}
|
||||
if k := v.Kind(); k != reflect.Array && k != reflect.Slice {
|
||||
panic("uniq: argument must be an array or a slice")
|
||||
}
|
||||
elemType := v.Type().Elem()
|
||||
intType := reflect.TypeOf(int(0))
|
||||
mapType := reflect.MapOf(elemType, intType)
|
||||
m := reflect.MakeMap(mapType)
|
||||
i := 0
|
||||
for j := 0; j < v.Len(); j++ {
|
||||
x := v.Index(j)
|
||||
if m.MapIndex(x).IsValid() {
|
||||
continue
|
||||
}
|
||||
m.SetMapIndex(x, reflect.ValueOf(i))
|
||||
if m.MapIndex(x).IsValid() {
|
||||
i++
|
||||
}
|
||||
}
|
||||
sliceType := reflect.SliceOf(elemType)
|
||||
result := reflect.MakeSlice(sliceType, i, i)
|
||||
hadNaN := false
|
||||
for _, key := range m.MapKeys() {
|
||||
ival := m.MapIndex(key)
|
||||
if !ival.IsValid() {
|
||||
hadNaN = true
|
||||
} else {
|
||||
result.Index(int(ival.Int())).Set(key)
|
||||
}
|
||||
}
|
||||
|
||||
return result.Interface(), hadNaN
|
||||
}
|
||||
|
||||
type MyType struct {
|
||||
name string
|
||||
value float32
|
||||
}
|
||||
|
||||
func main() {
|
||||
intArray := [...]int{5, 1, 2, 3, 2, 3, 4}
|
||||
intSlice := []int{5, 1, 2, 3, 2, 3, 4}
|
||||
stringSlice := []string{"five", "one", "two", "three", "two", "three", "four"}
|
||||
floats := []float64{1, 2, 2, 4,
|
||||
math.NaN(), 2, math.NaN(),
|
||||
math.Inf(1), math.Inf(1), math.Inf(-1), math.Inf(-1)}
|
||||
complexes := []complex128{1, 1i, 1 + 1i, 1 + 1i,
|
||||
complex(math.NaN(), 1), complex(1, math.NaN()),
|
||||
complex(math.Inf(+1), 1), complex(1, math.Inf(1)),
|
||||
complex(math.Inf(-1), 1), complex(1, math.Inf(1)),
|
||||
}
|
||||
structs := []MyType{
|
||||
{"foo", 42},
|
||||
{"foo", 2},
|
||||
{"foo", 42},
|
||||
{"bar", 42},
|
||||
{"bar", 2},
|
||||
{"fail", float32(math.NaN())},
|
||||
}
|
||||
|
||||
fmt.Print("intArray: ", intArray, " → ")
|
||||
fmt.Println(uniq(intArray))
|
||||
fmt.Print("intSlice: ", intSlice, " → ")
|
||||
fmt.Println(uniq(intSlice))
|
||||
fmt.Print("stringSlice: ", stringSlice, " → ")
|
||||
fmt.Println(uniq(stringSlice))
|
||||
fmt.Print("floats: ", floats, " → ")
|
||||
fmt.Println(uniq(floats))
|
||||
fmt.Print("complexes: ", complexes, "\n → ")
|
||||
fmt.Println(uniq(complexes))
|
||||
fmt.Print("structs: ", structs, " → ")
|
||||
fmt.Println(uniq(structs))
|
||||
// Passing a non slice or array will compile put
|
||||
// then produce a run time panic:
|
||||
//a := 42
|
||||
//uniq(a)
|
||||
//uniq(nil)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue