2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,6 +1,8 @@
|
|||
{{data structure}}
|
||||
A set is a collection of elements, without duplicates and without order.
|
||||
A '''set''' is a collection of elements, without duplicates and without order.
|
||||
|
||||
|
||||
;Task:
|
||||
Show each of these set operations:
|
||||
|
||||
* Set creation
|
||||
|
|
@ -9,13 +11,21 @@ Show each of these set operations:
|
|||
* A ∩ B -- ''intersection''; a set of all elements in ''both'' set A and set B.
|
||||
* A ∖ B -- ''difference''; a set of all elements in set A, except those in set B.
|
||||
* A ⊆ B -- ''subset''; true if every element in set A is also in set B.
|
||||
* A = B -- ''equality''; true if every element of set A is in set B and vice-versa.
|
||||
* A = B -- ''equality''; true if every element of set A is in set B and vice versa.
|
||||
|
||||
<br>
|
||||
As an option, show some other set operations.
|
||||
<br>(If A ⊆ B, but A ≠ B, then A is called a true or proper subset of B, written A ⊂ B or A ⊊ B.)
|
||||
|
||||
As an option, show some other set operations. (If A ⊆ B, but A ≠ B, then A is called a true or proper subset of B, written A ⊂ B or A ⊊ B.)
|
||||
As another option, show how to modify a mutable set.
|
||||
|
||||
|
||||
One might implement a set using an [[associative array]] (with set elements as array keys and some dummy value as the values).
|
||||
One might also implement a set with a binary search tree, or with a hash table, or with an ordered array of binary bits (operated on with bitwise binary operators).
|
||||
|
||||
One might also implement a set with a binary search tree, or with a hash table, or with an ordered array of binary bits (operated on with bit-wise binary operators).
|
||||
|
||||
The basic test, m ∈ S, is [[O]](n) with a sequential list of elements, O(''log'' n) with a balanced binary search tree, or (O(1) average-case, O(n) worst case) with a hash table.
|
||||
|
||||
|
||||
{{Template:See also lists}}
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,26 +1,28 @@
|
|||
iex(101)> s = HashSet.new
|
||||
#HashSet<[]>
|
||||
iex(102)> sa = Set.put(s, :a)
|
||||
#HashSet<[:a]>
|
||||
iex(103)> sab = Set.put(sa, :b)
|
||||
#HashSet<[:b, :a]>
|
||||
iex(104)> sbc = Enum.into([:b,:c], HashSet.new)
|
||||
#HashSet<[:c, :b]>
|
||||
iex(105)> Set.member?(sa, :a)
|
||||
iex(1)> s = MapSet.new
|
||||
#MapSet<[]>
|
||||
iex(2)> sa = MapSet.put(s, :a)
|
||||
#MapSet<[:a]>
|
||||
iex(3)> sab = MapSet.put(sa, :b)
|
||||
#MapSet<[:a, :b]>
|
||||
iex(4)> sbc = Enum.into([:b, :c], MapSet.new)
|
||||
#MapSet<[:b, :c]>
|
||||
iex(5)> MapSet.member?(sab, :a)
|
||||
true
|
||||
iex(106)> Set.member?(sa, :b)
|
||||
iex(6)> MapSet.member?(sab, :c)
|
||||
false
|
||||
iex(107)> Set.union(sab, sbc)
|
||||
#HashSet<[:c, :b, :a]>
|
||||
iex(108)> Set.intersection(sab, sbc)
|
||||
#HashSet<[:b]>
|
||||
iex(109)> Set.difference(sab, sbc)
|
||||
#HashSet<[:a]>
|
||||
iex(110)> Set.disjoint?(sab, sbc)
|
||||
false
|
||||
iex(111)> Set.subset?(sa, sab)
|
||||
iex(7)> :a in sab
|
||||
true
|
||||
iex(112)> Set.subset?(sab, sa)
|
||||
iex(8)> MapSet.union(sab, sbc)
|
||||
#MapSet<[:a, :b, :c]>
|
||||
iex(9)> MapSet.intersection(sab, sbc)
|
||||
#MapSet<[:b]>
|
||||
iex(10)> MapSet.difference(sab, sbc)
|
||||
#MapSet<[:a]>
|
||||
iex(11)> MapSet.disjoint?(sab, sbc)
|
||||
false
|
||||
iex(113)> sa == sab
|
||||
iex(12)> MapSet.subset?(sa, sab)
|
||||
true
|
||||
iex(13)> MapSet.subset?(sab, sa)
|
||||
false
|
||||
iex(14)> sa == sab
|
||||
false
|
||||
|
|
|
|||
57
Task/Set/Forth/set.fth
Normal file
57
Task/Set/Forth/set.fth
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
include FMS-SI.f
|
||||
include FMS-SILib.f
|
||||
|
||||
: union {: a b -- c :}
|
||||
begin
|
||||
b each:
|
||||
while dup
|
||||
a indexOf: if 2drop else a add: then
|
||||
repeat b <free a dup sort: ; ok
|
||||
|
||||
i{ 2 5 4 3 } i{ 5 6 7 } union p: i{ 2 3 4 5 6 7 } ok
|
||||
|
||||
|
||||
: free2 ( a b -- ) <free <free ;
|
||||
: intersect {: a b | c -- c :}
|
||||
heap> 1-array2 to c
|
||||
begin
|
||||
b each:
|
||||
while dup
|
||||
a indexOf: if drop c add: else drop then
|
||||
repeat a b free2 c dup sort: ;
|
||||
|
||||
i{ 2 5 4 3 } i{ 5 6 7 } intersect p: i{ 5 } ok
|
||||
|
||||
|
||||
: diff {: a b | c -- c :}
|
||||
heap> 1-array2 to c
|
||||
begin
|
||||
a each:
|
||||
while dup
|
||||
b indexOf: if 2drop else c add: then
|
||||
repeat a b free2 c dup sort: ;
|
||||
|
||||
i{ 2 5 4 3 } i{ 5 6 7 } diff p: i{ 2 3 4 } ok
|
||||
|
||||
: subset {: a b -- flag :}
|
||||
begin
|
||||
a each:
|
||||
while
|
||||
b indexOf: if drop else false exit then
|
||||
repeat a b free2 true ;
|
||||
|
||||
i{ 2 5 4 3 } i{ 5 6 7 } subset . 0 ok
|
||||
i{ 5 6 } i{ 5 6 7 } subset . -1 ok
|
||||
|
||||
|
||||
: set= {: a b -- flag :}
|
||||
a size: b size: <> if a b free2 false exit then
|
||||
a sort: b sort:
|
||||
begin
|
||||
a each: drop b each:
|
||||
while
|
||||
<> if a b free2 false exit then
|
||||
repeat a b free2 true ;
|
||||
|
||||
i{ 5 6 } i{ 5 6 7 } set= . 0 ok
|
||||
i{ 6 5 7 } i{ 5 6 7 } set= . -1 ok
|
||||
|
|
@ -2,7 +2,14 @@ package main
|
|||
|
||||
import "fmt"
|
||||
|
||||
type set map[int]bool
|
||||
// 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
|
||||
|
|
@ -11,7 +18,7 @@ func main() {
|
|||
s2 := set{3: true, 1: true} // create set with two elements
|
||||
|
||||
// option: another way to create a set
|
||||
s3 := newSet([]int{3, 1, 4, 1, 5, 9})
|
||||
s3 := newSet(3, 1, 4, 1, 5, 9)
|
||||
|
||||
// option: output!
|
||||
fmt.Println("s0:", s0)
|
||||
|
|
@ -52,7 +59,7 @@ func main() {
|
|||
fmt.Println("s3, 3 deleted:", s3)
|
||||
}
|
||||
|
||||
func newSet(ms []int) set {
|
||||
func newSet(ms ...complex128) set {
|
||||
s := make(set)
|
||||
for _, m := range ms {
|
||||
s[m] = true
|
||||
|
|
@ -71,7 +78,7 @@ func (s set) String() string {
|
|||
return r[:len(r)-2] + "}"
|
||||
}
|
||||
|
||||
func (s set) hasElement(m int) bool {
|
||||
func (s set) hasElement(m complex128) bool {
|
||||
return s[m]
|
||||
}
|
||||
|
||||
133
Task/Set/Go/set-2.go
Normal file
133
Task/Set/Go/set-2.go
Normal 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
60
Task/Set/Go/set-3.go
Normal 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
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ set.add('three');
|
|||
set.has(0); //=> true
|
||||
set.has(3); //=> false
|
||||
set.has('two'); // true
|
||||
set.has(Math.sqrt(4)); //=> true
|
||||
set.has(Math.sqrt(4)); //=> false
|
||||
set.has('TWO'.toLowerCase()); //=> true
|
||||
|
||||
set.size; //=> 4
|
||||
|
|
|
|||
68
Task/Set/Lua/set.lua
Normal file
68
Task/Set/Lua/set.lua
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
function emptySet() return { } end
|
||||
function insert(set, item) set[item] = true end
|
||||
function remove(set, item) set[item] = nil end
|
||||
function member(set, item) return set[item] end
|
||||
function size(set)
|
||||
local result = 0
|
||||
for _ in pairs(set) do result = result + 1 end
|
||||
return result
|
||||
end
|
||||
function fromTable(tbl) -- ignore the keys of tbl
|
||||
local result = { }
|
||||
for _, val in pairs(tbl) do
|
||||
result[val] = true
|
||||
end
|
||||
return result
|
||||
end
|
||||
function toArray(set)
|
||||
local result = { }
|
||||
for key in pairs(set) do
|
||||
table.insert(result, key)
|
||||
end
|
||||
return result
|
||||
end
|
||||
function printSet(set)
|
||||
print(table.concat(toArray(set), ", "))
|
||||
end
|
||||
function union(setA, setB)
|
||||
local result = { }
|
||||
for key, _ in pairs(setA) do
|
||||
result[key] = true
|
||||
end
|
||||
for key, _ in pairs(setB) do
|
||||
result[key] = true
|
||||
end
|
||||
return result
|
||||
end
|
||||
function intersection(setA, setB)
|
||||
local result = { }
|
||||
for key, _ in pairs(setA) do
|
||||
if setB[key] then
|
||||
result[key] = true
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
function difference(setA, setB)
|
||||
local result = { }
|
||||
for key, _ in pairs(setA) do
|
||||
if not setB[key] then
|
||||
result[key] = true
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
function subset(setA, setB)
|
||||
for key, _ in pairs(setA) do
|
||||
if not setB[key] then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
function properSubset(setA, setB)
|
||||
return subset(setA, setB) and (size(setA) ~= size(setB))
|
||||
end
|
||||
function equals(setA, setB)
|
||||
return subset(setA, setB) and (size(setA) == size(setB))
|
||||
end
|
||||
|
|
@ -1,56 +1,56 @@
|
|||
/*REXX program demonstrates some common SET functions. */
|
||||
truth.0='false'; truth.1='true' /*common names for truth table. */
|
||||
set.= /*order of sets isn't important. */
|
||||
/*REXX program demonstrates some common SET functions. */
|
||||
truth.0= 'false'; truth.1= "true" /*two common names for a truth table. */
|
||||
set.= /*the order of sets isn't important. */
|
||||
|
||||
call setAdd 'prime',2 3 2 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
|
||||
call setSay 'prime' /*a small set of primes (numbers).*/
|
||||
call setSay 'prime' /*a small set of some prime numbers. */
|
||||
|
||||
call setAdd 'emirp',97 97 89 83 79 73 71 67 61 59 53 47 43 41 37 31 29 23 19 17 13 11 7 5 3 2
|
||||
call setSay 'emirp' /*a small set of baclward primes. */
|
||||
call setSay 'emirp' /*a small set of backward primes. */
|
||||
|
||||
call setAdd 'happy',1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 100 94 97 97 97 97 97
|
||||
call setSay 'happy' /*a small set of happy numbers. */
|
||||
call setSay 'happy' /*a small set of some happy numbers. */
|
||||
|
||||
do j=11 to 100 by 10 /*see if PRIME contains some nums*/
|
||||
call setHas 'prime',j
|
||||
say ' prime contains' j":" truth.result
|
||||
do j=11 to 100 by 10 /*see if PRIME contains some numbers. */
|
||||
call setHas 'prime', j
|
||||
say ' prime contains' j":" truth.result
|
||||
end /*j*/
|
||||
|
||||
call setUnion 'prime','happy','eweion'; call setSay 'eweion'
|
||||
call setCommon 'prime','happy','common'; call setSay 'common'
|
||||
call setDiff 'prime','happy','diff' ; call setSay 'diff'
|
||||
call setSubset 'prime','happy' ; say ' prime is a subset of happy:' truth.result
|
||||
call setEqual 'prime','emirp' ; say ' prime is equal to emirp:' truth.result
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────subroutines─────────────────────────*/
|
||||
setHas: procedure expose set.; arg _ .,! .;return wordpos(!,set._)\==0
|
||||
setAdd: return set$('add' ,arg(1),arg(2))
|
||||
setDiff: return set$('diff' ,arg(1),arg(2),arg(3))
|
||||
setSay: return set$('say' ,arg(1),arg(2))
|
||||
setUnion: return set$('union' ,arg(1),arg(2),arg(3))
|
||||
setCommon: return set$('common',arg(1),arg(2),arg(3))
|
||||
setEqual: return set$('equal' ,arg(1),arg(2))
|
||||
setSubset: return set$('subSet',arg(1),arg(2))
|
||||
/*──────────────────────────────────set$ subroutine─────────────────────*/
|
||||
set$: procedure expose set.; arg $,_1,_2,_3; set_=set._1; t=_3; s=t; !=1
|
||||
if $=='SAY' then do; say '[set.'_1"]="set._1; return set._1; end
|
||||
if $=='UNION' then do
|
||||
call set$ 'add',_3,set._1
|
||||
call set$ 'add',_3,set._2
|
||||
return set._3
|
||||
end
|
||||
add=$=='ADD';common=$=='COMMON';diff=$=='DIFF';eq=$=='EQUAL';subset=$=='SUBSET'
|
||||
if common | diff | eq | subset then s=_2
|
||||
if add then do; set_=_2; t=_1; s=_1; end
|
||||
call setUnion 'prime','happy','eweion'; call setSay 'eweion' /* (sic). */
|
||||
call setCommon 'prime','happy','common'; call setSay 'common'
|
||||
call setDiff 'prime','happy','diff' ; call setSay 'diff'; _=left('', 12)
|
||||
call setSubset 'prime','happy' ; say _ 'prime is a subset of happy:' truth.result
|
||||
call setEqual 'prime','emirp' ; say _ 'prime is equal to emirp:' truth.result
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
setHas: procedure expose set.; arg _ .,! .; return wordpos(!, set._)\==0
|
||||
setAdd: return set$('add' , arg(1), arg(2))
|
||||
setDiff: return set$('diff' , arg(1), arg(2), arg(3))
|
||||
setSay: return set$('say' , arg(1), arg(2))
|
||||
setUnion: return set$('union' , arg(1), arg(2), arg(3))
|
||||
setCommon: return set$('common' , arg(1), arg(2), arg(3))
|
||||
setEqual: return set$('equal' , arg(1), arg(2))
|
||||
setSubset: return set$('subSet' , arg(1), arg(2))
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
set$: procedure expose set.; arg $,_1,_2,_3; set_=set._1; t=_3; s=t; !=1
|
||||
if $=='SAY' then do; say "[set."_1']= 'set._1; return set._1; end
|
||||
if $=='UNION' then do
|
||||
call set$ 'add', _3, set._1
|
||||
call set$ 'add', _3, set._2
|
||||
return set._3
|
||||
end
|
||||
add=$=='ADD'; common=$=='COMMON'; diff=$=='DIFF'; eq=$=='EQUAL'; subset=$=='SUBSET'
|
||||
if common | diff | eq | subset then s=_2
|
||||
if add then do; set_=_2; t=_1; s=_1; end
|
||||
|
||||
do j=1 for words(set_); _=word(set_,j); has=wordpos(_,set.s)\==0
|
||||
if (add & \has) |,
|
||||
(common & has) |,
|
||||
(diff & \has) then set.t=space(set.t _)
|
||||
if (eq | subset) & \has then return 0
|
||||
end /*j*/
|
||||
do j=1 for words(set_); _=word(set_, j); has=wordpos(_, set.s)\==0
|
||||
if (add & \has) |,
|
||||
(common & has) |,
|
||||
(diff & \has) then set.t=space(set.t _)
|
||||
if (eq | subset) & \has then return 0
|
||||
end /*j*/
|
||||
|
||||
if subset then return 1
|
||||
if eq then if arg()>3 then return 1
|
||||
else return set$('equal',_2,_1,1)
|
||||
return set.t
|
||||
if subset then return 1
|
||||
if eq then if arg()>3 then return 1
|
||||
else return set$('equal', _2, _1, 1)
|
||||
return set.t
|
||||
|
|
|
|||
70
Task/Set/Run-BASIC/set.run
Normal file
70
Task/Set/Run-BASIC/set.run
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
A$ = "apple cherry elderberry grape"
|
||||
B$ = "banana cherry date elderberry fig"
|
||||
C$ = "apple cherry elderberry grape orange"
|
||||
D$ = "apple cherry elderberry grape"
|
||||
E$ = "apple cherry elderberry"
|
||||
M$ = "banana"
|
||||
|
||||
print "A = ";A$
|
||||
print "B = ";B$
|
||||
print "C = ";C$
|
||||
print "D = ";D$
|
||||
print "E = ";E$
|
||||
print "M = ";M$
|
||||
|
||||
if instr(A$,M$) = 0 then a$ = "not "
|
||||
print "M is ";a$; "an element of Set A"
|
||||
a$ = ""
|
||||
if instr(B$,M$) = 0 then a$ = "not "
|
||||
print "M is ";a$; "an element of Set B"
|
||||
|
||||
un$ = A$ + " "
|
||||
for i = 1 to 5
|
||||
if instr(un$,word$(B$,i)) = 0 then un$ = un$ + word$(B$,i) + " "
|
||||
next i
|
||||
print "union(A,B) = ";un$
|
||||
|
||||
for i = 1 to 5
|
||||
if instr(A$,word$(B$,i)) <> 0 then ins$ = ins$ + word$(B$,i) + " "
|
||||
next i
|
||||
print "Intersection(A,B) = ";ins$
|
||||
|
||||
for i = 1 to 5
|
||||
if instr(B$,word$(A$,i)) = 0 then dif$ = dif$ + word$(A$,i) + " "
|
||||
next i
|
||||
print "Difference(A,B) = ";dif$
|
||||
|
||||
a = subs(A$,B$,"AB")
|
||||
a = subs(A$,C$,"AC")
|
||||
a = subs(A$,D$,"AD")
|
||||
a = subs(A$,E$,"AE")
|
||||
|
||||
a = eqs(A$,B$,"AB")
|
||||
a = eqs(A$,C$,"AC")
|
||||
a = eqs(A$,D$,"AD")
|
||||
a = eqs(A$,E$,"AE")
|
||||
end
|
||||
|
||||
function subs(a$,b$,sets$)
|
||||
for i = 1 to 5
|
||||
if instr(b$,word$(a$,i)) <> 0 then subs = subs + 1
|
||||
next i
|
||||
if subs = 4 then
|
||||
print left$(sets$,1);" is a subset of ";right$(sets$,1)
|
||||
else
|
||||
print left$(sets$,1);" is not a subset of ";right$(sets$,1)
|
||||
end if
|
||||
end function
|
||||
|
||||
function eqs(a$,b$,sets$)
|
||||
for i = 1 to 5
|
||||
if word$(a$,i) <> "" then a = a + 1
|
||||
if word$(b$,i) <> "" then b = b + 1
|
||||
if instr(b$,word$(a$,i)) <> 0 then c = c + 1
|
||||
next i
|
||||
if (a = b) and (a = c) then
|
||||
print left$(sets$,1);" is equal ";right$(sets$,1)
|
||||
else
|
||||
print left$(sets$,1);" is not equal ";right$(sets$,1)
|
||||
end if
|
||||
end function
|
||||
Loading…
Add table
Add a link
Reference in a new issue