June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
111
Task/Set/Lua/set-2.lua
Normal file
111
Task/Set/Lua/set-2.lua
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
local function new(_, ...)
|
||||
local r = {}
|
||||
local s = setmetatable({}, {
|
||||
-- API operations
|
||||
__index = {
|
||||
|
||||
-- single value insertion
|
||||
insert = function(s, v)
|
||||
if not r[v] then
|
||||
table.insert(s, v)
|
||||
r[v] = #s
|
||||
end
|
||||
return s
|
||||
end,
|
||||
|
||||
-- single value removal
|
||||
remove = function(s, v)
|
||||
local i = r[v]
|
||||
if i then
|
||||
r[v] = nil
|
||||
local t = table.remove(s)
|
||||
if t ~= v then
|
||||
r[t] = i
|
||||
s[i] = t
|
||||
end
|
||||
end
|
||||
return s
|
||||
end,
|
||||
|
||||
-- multi-value insertion
|
||||
batch_insert = function(s, ...)
|
||||
for _,v in pairs {...} do
|
||||
s:insert(v)
|
||||
end
|
||||
return s
|
||||
end,
|
||||
|
||||
-- multi-value removal
|
||||
batch_remove = function(s, ...)
|
||||
for _,v in pairs {...} do
|
||||
s:remove(v)
|
||||
end
|
||||
return s
|
||||
end,
|
||||
|
||||
-- membership test
|
||||
has = function(s, e)
|
||||
return r[e] ~= nil
|
||||
end
|
||||
},
|
||||
|
||||
-- set manipulation operators
|
||||
|
||||
-- union
|
||||
__add = function(s1, s2)
|
||||
r = set()
|
||||
r:batch_insert(table.unpack(s1))
|
||||
r:batch_insert(table.unpack(s2))
|
||||
return r
|
||||
end,
|
||||
|
||||
-- subtraction
|
||||
__sub = function(s1, s2)
|
||||
r = set()
|
||||
r:batch_insert(table.unpack(s1))
|
||||
r:batch_remove(table.unpack(s2))
|
||||
return r
|
||||
end,
|
||||
|
||||
-- intersection
|
||||
__mul = function(s1, s2)
|
||||
r = set()
|
||||
for _,v in ipairs(s1) do
|
||||
if s2:has(v) then
|
||||
r:insert(v)
|
||||
end
|
||||
end
|
||||
return r
|
||||
end,
|
||||
|
||||
-- equality
|
||||
__eq = function(s1, s2)
|
||||
if #s1 ~= #s2 then return false end
|
||||
for _,v in ipairs(s1) do
|
||||
if not s2:has(v) then return false end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
-- proper subset
|
||||
__lt = function(s1, s2)
|
||||
if s1 == s2 then return false end
|
||||
for _,v in ipairs(s1) do
|
||||
if not s2:has(v) then return false end
|
||||
end
|
||||
return true
|
||||
end,
|
||||
|
||||
-- subset
|
||||
__lte = function(s1, s2)
|
||||
return (s1 == s2) or (s1 < s2)
|
||||
end,
|
||||
|
||||
-- metatable type tag
|
||||
__type__ = 'set'
|
||||
})
|
||||
s:batch_insert(...)
|
||||
return s
|
||||
end
|
||||
|
||||
return setmetatable({}, { __call = new })
|
||||
|
|
@ -7,8 +7,8 @@ my $c = set <a b c d e>;
|
|||
ok 'c' ∈ $a, "c is an element in set A";
|
||||
nok 'd' ∈ $a, "d is not an element in set A";
|
||||
|
||||
is $a ∪ $b, set(<a b c d>), "union; a set of all elements either in set A or in set B";
|
||||
is $a ∩ $b, set(<b c>), "intersection; a set of all elements in both set A and set B";
|
||||
is-deeply $a ∪ $b, set(<a b c d>), "union; a set of all elements either in set A or in set B";
|
||||
is-deeply $a ∩ $b, set(<b c>), "intersection; a set of all elements in both set A and set B";
|
||||
is $a (-) $b, set(<a>), "difference; a set of all elements in set A, except those in set B";
|
||||
|
||||
ok $a ⊆ $c, "subset; true if every element in set A is also in set B";
|
||||
|
|
|
|||
116
Task/Set/Ring/set.ring
Normal file
116
Task/Set/Ring/set.ring
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
# Project : Set
|
||||
# Date : 2018/03/26
|
||||
# Author : Gal Zsolt [~ CalmoSoft ~]
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
arr = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"]
|
||||
for n = 1 to 25
|
||||
add(arr,"")
|
||||
next
|
||||
seta = "1010101"
|
||||
see "Set A: " + arrset(arr,seta) + nl
|
||||
setb = "0111110"
|
||||
see "Set B: " + arrset(arr,setb) + nl
|
||||
elementm = "0000010"
|
||||
see "Element M: " + arrset(arr,elementm) + nl
|
||||
|
||||
temp = arrsetinsec(elementm,seta)
|
||||
if len(temp) > 0
|
||||
see "M is an element of set A" + nl
|
||||
else
|
||||
see "M is not an element of set A" + nl
|
||||
ok
|
||||
temp = arrsetinsec(elementm,setb)
|
||||
if len(temp) > 0
|
||||
see "M is an element of set B" + nl
|
||||
else
|
||||
see "M is not an element of set B" + nl
|
||||
ok
|
||||
|
||||
see "The union of A and B is: "
|
||||
see arrsetunion(seta,setb) + nl
|
||||
see "The intersection of A and B is: "
|
||||
see arrsetinsec(seta,setb) + nl
|
||||
see "The difference of A and B is: "
|
||||
see arrsetnot(seta,setb) + nl
|
||||
|
||||
flag = arrsetsub(seta,setb)
|
||||
if flag = 1
|
||||
see "Set A is a subset of set B" + nl
|
||||
else
|
||||
see "Set A is not a subset of set B" + nl
|
||||
ok
|
||||
if seta = setb
|
||||
see "Set A is equal to set B" + nl
|
||||
else
|
||||
see "Set A is not equal to set B" + nl
|
||||
ok
|
||||
|
||||
func arrset(arr,set)
|
||||
o = ""
|
||||
for i = 1 to 7
|
||||
if set[i] = "1"
|
||||
o = o + arr[i] + ", "
|
||||
ok
|
||||
next
|
||||
return left(o,len(o)-2)
|
||||
|
||||
func arrsetunion(seta,setb)
|
||||
o = ""
|
||||
union = list(len(seta))
|
||||
for n = 1 to len(seta)
|
||||
if seta[n] = "1" or setb[n] = "1"
|
||||
union[n] = "1"
|
||||
else
|
||||
union[n] = "0"
|
||||
ok
|
||||
next
|
||||
for i = 1 to len(union)
|
||||
if union[i] = "1"
|
||||
o = o + arr[i] + ", "
|
||||
ok
|
||||
next
|
||||
return o
|
||||
|
||||
func arrsetinsec(setc,setd)
|
||||
o = ""
|
||||
union = list(len(setc))
|
||||
for n = 1 to len(setc)
|
||||
if setc[n] = "1" and setd[n] = "1"
|
||||
union[n] = "1"
|
||||
else
|
||||
union[n] = "0"
|
||||
ok
|
||||
next
|
||||
for i = 1 to len(union)
|
||||
if union[i] = "1"
|
||||
o = o + arr[i] + ", "
|
||||
ok
|
||||
next
|
||||
return o
|
||||
|
||||
func arrsetnot(seta,setb)
|
||||
o = ""
|
||||
union = list(len(seta))
|
||||
for n = 1 to len(seta)
|
||||
if seta[n] = "1" and setb[n] = "0"
|
||||
union[n] = "1"
|
||||
else
|
||||
union[n] = "0"
|
||||
ok
|
||||
next
|
||||
for i = 1 to len(union)
|
||||
if union[i] = "1"
|
||||
o = o + arr[i] + ", "
|
||||
ok
|
||||
next
|
||||
return o
|
||||
|
||||
func arrsetsub(setc,setd)
|
||||
flag = 1
|
||||
for n = 1 to len(setc)
|
||||
if setc[n] = "1" and setd[n] = "0"
|
||||
flag = 0
|
||||
ok
|
||||
next
|
||||
return flag
|
||||
Loading…
Add table
Add a link
Reference in a new issue