Set: Difference between revisions

3,146 bytes added ,  6 years ago
no edit summary
No edit summary
Line 4,343:
prime is a subset of happy: false
prime is equal to emirp: true
</pre>
 
=={{header|Ring}}==
<lang ring>
# 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 seta[n] = "1" and setb[n] = "0"
flag = 0
ok
next
return flag
</lang>
Output:
<pre>
Set A: apple, cherry, elderberry, grape
Set B: banana, cherry, date, elderberry, fig
Element M: fig
M is not an element of set A
M is an element of set B
The union of A and B is: apple, banana, cherry, date, elderberry, fig, grape,
The intersection of A and B is: cherry, elderberry,
The difference of A and B is: apple, grape,
Set A is not a subset of set B
Set A is not equal to set B
</pre>
 
2,468

edits