Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -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)
}

View file

@ -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]
}

View file

@ -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]
}

View file

@ -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)
}

View file

@ -1,2 +1 @@
NoDupes[input_List] := Split[Sort[input]][[All, 1]]
NoDupes[{0, 2, 1, 4, 2, 0, 3, 1, 1, 1, 0, 3}]
Union[{0, 2, 1, 4, 2, 0, 3, 1, 1, 1, 0, 3}]

View file

@ -1,7 +1,11 @@
declare t(20) fixed initial (1, 5, 6, 2, 1, 7,
5, 22, 4, 19, 1, 1, 6, 6, 6, 8, 9, 10, 11, 12);
*process mar(1,72);
remdup: Proc options(main);
declare t(20) fixed initial (6, 6, 1, 5, 6, 2, 1, 7,
5, 22, 4, 19, 1, 1, 6, 8, 9, 10, 11, 12);
declare (i, j, k, n, e) fixed;
put skip list ('Input:');
put edit ((t(k) do k = 1 to hbound(t))) (skip,20(f(3)));
n = hbound(t,1);
i = 0;
outer:
@ -15,4 +19,5 @@ outer:
end;
put skip list ('Unique elements are:');
put edit ((t(k) do k = 1 to i)) (skip, f(11));
put edit ((t(k) do k = 1 to i)) (skip,20(f(3)));
end;

View file

@ -1,5 +1,5 @@
member1(X,[H|_]) :- X==H,!.
member1(X,[_|T]) :- member1_(X,T).
member1(X,[_|T]) :- member1(X,T).
distinct([],[]).
distinct([H|T],C) :- member1(H,T),!, distinct(T,C).

View file

@ -1,3 +1,7 @@
import itertools
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = [k for k,g in itertools.groupby(sorted(items))]
unique = []
helperset = set()
for x in items:
if x not in helperset:
unique.append(x)
helperset.add(x)

View file

@ -1,5 +1,3 @@
import itertools
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = []
for x in items:
if x not in unique:
unique.append(x)
unique = [k for k,g in itertools.groupby(sorted(items))]

View file

@ -0,0 +1,5 @@
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = []
for x in items:
if x not in unique:
unique.append(x)

View file

@ -0,0 +1,2 @@
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
p ary.uniq # => [1, 2, "redundant", [1, 2, 3]]

View file

@ -0,0 +1,26 @@
class Array
# used Hash
def uniq1
each_with_object({}) {|elem, h| h[elem] = true}.keys
end
# sort (requires comparable)
def uniq2
sorted = sort
pre = sorted.first
sorted.each_with_object([pre]){|elem, uniq| uniq << (pre = elem) if elem != pre}
end
# go through the list
def uniq3
each_with_object([]) {|elem, uniq| uniq << elem unless uniq.include?(elem)}
end
end
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
p ary.uniq1 #=> [1, 2, "redundant", [1, 2, 3]]
p ary.uniq2 rescue nil # Error (not comparable)
p ary.uniq3 #=> [1, 2, "redundant", [1, 2, 3]]
ary = [1,2,3,7,6,5,2,3,4,5,6,1,1,1]
p ary.uniq1 #=> [1, 2, 3, 7, 6, 5, 4]
p ary.uniq2 #=> [1, 2, 3, 4, 5, 6, 7]
p ary.uniq3 #=> [1, 2, 3, 7, 6, 5, 4]

View file

@ -0,0 +1,15 @@
def unique(array)
pure = Array.new
for i in array
flag = false
for j in pure
flag = true if j==i
end
pure << i unless flag
end
return pure
end
unique ["hi","hey","hello","hi","hey","heyo"] # => ["hi", "hey", "hello", "heyo"]
unique [1,2,3,4,1,2,3,5,1,2,3,4,5] # => [1,2,3,4,5]

View file

@ -1,3 +0,0 @@
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
uniq_ary = ary.uniq
# => [1, 2, "redundant", [1, 2, 3]]