September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,54 +1,29 @@
|
|||
with Ada.Text_IO, Ada.Containers.Ordered_Sets;
|
||||
with ada.containers.ordered_sets, ada.text_io;
|
||||
use ada.text_io;
|
||||
|
||||
procedure Set_Demo is
|
||||
procedure set_demo is
|
||||
package cs is new ada.containers.ordered_sets (character); use cs;
|
||||
|
||||
package CS is new Ada.Containers.Ordered_Sets(Character); use CS;
|
||||
package IO renames Ada.Text_IO;
|
||||
function "+" (s : string) return set is
|
||||
(if s = "" then empty_set else Union(+ s(s'first..s'last - 1), To_Set (s(s'last))));
|
||||
|
||||
-- helper functions for string to something conversion, and vice versa
|
||||
function To_Set(S: String) return Set is
|
||||
Result: Set;
|
||||
begin
|
||||
for I in S'Range loop
|
||||
begin
|
||||
Result.Insert(S(I));
|
||||
-- raises Constraint_Error if S(I) is already in Result
|
||||
exception
|
||||
when Constraint_Error => null;
|
||||
end;
|
||||
end loop;
|
||||
return Result;
|
||||
end To_Set;
|
||||
|
||||
function Image(S: Set) return String is
|
||||
C: Character;
|
||||
T: Set := S;
|
||||
begin
|
||||
if T.Is_Empty then
|
||||
return "";
|
||||
else
|
||||
C:= T.First_Element;
|
||||
T.Delete_First;
|
||||
return C & Image(T);
|
||||
end if;
|
||||
end Image;
|
||||
|
||||
function Image(C: Ada.Containers.Count_Type) return String renames
|
||||
Ada.Containers.Count_Type'Image;
|
||||
|
||||
S1, S2: Set;
|
||||
begin -- main program
|
||||
loop
|
||||
S1 := To_Set(Ada.Text_IO.Get_Line);
|
||||
exit when S1 = To_Set("quit!");
|
||||
S2 := To_Set(Ada.Text_IO.Get_Line);
|
||||
IO.Put_Line("Sets [" & Image(S1) & "], [" & Image(S2) & "] of size"
|
||||
& Image(S1.Length) & " and" & Image(S2.Length) & ".");
|
||||
IO.Put_Line("Intersection: [" & Image(Intersection(S1, S2)) & "],");
|
||||
IO.Put_Line("Union: [" & Image(Union(S1, S2)) & "],");
|
||||
IO.Put_Line("Difference: [" & Image(Difference(S1, S2)) & "],");
|
||||
IO.Put_Line("Symmetric Diff: [" & Image(S1 xor S2) & "],");
|
||||
IO.Put_Line("Subset: " & Boolean'Image(S1.Is_Subset(S2))
|
||||
& ", Equal: " & Boolean'Image(S1 = S2) & ".");
|
||||
end loop;
|
||||
end Set_Demo;
|
||||
function "-" (s : Set) return string is
|
||||
(if s = empty_set then "" else - (s - To_Set (s.last_element)) & s.last_element);
|
||||
s1, s2 : set;
|
||||
begin
|
||||
loop
|
||||
put ("s1= ");
|
||||
s1 := + get_line;
|
||||
exit when s1 = +"Quit!";
|
||||
put ("s2= ");
|
||||
s2 := + get_line;
|
||||
Put_Line("Sets [" & (-s1) & "], [" & (-s2) & "] of size"
|
||||
& S1.Length'img & " and" & s2.Length'img & ".");
|
||||
Put_Line("Intersection: [" & (-(Intersection(S1, S2))) & "],");
|
||||
Put_Line("Union: [" & (-(Union(s1, s2))) & "],");
|
||||
Put_Line("Difference: [" & (-(Difference(s1, s2))) & "],");
|
||||
Put_Line("Symmetric Diff: [" & (-(s1 xor s2)) & "],");
|
||||
Put_Line("Subset: " & Boolean'Image(s1.Is_Subset(s2))
|
||||
& ", Equal: " & Boolean'Image(s1 = s2) & ".");
|
||||
end loop;
|
||||
end set_demo;
|
||||
|
|
|
|||
31
Task/Set/Ol/set.ol
Normal file
31
Task/Set/Ol/set.ol
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
; test set
|
||||
(define set1 '(1 2 3 4 5 6 7 8 9))
|
||||
(define set2 '(3 4 5 11 12 13 14))
|
||||
(define set3 '(4 5 6 7))
|
||||
(define set4 '(1 2 3 4 5 6 7 8 9))
|
||||
|
||||
; union
|
||||
(print (union set1 set2))
|
||||
; ==> (1 2 6 7 8 9 3 4 5 11 12 13 14)
|
||||
|
||||
; intersection
|
||||
(print (intersect set1 set2))
|
||||
; ==> (3 4 5)
|
||||
|
||||
; difference
|
||||
(print (diff set1 set2))
|
||||
; ==> (1 2 6 7 8 9)
|
||||
|
||||
; subset (no predefined function)
|
||||
(define (subset? a b)
|
||||
(all (lambda (i) (has? b i)) a))
|
||||
(print (subset? set3 set1))
|
||||
; ==> #true
|
||||
(print (subset? set3 set2))
|
||||
; ==> #false
|
||||
|
||||
; equality
|
||||
(print (equal? set1 set2))
|
||||
; ==> #false
|
||||
(print (equal? set1 set4))
|
||||
; ==> #true
|
||||
|
|
@ -1,56 +1,56 @@
|
|||
class Set(*set) {
|
||||
class MySet(*set) {
|
||||
|
||||
method init {
|
||||
var elems = set;
|
||||
set = Hash.new;
|
||||
var elems = set
|
||||
set = Hash()
|
||||
elems.each { |e| self += e }
|
||||
}
|
||||
|
||||
method +(elem) {
|
||||
set{elem} = elem;
|
||||
self;
|
||||
set{elem} = elem
|
||||
self
|
||||
}
|
||||
|
||||
method del(elem) {
|
||||
set.delete(elem);
|
||||
set.delete(elem)
|
||||
}
|
||||
|
||||
method has(elem) {
|
||||
set.has_key(elem);
|
||||
set.has_key(elem)
|
||||
}
|
||||
|
||||
method ∪(Set that) {
|
||||
Set(set.values..., that.values...);
|
||||
method ∪(MySet that) {
|
||||
MySet(set.values..., that.values...)
|
||||
}
|
||||
|
||||
method ∩(Set that) {
|
||||
Set(set.keys.grep{ |k| k ∈ that } \
|
||||
.map { |k| set{k} }...);
|
||||
method ∩(MySet that) {
|
||||
MySet(set.keys.grep{ |k| k ∈ that } \
|
||||
.map { |k| set{k} }...)
|
||||
}
|
||||
|
||||
method ∖(Set that) {
|
||||
Set(set.keys.grep{|k| !(k ∈ that) } \
|
||||
.map {|k| set{k} }...);
|
||||
method ∖(MySet that) {
|
||||
MySet(set.keys.grep{|k| !(k ∈ that) } \
|
||||
.map {|k| set{k} }...)
|
||||
}
|
||||
|
||||
method ^(Set that) {
|
||||
var d = ((self ∖ that) ∪ (that ∖ self));
|
||||
Set(d.values...);
|
||||
method ^(MySet that) {
|
||||
var d = ((self ∖ that) ∪ (that ∖ self))
|
||||
MySet(d.values...)
|
||||
}
|
||||
|
||||
method count { set.len }
|
||||
|
||||
method ≡(Set that) {
|
||||
(self ∖ that -> count.is_zero) && (that ∖ self -> count.is_zero);
|
||||
method ≡(MySet that) {
|
||||
(self ∖ that -> count.is_zero) && (that ∖ self -> count.is_zero)
|
||||
}
|
||||
|
||||
method values { set.values }
|
||||
|
||||
method ⊆(Set that) {
|
||||
method ⊆(MySet that) {
|
||||
that.set.keys.each { |k|
|
||||
k ∈ self || return false;
|
||||
k ∈ self || return false
|
||||
}
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
|
||||
method to_s {
|
||||
|
|
@ -59,7 +59,7 @@ class Set(*set) {
|
|||
}
|
||||
|
||||
class Object {
|
||||
method ∈(Set set) {
|
||||
set.has(self);
|
||||
method ∈(MySet set) {
|
||||
set.has(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
var x = Set(1, 2, 3);
|
||||
5..7 -> each { |i| x += i };
|
||||
var x = MySet(1, 2, 3)
|
||||
5..7 -> each { |i| x += i }
|
||||
|
||||
var y = Set(1, 2, 4, x);
|
||||
var y = MySet(1, 2, 4, x)
|
||||
|
||||
say "set x is: #{x}";
|
||||
say "set y is: #{y}";
|
||||
say "set x is: #{x}"
|
||||
say "set y is: #{y}"
|
||||
|
||||
[1,2,3,4,x].each { |elem|
|
||||
say ("#{elem} is ", elem ∈ y ? '' : 'not', " in y");
|
||||
say ("#{elem} is ", elem ∈ y ? '' : 'not', " in y")
|
||||
}
|
||||
|
||||
var (w, z);
|
||||
say ("union: ", x ∪ y);
|
||||
say ("intersect: ", x ∩ y);
|
||||
say ("z = x ∖ y = ", z = (x ∖ y) );
|
||||
say ("y is ", x ⊆ y ? "" : "not ", "a subset of x");
|
||||
say ("z is ", x ⊆ z ? "" : "not ", "a subset of x");
|
||||
say ("z = (x ∪ y) ∖ (x ∩ y) = ", z = ((x ∪ y) ∖ (x ∩ y)));
|
||||
say ("w = x ^ y = ", w = (x ^ y));
|
||||
say ("w is ", w ≡ z ? "" : "not ", "equal to z");
|
||||
say ("w is ", w ≡ x ? "" : "not ", "equal to x");
|
||||
var (w, z)
|
||||
say ("union: ", x ∪ y)
|
||||
say ("intersect: ", x ∩ y)
|
||||
say ("z = x ∖ y = ", z = (x ∖ y) )
|
||||
say ("y is ", x ⊆ y ? "" : "not ", "a subset of x")
|
||||
say ("z is ", x ⊆ z ? "" : "not ", "a subset of x")
|
||||
say ("z = (x ∪ y) ∖ (x ∩ y) = ", z = ((x ∪ y) ∖ (x ∩ y)))
|
||||
say ("w = x ^ y = ", w = (x ^ y))
|
||||
say ("w is ", w ≡ z ? "" : "not ", "equal to z")
|
||||
say ("w is ", w ≡ x ? "" : "not ", "equal to x")
|
||||
|
|
|
|||
124
Task/Set/VBA/set.vba
Normal file
124
Task/Set/VBA/set.vba
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
'Implementation of "set" using the built in Collection datatype.
|
||||
'A collection can hold any object as item. The examples here are only strings.
|
||||
'A collection stores item, key pairs. With the key you can retrieve the item.
|
||||
'The keys are hidden and cannot be changed. No duplicate keys are allowed.
|
||||
'For the "set" implementation item is the same as the key. And keys must
|
||||
'be a string.
|
||||
Private Function createSet(t As Variant) As Collection
|
||||
Dim x As New Collection
|
||||
For Each elem In t
|
||||
x.Add elem, elem
|
||||
Next elem
|
||||
Set createSet = x
|
||||
End Function
|
||||
Private Function isElement(s As Variant, x As Collection) As Boolean
|
||||
Dim errno As Integer, t As Variant
|
||||
On Error GoTo err
|
||||
t = x(s)
|
||||
isElement = True
|
||||
Exit Function
|
||||
err:
|
||||
isElement = False
|
||||
End Function
|
||||
Private Function setUnion(A As Collection, B As Collection) As Collection
|
||||
Dim x As New Collection
|
||||
For Each elem In A
|
||||
x.Add elem, elem
|
||||
Next elem
|
||||
For Each elem In B
|
||||
On Error Resume Next 'Trying to add a duplicate throws an error
|
||||
x.Add elem, elem
|
||||
Next elem
|
||||
Set setUnion = x
|
||||
End Function
|
||||
Private Function intersection(A As Collection, B As Collection) As Collection
|
||||
Dim x As New Collection
|
||||
For Each elem In A
|
||||
If isElement(elem, B) Then x.Add elem, elem
|
||||
Next elem
|
||||
For Each elem In B
|
||||
If isElement(elem, A) Then
|
||||
On Error Resume Next
|
||||
x.Add elem, elem
|
||||
End If
|
||||
Next elem
|
||||
Set intersection = x
|
||||
End Function
|
||||
Private Function difference(A As Collection, B As Collection) As Collection
|
||||
Dim x As New Collection
|
||||
For Each elem In A
|
||||
If Not isElement(elem, B) Then x.Add elem, elem
|
||||
Next elem
|
||||
Set difference = x
|
||||
End Function
|
||||
Private Function subset(A As Collection, B As Collection) As Boolean
|
||||
Dim flag As Boolean
|
||||
flag = True
|
||||
For Each elem In A
|
||||
If Not isElement(elem, B) Then
|
||||
flag = False
|
||||
Exit For
|
||||
End If
|
||||
Next elem
|
||||
subset = flag
|
||||
End Function
|
||||
Private Function equality(A As Collection, B As Collection) As Boolean
|
||||
Dim flag As Boolean
|
||||
flag = True
|
||||
If A.Count = B.Count Then
|
||||
For Each elem In A
|
||||
If Not isElement(elem, B) Then
|
||||
flag = False
|
||||
Exit For
|
||||
End If
|
||||
Next elem
|
||||
Else
|
||||
flag = False
|
||||
End If
|
||||
equality = flag
|
||||
End Function
|
||||
Private Function properSubset(A As Collection, B As Collection) As Boolean
|
||||
Dim flag As Boolean
|
||||
flag = True
|
||||
If A.Count < B.Count Then
|
||||
For Each elem In A
|
||||
If Not isElement(elem, B) Then
|
||||
flag = False
|
||||
Exit For
|
||||
End If
|
||||
Next elem
|
||||
Else
|
||||
flag = False
|
||||
End If
|
||||
properSubset = flag
|
||||
End Function
|
||||
Public Sub main()
|
||||
'Set creation
|
||||
Dim s As Variant
|
||||
Dim A As Collection, B As Collection, C As Collection
|
||||
s = [{"Apple","Banana","Pear","Pineapple"}]
|
||||
Set A = createSet(s) 'Fills the collection A with the elements of s
|
||||
'Test m ? S -- "m is an element in set S"
|
||||
Debug.Print isElement("Apple", A) 'returns True
|
||||
Debug.Print isElement("Fruit", A) 'returns False
|
||||
'A ? B -- union; a set of all elements either in set A or in set B.
|
||||
s = [{"Fruit","Banana","Pear","Orange"}]
|
||||
Set B = createSet(s)
|
||||
Set C = setUnion(A, B)
|
||||
'A n B -- intersection; a set of all elements in both set A and set B.
|
||||
Set C = intersection(A, B)
|
||||
'A \ B -- difference; a set of all elements in set A, except those in set B.
|
||||
Set C = difference(A, B)
|
||||
'A ? B -- subset; true if every element in set A is also in set B.
|
||||
Debug.Print subset(A, B)
|
||||
'A = B -- equality; true if every element of set A is in set B and vice versa.
|
||||
Debug.Print equality(A, B)
|
||||
'Proper subset
|
||||
Debug.Print properSubset(A, B)
|
||||
'Modify -remove an element by key
|
||||
A.Remove "Apple"
|
||||
'Modify -remove the first element in the collection/set
|
||||
A.Remove 1
|
||||
'Add "10" to A
|
||||
A.Add "10", "10"
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue