June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -4,39 +4,27 @@ show_sdiff(record u, record x)
record r;
text s;
r_copy(r, u);
r.copy(u);
if (r_first(x, s)) {
do {
if (r_key(r, s)) {
r_delete(r, s);
} else {
r_p_integer(r, s, 0);
}
} while (rsk_greater(x, s, s));
for (s in x) {
if (r.key(s)) {
r.delete(s);
} else {
r.p_integer(s, 0);
}
}
if (r_first(r, s)) {
do {
o_text(s);
o_byte('\n');
} while (rsk_greater(r, s, s));
}
r.vcall(o_, 0, "\n");
}
record
new_set(...)
{
integer i;
record r;
i = 0;
while (i < count()) {
r_p_integer(r, $i, 0);
i += 1;
}
ucall(r_p_integer, 1, r, 0);
return r;
r;
}
integer
@ -45,5 +33,5 @@ main(void)
show_sdiff(new_set("John", "Bob", "Mary", "Serena"),
new_set("Jim", "Mary", "John", "Bob"));
return 0;
0;
}

View file

@ -0,0 +1,12 @@
const symmetricDifference = (...args) => {
let result = new Set();
for (const x of args)
for (const e of new Set(x))
if (result.has(e)) result.delete(e)
else result.add(e);
return [...result];
}
// TEST -------------------------------------------------------------------
console.log(symmetricDifference(["Jim", "Mary", "John", "Jim", "Bob"],["John", "Serena", "Bob", "Mary", "Serena"]));
console.log(symmetricDifference([1, 2, 5], [2, 3, 5], [3, 4, 5]));

View file

@ -0,0 +1,2 @@
["Jim", "Serena"]
[1, 4, 5]

View file

@ -1,7 +1,3 @@
a = Set(["John", "Bob", "Mary", "Serena"])
b = Set(["Jim", "Mary", "John", "Bob"])
c = symdiff(a, b)
println("Set a is: ", a)
println("Set b is: ", b)
println("Their symmetric difference is: ", c)
A = ["John", "Bob", "Mary", "Serena"]
B = ["Jim", "Mary", "John", "Bob"]
@show A B symdiff(A, B)

View file

@ -1,43 +1,37 @@
/*REXX pgm finds symmetric difference and symm. AND (between two lists).*/
a.=0 /*falsify the booleans*/
a= '["John", "Serena", "Bob", "Mary", "Serena"]' /*note the duplicate. */
b= '["Jim", "Mary", "John", "Jim", "Bob"]' /* " " " */
a.1=a; say 'list A =' a /*assign and display. */
a.2=b; say 'list B =' b /* " " " */
SD= ; SA= /*Sym. Diff; Sym And.*/
SD.=0; SA.=0 /*falsify SD & SA bool*/
/*══════════════════════════════════════════════════parse the two lists.*/
do k=1 for 2 /*process both lists (stemmed array).*/
a.k=strip(strip(a.k,,"["),,']') /*strip leading and trailing brackets*/
do j=1 until a.k='' /*parse names [they may have blanks]*/
a.k=strip(a.k,,',') /*strip comma (if any)*/
parse var a.k '"' _ '"' a.k /*get the list's name.*/
a.k.j=_ /*store the list name.*/
a.k._=1 /*make a boolean val. */
end /*j*/
a.k.0=j-1 /*number of list names*/
end /*k*/
/*══════════════════════════════════════════════════find symmetric DIFF.*/
do k=1 for 2 /*process both lists. */
ko=word(2 1,k) /*point to other list.*/
do j=1 for a.k.0; _=a.k.j /*process list names. */
if \a.ko._ & \SD._ then do /*if not in both··· */
SD=SD '"'_'",' /*add to sym diff list*/
SD._=1 /*"trueify" a boolean.*/
/*REXX program finds symmetric difference and symmetric AND (between two lists). */
a= '["John", "Serena", "Bob", "Mary", "Serena"]' /*note the duplicate element: Serena */
b= '["Jim", "Mary", "John", "Jim", "Bob"]' /* " " " " Jim */
a.=0; SD.=0; SA.=0; SD=; SA= /*falsify booleans; zero & nullify vars*/
a.1=a; say 'list A =' a /*assign a list and display it to term.*/
a.2=b; say 'list B =' b /* " " " " " " " " */
/* [↓] parse the two lists. */
do k=1 for 2 /*process both lists (stemmed array). */
a.k=strip( strip(a.k, , "["), ,']') /*strip leading and trailing brackets. */
do j=1 until a.k='' /*parse names [they may have blanks]. */
a.k=strip(a.k, , ',') /*strip all commas (if there are any). */
parse var a.k '"' _ '"' a.k /*obtain the name of the list. */
a.k.j=_ /*store the name of the list. */
a.k._=1 /*make a boolean value. */
end /*j*/
a.k.0=j-1 /*the number of this list (of names). */
end /*k*/
say /* [↓] find the symmetric difference. */
do k=1 for 2; ko=word(2 1, k) /*process both lists; KO=other list. */
do j=1 for a.k.0; _=a.k.j /*process the list names. */
if \a.ko._ & \SD._ then do; SD._=1 /*if not in both, then ··· */
SD=SD '"'_'",' /*add to symmetric difference list. */
end
end /*j*/
end /*k*/
SD= "["strip(strip(SD),'T',",")']' /*clean up and [ ] it*/
say; say 'symmetric difference =' SD /*show symmetric DIFF.*/
/*══════════════════════════════════════════════════find symmetric AND. */
do j=1 for a.1.0; _=a.1.j /*process A list names*/
if a.1._ & a.2._ & \SA._ then do /*if common to both···*/
SA=SA '"'_'",' /*add to symmetric AND*/
SA._=1 /*"trueify" a boolean.*/
end
end /*j*/
SA="["strip(strip(SA),'T',",")']' /*clean up and [ ] it*/
say; say ' symmetric AND =' SA /*show symmetric DIFF.*/
/*stick a fork in it, we're done.*/
end /*k*/
/* [↓] SD ≡ symmetric difference. */
SD= "["strip( strip(SD), 'T', ",")']' /*clean up and add brackets [ ] to it.*/
say 'symmetric difference =' SD /*display the symmetric difference. */
/* [↓] locate the symmetric AND. */
do j=1 for a.1.0; _=a.1.j /*process the A list names. */
if a.1._ & a.2._ & \SA._ then do; SA._=1 /*if it's common to both, then ··· */
SA=SA '"'_'",' /*add to symmetric AND list. */
end
end /*j*/
say /* [↓] SA ≡ symmetric AND. */
SA= "["strip( strip(SA), 'T', ",")']' /*clean up and add brackets [ ] to it.*/
say ' symmetric AND =' SA /*stick a fork in it, we're all done. */