Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

131
Task/Set/Go/set-1.go Normal file
View file

@ -0,0 +1,131 @@
package main
import "fmt"
// Define set as a type to hold a set of complex numbers. A type
// could be defined similarly to hold other types of elements. A common
// variation is to make a map of interface{} to represent a set of
// mixed types. Also here the map value is a bool. By always storing
// true, the code is nicely readable. A variation to use less memory
// is to make the map value an empty struct. The relative advantages
// can be debated.
type set map[complex128]bool
func main() {
// task: set creation
s0 := make(set) // create empty set
s1 := set{3: true} // create set with one element
s2 := set{3: true, 1: true} // create set with two elements
// option: another way to create a set
s3 := newSet(3, 1, 4, 1, 5, 9)
// option: output!
fmt.Println("s0:", s0)
fmt.Println("s1:", s1)
fmt.Println("s2:", s2)
fmt.Println("s3:", s3)
// task: element predicate
fmt.Printf("%v ∈ s0: %t\n", 3, s0.hasElement(3))
fmt.Printf("%v ∈ s3: %t\n", 3, s3.hasElement(3))
fmt.Printf("%v ∈ s3: %t\n", 2, s3.hasElement(2))
// task: union
b := set{4: true, 2: true}
fmt.Printf("s3 %v: %v\n", b, union(s3, b))
// task: intersection
fmt.Printf("s3 ∩ %v: %v\n", b, intersection(s3, b))
// task: difference
fmt.Printf("s3 \\ %v: %v\n", b, difference(s3, b))
// task: subset predicate
fmt.Printf("%v ⊆ s3: %t\n", b, subset(b, s3))
fmt.Printf("%v ⊆ s3: %t\n", s2, subset(s2, s3))
fmt.Printf("%v ⊆ s3: %t\n", s0, subset(s0, s3))
// task: equality
s2Same := set{1: true, 3: true}
fmt.Printf("%v = s2: %t\n", s2Same, equal(s2Same, s2))
// option: proper subset
fmt.Printf("%v ⊂ s2: %t\n", s2Same, properSubset(s2Same, s2))
fmt.Printf("%v ⊂ s3: %t\n", s2Same, properSubset(s2Same, s3))
// option: delete. it's built in.
delete(s3, 3)
fmt.Println("s3, 3 deleted:", s3)
}
func newSet(ms ...complex128) set {
s := make(set)
for _, m := range ms {
s[m] = true
}
return s
}
func (s set) String() string {
if len(s) == 0 {
return "∅"
}
r := "{"
for e := range s {
r = fmt.Sprintf("%s%v, ", r, e)
}
return r[:len(r)-2] + "}"
}
func (s set) hasElement(m complex128) bool {
return s[m]
}
func union(a, b set) set {
s := make(set)
for e := range a {
s[e] = true
}
for e := range b {
s[e] = true
}
return s
}
func intersection(a, b set) set {
s := make(set)
for e := range a {
if b[e] {
s[e] = true
}
}
return s
}
func difference(a, b set) set {
s := make(set)
for e := range a {
if !b[e] {
s[e] = true
}
}
return s
}
func subset(a, b set) bool {
for e := range a {
if !b[e] {
return false
}
}
return true
}
func equal(a, b set) bool {
return len(a) == len(b) && subset(a, b)
}
func properSubset(a, b set) bool {
return len(a) < len(b) && subset(a, b)
}

133
Task/Set/Go/set-2.go Normal file
View file

