September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

81
Task/Set/Aime/set.aime Normal file
View file

@ -0,0 +1,81 @@
record
union(record a, record b)
{
record c;
r_copy(c, a);
r_wcall(b, r_add, 1, 2, c);
return c;
}
record
intersection(record a, record b)
{
record c;
text s;
for (s in a) {
if (r_key(b, s)) {
c[s] = 0;
}
}
return c;
}
record
difference(record a, record b)
{
record c;
r_copy(c, a);
r_vcall(b, r_resign, 1, c);
return c;
}
integer
subset(record a, record b)
{
integer e;
text s;
e = 1;
for (s in a) {
if (!r_key(b, s)) {
e = 0;
break;
}
}
return e;
}
integer
equal(record a, record b)
{
return subset(a, b) && subset(b, a);
}
integer
main(void)
{
record a, b;
text s;
r_fit(a, "apple", 0, "cherry", 0, "grape", 0);
r_fit(b, "banana", 0, "cherry", 0, "date", 0);
s = "banana";
o_(" ", s, " is ", r_key(a, s) ? "" : "not ", "an element of A\n");
o_(" ", s, " is ", r_key(b, s) ? "" : "not ", "an element of B\n");
r_vcall(union(a, b), o_, 1, " ");
o_newline();
r_vcall(intersection(a, b), o_, 1, " ");
o_newline();
r_vcall(difference(a, b), o_, 1, " ");
o_newline();
o_(" ", subset(a, b) ? "yes" : "no", "\n");
o_(" ", equal(a, b) ? "yes" : "no", "\n");
return 0;
}

View file

