How to resolve the algorithm Set step by step in the Nanoquery programming language
How to resolve the algorithm Set step by step in the Nanoquery programming language
Table of Contents
Problem Statement
A set is a collection of elements, without duplicates and without order.
Show each of these set operations:
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 bit-wise 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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Set step by step in the Nanoquery programming language
Source code in the nanoquery programming language
class set
declare internal_list
def set()
internal_list = list()
end
def set(list)
internal_list = list
end
def append(value)
if not value in internal_list
internal_list.append(value)
end
return this
end
def contains(value)
return value in internal_list
end
def difference(other)
diff = list()
for value in this.internal_list
diff.append(value)
end
for i in range(len(diff) - 1, 0)
if diff[i] in other.internal_list
diff.remove(i)
end
end
return new(set, diff)
end
def operator=(other)
for value in other.internal_list
if not value in this.internal_list
return false
end
end
return true
end
def intersection(other)
intersect = list()
for value in this.internal_list
if other.contains(value)
intersect.append(value)
end
end
return new(set, intersect)
end
def subset(other)
for value in this.internal_list
if not value in other.internal_list
return false
end
end
return true
end
def union(other)
u = list()
for value in this.internal_list
u.append(value)
end
for value in other.internal_list
if not value in u
u.append(value)
end
end
return new(set, u)
end
def toString()
return str(this.internal_list)
end
end
import "rosetta-code/set.nq"
a = new(set, {1, 2, 3, 4, 5})
b = new(set, {2, 3, 4, 5, 6, 8})
c = new(set, {2, 3, 4})
d = new(set, {2, 3, 4})
println "a: " + a
println "b: " + b
println "c: " + c
println "d: " + d
println "2 in a: " + a.contains(2)
println "6 in a: " + a.contains(6)
println "a union b: " + a.union(b)
println "a - b: " + a.difference(b)
println "c subset of a: " + c.subset(a)
println "c = d: " + (c = d)
println "d = c: " + (d = c)
println "a intersect b: " + a.intersection(b)
println "add 7 to a: " + a.append(7)
println "add 2 to a again: " + a.append(2)
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the J programming language
You may also check:How to resolve the algorithm One of n lines in a file step by step in the zkl programming language
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Factor programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Clojure programming language
You may also check:How to resolve the algorithm Rate counter step by step in the Delphi programming language