@ -0,0 +1,133 @@
package main
import (
"fmt"
"math/big"
)
func main() {
// create an empty set
var s0 big.Int
// create sets with elements
s1 := newSet(3)
s2 := newSet(3, 1)
s3 := newSet(3, 1, 4, 1, 5, 9)
// output
fmt.Println("s0:", format(s0))
fmt.Println("s1:", format(s1))
fmt.Println("s2:", format(s2))
fmt.Println("s3:", format(s3))
// element predicate
fmt.Printf("%v ∈ s0: %t\n", 3, hasElement(s0, 3))
fmt.Printf("%v ∈ s3: %t\n", 3, hasElement(s3, 3))
fmt.Printf("%v ∈ s3: %t\n", 2, hasElement(s3, 2))
// union
b := newSet(4, 2)
fmt.Printf("s3 %v: %v\n", format(b), format(union(s3, b)))
// intersection
fmt.Printf("s3 ∩ %v: %v\n", format(b), format(intersection(s3, b)))
// difference
fmt.Printf("s3 \\ %v: %v\n", format(b), format(difference(s3, b)))
// subset predicate
fmt.Printf("%v ⊆ s3: %t\n", format(b), subset(b, s3))
fmt.Printf("%v ⊆ s3: %t\n", format(s2), subset(s2, s3))
fmt.Printf("%v ⊆ s3: %t\n", format(s0), subset(s0, s3))
// equality
s2Same := newSet(1, 3)
fmt.Printf("%v = s2: %t\n", format(s2Same), equal(s2Same, s2))
// proper subset
fmt.Printf("%v ⊂ s2: %t\n", format(s2Same), properSubset(s2Same, s2))
fmt.Printf("%v ⊂ s3: %t\n", format(s2Same), properSubset(s2Same, s3))
// delete
remove(&s3, 3)
fmt.Println("s3, 3 removed:", format(s3))
}
func newSet(ms ...int) (set big.Int) {
for _, m := range ms {
set.SetBit(&set, m, 1)
}
return
}
func remove(set *big.Int, m int) {
set.SetBit(set, m, 0)
}
func format(set big.Int) string {
if len(set.Bits()) == 0 {
return "∅"
}
r := "{"
for e, l := 0, set.BitLen(); e < l; e++ {
if set.Bit(e) == 1 {
r = fmt.Sprintf("%s%v, ", r, e)
}
}
return r[:len(r)-2] + "}"
}
func hasElement(set big.Int, m int) bool {
return set.Bit(m) == 1
}
func union(a, b big.Int) (set big.Int) {
set.Or(&a, &b)
return
}
func intersection(a, b big.Int) (set big.Int) {
set.And(&a, &b)
return
}
func difference(a, b big.Int) (set big.Int) {
set.AndNot(&a, &b)
return
}
func subset(a, b big.Int) bool {
ab := a.Bits()
bb := b.Bits()
if len(ab) > len(bb) {
return false
}
for i, aw := range ab {
if aw&^bb[i] != 0 {
return false
}
}
return true
}
func equal(a, b big.Int) bool {
return a.Cmp(&b) == 0
}
func properSubset(a, b big.Int) (p bool) {
ab := a.Bits()
bb := b.Bits()
if len(ab) > len(bb) {
return false
}
for i, aw := range ab {
bw := bb[i]
if aw&^bw != 0 {
return false
}
if aw != bw {
p = true
}
}
return
}

60
Task/Set/Go/set-3.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"fmt"
"golang.org/x/tools/container/intsets"
)
func main() {
var s0, s1 intsets.Sparse // create some empty sets
s1.Insert(3) // insert an element
s2 := newSet(3, 1) // create sets with elements
s3 := newSet(3, 1, 4, 1, 5, 9)
// output
fmt.Println("s0:", &s0)
fmt.Println("s1:", &s1)
fmt.Println("s2:", s2)
fmt.Println("s3:", s3)
// element predicate
fmt.Printf("%v ∈ s0: %t\n", 3, s0.Has(3))
fmt.Printf("%v ∈ s3: %t\n", 3, s3.Has(3))
fmt.Printf("%v ∈ s3: %t\n", 2, s3.Has(2))
// union
b := newSet(4, 2)
var s intsets.Sparse
s.Union(s3, b)
fmt.Printf("s3 %v: %v\n", b, &s)
// intersection
s.Intersection(s3, b)
fmt.Printf("s3 ∩ %v: %v\n", b, &s)
// difference
s.Difference(s3, b)
fmt.Printf("s3 \\ %v: %v\n", b, &s)
// subset predicate
fmt.Printf("%v ⊆ s3: %t\n", b, b.SubsetOf(s3))
fmt.Printf("%v ⊆ s3: %t\n", s2, s2.SubsetOf(s3))
fmt.Printf("%v ⊆ s3: %t\n", &s0, s0.SubsetOf(s3))
// equality
s2Same := newSet(1, 3)
fmt.Printf("%v = s2: %t\n", s2Same, s2Same.Equals(s2))
// delete
s3.Remove(3)
fmt.Println("s3, 3 removed:", s3)
}
func newSet(ms ...int) *intsets.Sparse {
var set intsets.Sparse
for _, m := range ms {
set.Insert(m)
}
return &set
}