@ -0,0 +1,18 @@
shared void run() {
value a = set {1, 2, 3};
value b = set {3, 4, 5};
value union = a | b;
value intersection = a & b;
value difference = a ~ b;
value subset = a.subset(b);
value equality = a == b;
print("set a: ``a``
set b: ``b``
1 in a? ``1 in a``
a | b: ``union``
a & b: ``intersection``
a ~ b: ``difference``
a subset of b? ``subset``
a == b? ``equality``");
}

View file

@ -1,21 +1,152 @@
void main() {
import std.stdio, std.algorithm, std.range;
module set;
import std.typecons : Tuple, tuple;
struct Set(V) { // Limited set of V-type elements // here 'this' is named A, s is B, v V-type item
// Not true sets, items can be repeated, but must be sorted.
auto s1 = [1, 2, 3, 4, 5, 6].assumeSorted;
auto s2 = [2, 5, 6, 3, 4, 8].sort(); // [2,3,4,5,6,8].
auto s3 = [1, 2, 5].assumeSorted;
protected V[] array;
assert(s1.canFind(4)); // Linear search.
assert(s1.contains(4)); // Binary search.
assert(s1.setUnion(s2).equal([1,2,2,3,3,4,4,5,5,6,6,8]));
assert(s1.setIntersection(s2).equal([2, 3, 4, 5, 6]));
assert(s1.setDifference(s2).equal([1]));
assert(s1.setSymmetricDifference(s2).equal([1, 8]));
assert(s3.setDifference(s1).empty); // It's a subset.
assert(!s1.equal(s2));
this(const Set s) { // construct A by copy of B
array = s.array.dup;
}
this(V[] arg...){ // construct A with items
foreach(v; arg) if (v.isNotIn(array)) array ~= v;
}
enum : Set { empty = Set() } // ∅
ref Set opAssign()(const Set s) { // A = B
array = s.array.dup;
return this;
}
bool opBinaryRight(string op : "in")(const V v) const { // v ∈ A
return v.isIn(array);
}
ref Set opOpAssign(string op)(const V v) if (op == "+" || op == "|") { // A += {v} // + = = |
if (v.isIn(array)) return this;
array ~= v;
return this;
}
ref Set opOpAssign(string op)(const Set s) if (op == "+" || op == "|") { // A += B
foreach(x; s.array) if (x.isNotIn(array)) array ~= x;
return this;
}
Set opBinary(string op)(const V v) const if (op == "+" || op == "|"){ // A + {v}
Set result = this;
result += v;
return result;
}
Set opBinaryRight(string op)(const V v) const if (op == "+" || op == "|") { // {v} + A
Set result = this;
result += v;
return result;
}
Set opBinary(string op)(const Set s) const if (op == "+" || op == "|") { // A + B
Set result = this;
result += s;
return result;
}
Set opBinary(string op : "&")(const Set s) const{ // A ∩ B // ∩ = &
Set result;
foreach(x; array) if(x.isIn(s.array)) result += x;
return result;
}
ref Set opOpAssign(string op : "&")(const Set s) { // A ∩= B
return this(this & s);
}
Set opBinary(string op : "^")(const Set s) const { // (A B) - (A ∩ B) // = A ^ B
Set result;
foreach(x; array) if (x.isNotIn(s.array)) result += x;
foreach(x; s.array) if(x.isNotIn(array)) result += x;
return result;
}
ref opOpAssign(string op : "^")(const Set s) {
return this = this ^ s;
}
Set opBinary(string op : "-")(const Set s) const { // A - B
Set r;
foreach(x; array) if(x.isNot(s.array)) r += x;
return r;
}
ref Set opOpAssign(string op : "-")(const Set s) { // A -= B
return this = this - s;
}
Set!(Tuple!(V,U)) opBinary(U, string op : "*")(const Set!U s) const { // A × B = { (x, y) | ∀x ∈ A ∧ ∀y ∈ B }
Set!(Tuple!(V, U)) r;
foreach(x; array) foreach(y; s.array) r += tuple(x, y);
return r;
}
bool isEmpty() const { return !array.length;} // A ≟ ∅
bool opBinary(string op : "in")(const Set s) const { // A ⊂ s
foreach(v; array) if(v.isNotIn(s.array)) return false;
return true;
}
bool opEquals(const Set s) const { // A ≟ B
if (array.length != s.array.length) return false;
return this in s;
}
T[] array() const @property { return array.dup;}
auto s4 = [[1, 4, 7, 8], [1, 7], [1, 7, 8], [4], [7]];
const s5 = [1, 1, 1, 4, 4, 7, 7, 7, 7, 8, 8];
assert(s4.nWayUnion.equal(s5));
}
Set!(Tuple!(T, T)) sqr(T)(const Set!T s) { return s * s; } // A²
auto pow(T, uint n : 0)(const Set!T s) { // A ^ 0
return Set!T.empty;
}
auto pow(T, uint n : 1)(const Set!T s) { // A ^ 1 = A
return s;
}
auto pow(T, uint n : 2)(const Set!T s) { // A ^ 2 (=A²)
return sqr!T(s);
}
auto pow(T, uint n)(const Set!T s) if(n % 2) { // if n Odd, A^n = A * (A^(n/2))²
return s * sqr!T(pow!(T, n/2)(s));
}
auto pow(T, uint n)(const Set!T s) if(!(n % 2)) { // if n Even, A^n = (A^(n/2))²
return sqr!T(pow!(T, n/2)(s));
}
size_t Card(T)(const Set!T s) {return s.length; } // Card(A)
Set!(Set!T) power(T)(Set!T s) { // ∀B ∈ P(A) ⇒ B ⊂ A
Set!(Set!T) ret;
foreach(e; s.array) {
Set!(Set!T) rs;
foreach(x; ret.array) {
x += e;
rs += x;
}
ret += rs;
}
return ret;
}
bool isIn(T)(T x, T[] array){
foreach(a; array) if(a == x) return true;
return false;
}
bool isNotIn(T)(T x, T[] array){
foreachj(a; array) if(a == x) return false;
return true;
}

View file

@ -0,0 +1,36 @@
// version 1.0.6
fun main(args: Array<String>) {
val fruits = setOf("apple", "pear", "orange", "banana")
println("fruits : $fruits")
val fruits2 = setOf("melon", "orange", "lemon", "gooseberry")
println("fruits2 : $fruits2\n")
println("fruits contains 'banana' : ${"banana" in fruits}")
println("fruits2 contains 'elderberry' : ${"elderbury" in fruits2}\n")
println("Union : ${fruits.union(fruits2)}")
println("Intersection : ${fruits.intersect(fruits2)}")
println("Difference : ${fruits.minus(fruits2)}\n")
println("fruits2 is a subset of fruits : ${fruits.containsAll(fruits2)}\n")
val fruits3 = fruits
println("fruits3 : $fruits3\n")
var areEqual = fruits.containsAll(fruits2) && fruits3.containsAll(fruits)
println("fruits2 and fruits are equal : $areEqual")
areEqual = fruits.containsAll(fruits3) && fruits3.containsAll(fruits)
println("fruits3 and fruits are equal : $areEqual\n")
val fruits4 = setOf("apple", "orange")
println("fruits4 : $fruits4\n")
var isProperSubset = fruits.containsAll(fruits3) && !fruits3.containsAll(fruits)
println("fruits3 is a proper subset of fruits : $isProperSubset")
isProperSubset = fruits.containsAll(fruits4) && !fruits4.containsAll(fruits)
println("fruits4 is a proper subset of fruits : $isProperSubset\n")
val fruits5 = mutableSetOf("cherry", "blueberry", "raspberry")
println("fruits5 : $fruits5\n")
fruits5 += "guava"
println("fruits5 + 'guava' : $fruits5")
println("fruits5 - 'cherry' : ${fruits5 - "cherry"}")
}

32
Task/Set/OoRexx/set.rexx Normal file
View file

@ -0,0 +1,32 @@
-- Set creation
-- Using the OF method
s1 = .set~of(1, 2, 3, 4, 5, 6)
-- Explicit addition of individual items
s2 = .set~new
s2~put(2)
s2~put(4)
s2~put(6)
-- group addition
s3 = .set~new
s3~putall(.array~of(1, 3, 5))
-- Test m ? S -- "m is an element in set S"
say s1~hasindex(1) s3~hasindex(2) -- "1 0", which is "true" and "false"
-- A ? B -- union; a set of all elements either in set A or in set B.
s4 = s2~union(s3) -- {1, 2, 3, 4, 5, 6}
Call show 's4',s4
-- A ? B -- intersection; a set of all elements in both set A and set B.
s5 = s1~intersection(s2) -- {2, 4, 6}
Call show 's5',s5
-- A ? B -- difference; a set of all elements in set A, except those in set B.
s6 = s1~difference(s2) -- {1, 3, 5}
Call show 's6',s6
-- A ? B -- subset; true if every element in set A is also in set B.
say s1~subset(s2) s2~subset(s1) -- "0 1"
-- A = B -- equality; true if every element of set A is in set B and vice-versa.
-- No direct equivalence method, but the XOR method can be used to determine this
say s1~xor(s4)~isempty -- true
Exit
show: Procedure
Use Arg set_name,set
Say set_name':' set~makearray~makestring((LINE),',')
return

62
Task/Set/Phix/set-1.phix Normal file
View file

@ -0,0 +1,62 @@
sequence set1 = {1,2,3},
set2 = {3,4,5}
function element(object x, sequence set)
return find(x,set)!=0
end function
function union(sequence set1, sequence set2)
for i=1 to length(set2) do
if not element(set2[i],set1) then
set1 = append(set1,set2[i])
end if
end for
return set1
end function
function intersection(sequence set1, sequence set2)
sequence res = {}
for i=1 to length(set1) do
if element(set1[i],set2) then
res = append(res,set1[i])
end if
end for
return res
end function
function difference(sequence set1, sequence set2)
sequence res = {}
for i=1 to length(set1) do
if not element(set1[i],set2) then
res = append(res,set1[i])
end if
end for
return res
end function
function subset(sequence set1, sequence set2)
for i=1 to length(set1) do
if not element(set1[i],set2) then
return false
end if
end for
return true
end function
function equality(sequence set1, sequence set2)
if length(set1)!=length(set2) then
return false
end if
return subset(set1,set2)
end function
--test code:
?element(3,set1) -- 1
?element(4,set1) -- 0
?union(set1,set2) -- {1,2,3,4,5}
?intersection(set1,set2) -- {3}
?difference(set1,set2) -- {1,2}
?subset(set1,set2) -- 0
?subset({1,2},set1) -- 1
?equality(set1,set2) -- 0
?equality(set1,{3,1,2}) -- 1

96
Task/Set/Phix/set-2.phix Normal file
View file

@ -0,0 +1,96 @@
integer set1 = new_dict(),
set2 = new_dict()
setd(3,0,set1)
setd(1,0,set1)
setd(2,0,set1)
setd(5,0,set2)
setd(3,0,set2)
setd(4,0,set2)
function element(object x, integer set)
return getd_index(x,set)!=0
end function
function u_visitor(object key, object data, object user_data)
integer {union_set,set2} = user_data
if set2=0
or not element(key,union_set) then
setd(key,data,union_set)
end if
return 1
end function
function union(integer set1, integer set2)
integer union_set = new_dict()
traverse_dict(routine_id("u_visitor"),{union_set,0},set1)
traverse_dict(routine_id("u_visitor"),{union_set,set2},set2)
return union_set
end function
function i_visitor(object key, object data, object user_data)
integer {union_set,set2} = user_data
if element(key,set2) then
setd(key,data,union_set)
end if
return 1
end function
function intersection(integer set1, integer set2)
integer union_set = new_dict()
traverse_dict(routine_id("i_visitor"),{union_set,set2},set1)
return union_set
end function
function d_visitor(object key, object data, object user_data)
integer {union_set,set2} = user_data
if not element(key,set2) then
setd(key,data,union_set)
end if
return 1
end function
function difference(integer set1, integer set2)
integer union_set = new_dict()
traverse_dict(routine_id("d_visitor"),{union_set,set2},set1)
return union_set
end function
integer res
function s_visitor(object key, object data, object user_data)
integer set2 = user_data
if not element(key,set2) then
res = 0
return 0 -- cease traversal
end if
return 1
end function
function subset(integer set1, integer set2)
res = 1
traverse_dict(routine_id("s_visitor"),set2,set1)
return res
end function
function equality(integer set1, integer set2)
if dict_size(set1)!=dict_size(set2) then
return false
end if
return subset(set1,set2)
end function
include builtins/map.e -- for keys()
-- matching test code:
?element(3,set1) -- 1
?element(4,set1) -- 0
?keys(union(set1,set2)) -- {1,2,3,4,5}
?keys(intersection(set1,set2)) -- {3}
?keys(difference(set1,set2)) -- {1,2}
?subset(set1,set2) -- 0
integer set3 = new_dict()
setd(2,0,set3)
setd(1,0,set3)
?subset(set3,set1) -- 1
?equality(set1,set2) -- 0
setd(3,0,set3)
?equality(set1,set3) -- 1

View file

@ -0,0 +1,17 @@
[System.Collections.Generic.HashSet[object]]$set1 = 1..4
[System.Collections.Generic.HashSet[object]]$set2 = 3..6
# Operation + Definition + Result
#--------------------------------+---------------------+-------------------------
$set1.UnionWith($set2) # Union $set1 = 1, 2, 3, 4, 5, 6
$set1.IntersectWith($set2) # Intersection $set1 = 3, 4
$set1.ExceptWith($set2) # Difference $set1 = 1, 2
$set1.SymmetricExceptWith($set2) # Symmetric difference $set1 = 1, 2, 6, 5
$set1.IsSupersetOf($set2) # Test superset False
$set1.IsSubsetOf($set2) # Test subset False
$set1.Equals($set2) # Test equality False
$set1.IsProperSupersetOf($set2) # Test proper superset False
$set1.IsProperSubsetOf($set2) # Test proper subset False
5 -in $set1 # Test membership False
7 -notin $set1 # Test non-membership True

83
Task/Set/SQL/set.sql Normal file
View file

@ -0,0 +1,83 @@
-- set of numbers is a table
-- create one set with 3 elements
create table myset1 (element number);
insert into myset1 values (1);
insert into myset1 values (2);
insert into myset1 values (3);
commit;
-- check if 1 is an element
select 'TRUE' BOOL from dual
where 1 in
(select element from myset1);
-- create second set with 3 elements
create table myset2 (element number);
insert into myset2 values (1);
insert into myset2 values (5);
insert into myset2 values (6);
commit;
-- union sets
select element from myset1
union
select element from myset2;
-- intersection
select element from myset1
intersect
select element from myset2;
-- difference
select element from myset1
minus
select element from myset2;
-- subset
-- change myset2 to only have 1 as element
delete from myset2 where not element = 1;
commit;
-- check if myset2 subset of myset1
select 'TRUE' BOOL from dual
where 0 = (select count(*) from
(select element from myset2
minus
select element from myset1));
-- equality
-- change myset1 to only have 1 as element
delete from myset1 where not element = 1;
commit;
-- check if myset2 subset of myset1 and
-- check if myset1 subset of myset2 and
select 'TRUE' BOOL from dual
where
0 = (select count(*) from
(select element from myset2
minus
select element from myset1)) and
0 =
(select count(*) from
(select element from myset1
minus
select element from myset2));

202
Task/Set/Simula/set.simula Normal file
View file

@ -0,0 +1,202 @@
SIMSET
BEGIN
! WE DON'T SUBCLASS HEAD BUT USE COMPOSITION FOR CLASS SET ;
CLASS SET;
BEGIN
PROCEDURE ADD(E); REF(ELEMENT) E;
BEGIN
IF NOT ISIN(E, THIS SET) THEN E.CLONE.INTO(H);
END**OF**ADD;
BOOLEAN PROCEDURE EMPTY; EMPTY := H.EMPTY;
REF(LINK) PROCEDURE FIRST; FIRST :- H.FIRST;
REF(HEAD) H;
H :- NEW HEAD;
END**OF**SET;
! WE SUBCLASS LINK FOR THE ELEMENTS CONTAINED IN THE SET ;
LINK CLASS ELEMENT;
VIRTUAL:
PROCEDURE ISEQUAL IS
BOOLEAN PROCEDURE ISEQUAL(OTHER); REF(ELEMENT) OTHER;;
PROCEDURE REPR IS
TEXT PROCEDURE REPR;;
PROCEDURE REPR IS
REF(ELEMENT) PROCEDURE CLONE;;
BEGIN
END**OF**ELEMENT;
REF(SET) PROCEDURE UNION(S1, S2); REF(SET) S1, S2;
BEGIN REF(SET) SU, S;
SU :- NEW SET;
FOR S :- S1, S2 DO
BEGIN
IF NOT S.EMPTY THEN
BEGIN REF(ELEMENT) E;
E :- S.FIRST;
WHILE E =/= NONE DO
BEGIN SU.ADD(E); E :- E.SUC;
END;
END;
END;
UNION :- SU;
END**OF**UNION;
REF(SET) PROCEDURE INTERSECTION(S1, S2); REF(SET) S1, S2;
BEGIN REF(SET) SI;
SI :- NEW SET;
IF NOT S1.EMPTY THEN
BEGIN REF(ELEMENT) E;
E :- S1.FIRST;
WHILE E =/= NONE DO
BEGIN IF ISIN(E, S2) THEN SI.ADD(E); E :- E.SUC;
END;
END;
INTERSECTION :- SI;
END**OF**INTERSECTION;
REF(SET) PROCEDURE MINUS(S1, S2); REF(SET) S1, S2;
BEGIN REF(SET) SM;
SM :- NEW SET;
IF NOT S1.EMPTY THEN
BEGIN REF(ELEMENT) E;
E :- S1.FIRST;
WHILE E =/= NONE DO
BEGIN IF NOT ISIN(E, S2) THEN SM.ADD(E); E :- E.SUC;
END;
END;
MINUS :- SM;
END**OF**MINUS;
BOOLEAN PROCEDURE ISSUBSET(S1, S2); REF(SET) S1, S2;
BEGIN BOOLEAN B;
B := TRUE;
IF NOT S1.EMPTY THEN
BEGIN REF(ELEMENT) E;
E :- S1.FIRST;
WHILE B AND E =/= NONE DO
BEGIN
B := ISIN(E, S2);
E :- E.SUC;
END;
END;
ISSUBSET := B;
END**OF**ISSUBSET;
BOOLEAN PROCEDURE ISEQUAL(S1, S2); REF(SET) S1, S2;
BEGIN
ISEQUAL := ISSUBSET(S1, S2) AND THEN ISSUBSET(S2, S1)
END**OF**ISEQUAL;
BOOLEAN PROCEDURE ISIN(ELE,S); REF(ELEMENT) ELE; REF(SET) S;
BEGIN
REF(ELEMENT) E; BOOLEAN FOUND;
IF NOT S.EMPTY THEN
BEGIN
E :- S.FIRST;
FOUND := E.ISEQUAL(ELE);
WHILE NOT FOUND AND E =/= NONE DO
BEGIN FOUND := E.ISEQUAL(ELE); E :- E.SUC;
END;
END;
ISIN := FOUND
END**OF**ISIN;
PROCEDURE OUTSET(S); REF(SET) S;
BEGIN
REF(ELEMENT) E;
OUTCHAR('{');
IF NOT S.EMPTY THEN
BEGIN
E :- S.FIRST; OUTTEXT(E.REPR);
FOR E :- E.SUC WHILE E =/= NONE DO
BEGIN OUTTEXT(", "); OUTTEXT(E.REPR);
END;
END;
OUTCHAR('}');
END**OF**OUTSET;
COMMENT ============== EXAMPLE USING SETS OF NUMBERS ============== ;
ELEMENT CLASS NUMBER(N); INTEGER N;
BEGIN
BOOLEAN PROCEDURE ISEQUAL(OTHER); REF(ELEMENT) OTHER;
ISEQUAL := N = OTHER QUA NUMBER.N;
TEXT PROCEDURE REPR;
BEGIN TEXT T; INTEGER I;
T :- BLANKS(20); T.PUTINT(N);
T.SETPOS(1);
WHILE T.GETCHAR = ' ' DO;
REPR :- T.SUB(T.POS - 1, T.LENGTH - T.POS + 2);
END;
REF(ELEMENT) PROCEDURE CLONE;
CLONE :- NEW NUMBER(N);
END**OF**NUMBER;
PROCEDURE REPORT(S1, MSG1, S2, MSG2, S3); REF(SET) S1, S2, S3; TEXT MSG1, MSG2;
BEGIN
OUTSET(S1); OUTCHAR(' ');
OUTTEXT(MSG1); OUTCHAR(' ');
OUTSET(S2); OUTCHAR(' ');
OUTTEXT(MSG2); OUTCHAR(' ');
OUTSET(S3);
OUTIMAGE;
END**OF**REPORT;
PROCEDURE REPORTBOOL(S1, MSG1, S2, MSG2, B); REF(SET) S1, S2; TEXT MSG1, MSG2; BOOLEAN B;
BEGIN
OUTSET(S1); OUTCHAR(' ');
OUTTEXT(MSG1); OUTCHAR(' ');
OUTSET(S2); OUTCHAR(' ');
OUTTEXT(MSG2); OUTCHAR(' ');
OUTTEXT(IF B THEN "T" ELSE "F");
OUTIMAGE;
END**OF**REPORTBOOL;
PROCEDURE REPORTNUMBOOL(N1, MSG1, S1, MSG2, B); REF(ELEMENT) N1; REF(SET) S1; TEXT MSG1, MSG2; BOOLEAN B;
BEGIN
OUTTEXT(N1.REPR); OUTCHAR(' ');
OUTTEXT(MSG1); OUTCHAR(' ');
OUTSET(S1); OUTCHAR(' ');
OUTTEXT(MSG2); OUTCHAR(' ');
OUTTEXT(IF B THEN "T" ELSE "F");
OUTIMAGE;
END**OF**REPORTNUMBOOL;
REF(SET) S1, S2, S3, S4, S5;
REF(ELEMENT) E;
INTEGER I;
S1 :- NEW SET; FOR I := 1, 2, 3, 4 DO S1.ADD(NEW NUMBER(I));
S2 :- NEW SET; FOR I := 3, 4, 5, 6 DO S2.ADD(NEW NUMBER(I));
S3 :- NEW SET; FOR I := 3, 1 DO S3.ADD(NEW NUMBER(I));
S4 :- NEW SET; FOR I := 1, 2, 3, 4, 5 DO S4.ADD(NEW NUMBER(I));
S5 :- NEW SET; FOR I := 4, 3, 2, 1 DO S5.ADD(NEW NUMBER(I));
REPORT(S1, "UNION", S2, " = ", UNION(S1, S2));
REPORT(S1, "INTERSECTION", S2, " = ", INTERSECTION(S1, S2));
REPORT(S1, "MINUS", S2, " = ", MINUS(S1, S2));
REPORT(S2, "MINUS", S1, " = ", MINUS(S2, S1));
E :- NEW NUMBER(2);
REPORTNUMBOOL(E, "IN", S1, " = ", ISIN(E, S1));
E :- NEW NUMBER(10);
REPORTNUMBOOL(E, "NOT IN", S1, " = ", NOT ISIN(E, S1));
REPORTBOOL(S1, "IS SUBSET OF", S1, " = ", ISSUBSET(S1, S1));
REPORTBOOL(S3, "IS SUBSET OF", S1, " = ", ISSUBSET(S3, S1));
REPORTBOOL(S4, "IS SUPERSET OF", S1, " = ", ISSUBSET(S1, S4));
REPORTBOOL(S1, "IS EQUAL TO", S2, " = ", ISEQUAL(S1, S2));
REPORTBOOL(S2, "IS EQUAL TO", S2, " = ", ISEQUAL(S2, S2));
REPORTBOOL(S1, "IS EQUAL TO", S5, " = ", ISEQUAL(S1, S5));
END.

21
Task/Set/Zkl/set.zkl Normal file
View file

@ -0,0 +1,21 @@
var [const] unique = Utils.Helpers.listUnique;
class Set {
fcn init { var [const] set = (vm.arglist.copy() : unique(_)) }
fcn holds(x) { set.holds(x) }
fcn union(setB) { self(set.xplode(),setB.set.xplode()) }
fcn intersection(setB){ sb:=setB.set;
C:=self(); sc:=C.set;
foreach x in (set){ if (sb.holds(x)) sc.append(x) }
C
}
fcn diff(setB){ C:=self(); C.set.extend(set);
setB.set.pump(Void,C.set.remove);
C
}
fcn isSubset(setB){ sb:=setB.set;
set.pump(Void,'wrap(x){
if (not sb.holds(x)) return(Void.Stop,False); True
})
}
fcn __opEQ(setB) { ((set.len() == setB.set.len()) and self.isSubset(setB)) }
}