Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -11,8 +11,11 @@ Show each of these set operations:
|
|||
* 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.
|
||||
|
||||
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.
|
||||
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). 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.
|
||||
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).
|
||||
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}}
|
||||
|
|
|
|||
55
Task/Set/Prolog/set-2.pro
Normal file
55
Task/Set/Prolog/set-2.pro
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
%% Set creation
|
||||
|
||||
?- list_to_ord_set([1,2,3,4], A), list_to_ord_set([2,4,6,8], B).
|
||||
A = [1, 2, 3, 4],
|
||||
B = [2, 4, 6, 8].
|
||||
|
||||
%% Test m ∈ S -- "m is an element in set S"
|
||||
|
||||
?- ord_memberchk(2, $A).
|
||||
true.
|
||||
|
||||
%% A ∪ B -- union; a set of all elements either in set A or in set B.
|
||||
|
||||
?- ord_union($A, $B, Union).
|
||||
Union = [1, 2, 3, 4, 6, 8].
|
||||
|
||||
%% A ∩ B -- intersection; a set of all elements in both set A and set B.
|
||||
|
||||
?- ord_intersection($A, $B, Intersection).
|
||||
Intersection = [2, 4].
|
||||
|
||||
%% A ∖ B -- difference; a set of all elements in set A, except those in set B.
|
||||
|
||||
?- ord_subtract($A, $B, Diff).
|
||||
Diff = [1, 3].
|
||||
|
||||
%% A ⊆ B -- subset; true if every element in set A is also in set B.
|
||||
|
||||
?- ord_subset($A, $B).
|
||||
false.
|
||||
|
||||
?- ord_subset([2,4], $B).
|
||||
true.
|
||||
|
||||
%% A = B -- equality; true if every element of set A is in set B and vice-versa.
|
||||
|
||||
?- $A == $B.
|
||||
false.
|
||||
|
||||
?- $A == [1,2,3,4].
|
||||
true.
|
||||
|
||||
%% Definition of a proper subset:
|
||||
|
||||
ord_propsubset(A, B) :-
|
||||
ord_subset(A, B),
|
||||
\+(A == B).
|
||||
|
||||
%% add/remove elements
|
||||
|
||||
?- ord_add_element($A, 19, NewA).
|
||||
NewA = [1, 2, 3, 4, 19].
|
||||
|
||||
?- ord_del_element($NewA, 3, NewerA).
|
||||
NewerA = [1, 2, 4, 19].
|
||||
|
|
@ -1,49 +1,49 @@
|
|||
>>> s1, s2 = {1, 2, 3, 4}, {3, 4, 5, 6}
|
||||
>>> s1 | s2; # Union
|
||||
>>> s1 | s2 # Union
|
||||
{1, 2, 3, 4, 5, 6}
|
||||
>>> s1 & s2; # Intersection
|
||||
>>> s1 & s2 # Intersection
|
||||
{3, 4}
|
||||
>>> s1 - s2; # Difference
|
||||
>>> s1 - s2 # Difference
|
||||
{1, 2}
|
||||
>>> s1 < s1; # True subset
|
||||
>>> s1 < s1 # True subset
|
||||
False
|
||||
>>> {3, 1} < s1; # True subset
|
||||
>>> {3, 1} < s1 # True subset
|
||||
True
|
||||
>>> s1 <= s1; # Subset
|
||||
>>> s1 <= s1 # Subset
|
||||
True
|
||||
>>> {3, 1} <= s1; # Subset
|
||||
>>> {3, 1} <= s1 # Subset
|
||||
True
|
||||
>>> {3, 2, 4, 1} == s1; # Equality
|
||||
>>> {3, 2, 4, 1} == s1 # Equality
|
||||
True
|
||||
>>> s1 == s2; # Equality
|
||||
>>> s1 == s2 # Equality
|
||||
False
|
||||
>>> 2 in s1; # Membership
|
||||
>>> 2 in s1 # Membership
|
||||
True
|
||||
>>> 10 not in s1; # Non-membership
|
||||
>>> 10 not in s1 # Non-membership
|
||||
True
|
||||
>>> {1, 2, 3, 4, 5} > s1; # True superset
|
||||
>>> {1, 2, 3, 4, 5} > s1 # True superset
|
||||
True
|
||||
>>> {1, 2, 3, 4} > s1; # True superset
|
||||
>>> {1, 2, 3, 4} > s1 # True superset
|
||||
False
|
||||
>>> {1, 2, 3, 4} >= s1; # Superset
|
||||
>>> {1, 2, 3, 4} >= s1 # Superset
|
||||
True
|
||||
>>> s1 ^ s2; # Symmetric difference
|
||||
>>> s1 ^ s2 # Symmetric difference
|
||||
{1, 2, 5, 6}
|
||||
>>> len(s1); # Cardinality
|
||||
>>> len(s1) # Cardinality
|
||||
4
|
||||
>>> s1.add(99); # Mutability
|
||||
>>> s1.add(99) # Mutability
|
||||
>>> s1
|
||||
{99, 1, 2, 3, 4}
|
||||
>>> s1.discard(99); # Mutability
|
||||
>>> s1.discard(99) # Mutability
|
||||
>>> s1
|
||||
{1, 2, 3, 4}
|
||||
>>> s1 |= s2; # Mutability
|
||||
>>> s1 |= s2 # Mutability
|
||||
>>> s1
|
||||
{1, 2, 3, 4, 5, 6}
|
||||
>>> s1 -= s2; # Mutability
|
||||
>>> s1 -= s2 # Mutability
|
||||
>>> s1
|
||||
{1, 2}
|
||||
>>> s1 ^= s2; # Mutability
|
||||
>>> s1 ^= s2 # Mutability
|
||||
>>> s1
|
||||
{1, 2, 3, 4, 5, 6}
|
||||
>>>
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ if add then do; set_=_2; t=_1; s=_1; end
|
|||
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)
|
||||
if subset then return 1
|
||||
if eq then if arg()>3 then return 1
|
||||
else return set$('equal',_2,_1,1)
|
||||
return set.t
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue