Skip to content

Commit 9fde159

Browse files
committed
Set 🎉
1 parent a47fbd7 commit 9fde159

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

examples/datastructures/set.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class Set {
2+
constructor() {
3+
this.dataStore = [];
4+
}
5+
6+
contains(data) {
7+
let pos = this.dataStore.indexOf(data);
8+
if(!~pos)
9+
return false;
10+
return true;
11+
}
12+
13+
add(data) {
14+
let pos = this.dataStore.indexOf(data);
15+
if(!~pos){
16+
this.dataStore.push(data);
17+
return true;
18+
}
19+
return false;
20+
}
21+
22+
remove(data) {
23+
const pos = this.dataStore.indexOf(data);
24+
if(~pos) {
25+
this.dataStore.splice(pos,1);
26+
return true;
27+
}
28+
return false;
29+
}
30+
31+
show() {
32+
return this.dataStore.toString();
33+
}
34+
35+
size() {
36+
return this.dataStore.length;
37+
}
38+
39+
union(set) {
40+
let tempSet = new Set();
41+
for(let i = 0; i < this.dataStore.length; i++)
42+
if(set.contains(this.dataStore[i]))
43+
tempSet.add(this.dataStore[i]);
44+
return tempSet;
45+
}
46+
47+
intersect(set) {
48+
let tempSet = new Set();
49+
for(let i = 0; i < this.dataStore.length; i++)
50+
if(set.contains(this.dataStore[i]))
51+
tempSet.add(this.dataStore[i]);
52+
return tempSet;
53+
}
54+
55+
difference(set) {
56+
let tempSet = new Set();
57+
for(let i = 0; i < this.dataStore.length; i++)
58+
if(!set.contains(this.dataStore[i]))
59+
tempSet.add(this.dataStore[i]);
60+
return tempSet;
61+
}
62+
63+
subset(set) {
64+
if(this.size() > set.size())
65+
return false;
66+
else
67+
for(let member of this.dataStore)
68+
if(!set.contains(member))
69+
return false;
70+
return true;
71+
}
72+
}
73+
74+
module.exports = Set;

test/datastructures/set.test.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const expect = require('chai').expect;
2+
const Set = require('../../examples/datastructures/set');
3+
4+
describe('=> SET', function() {
5+
let numbers;
6+
before(function () {
7+
numbers = new Set();
8+
});
9+
10+
it('should create an Empty Set', function(done) {
11+
expect(numbers.dataStore).to.deep.equal([]);
12+
done();
13+
});
14+
15+
it('should add element into the Set', function(done) {
16+
expect(numbers.add(1)).to.equal(true);
17+
expect(numbers.add(2)).to.equal(true);
18+
expect(numbers.add(3)).to.equal(true);
19+
expect(numbers.dataStore.length).to.equal(3);
20+
expect(numbers.dataStore).to.deep.equal([1,2,3]);
21+
done();
22+
});
23+
24+
it('should display elements of Set', function(done) {
25+
expect(numbers.show()).to.equal("1,2,3");
26+
done();
27+
});
28+
29+
it('should return size of Set', function(done) {
30+
expect(numbers.size()).to.equal(3);
31+
done();
32+
});
33+
34+
it('should check if a given Set is a subset of the Original Set', function(done) {
35+
tempSet = new Set();
36+
tempSet.add(1);
37+
tempSet.add(2);
38+
tempSet.add(3);
39+
expect(numbers.subset(tempSet)).to.equal(true);
40+
done();
41+
});
42+
43+
it('should contain some elements in the Set', function(done) {
44+
expect(numbers.contains(1)).to.equal(true);
45+
expect(numbers.contains(31)).to.equal(false);
46+
done();
47+
});
48+
49+
it('should not add duplicate elements into the Set', function(done) {
50+
expect(numbers.add(1)).to.equal(false);
51+
expect(numbers.add(2)).to.equal(false);
52+
expect(numbers.dataStore.length).to.equal(3);
53+
expect(numbers.dataStore).to.deep.equal([1,2,3]);
54+
done();
55+
});
56+
57+
it('should remove element from the Set', function(done) {
58+
expect(numbers.remove(1)).to.equal(true);
59+
expect(numbers.dataStore.length).to.equal(2);
60+
expect(numbers.dataStore).to.deep.equal([2,3]);
61+
done();
62+
});
63+
64+
it('should not remove element which does not exist in Set', function(done) {
65+
expect(numbers.remove(1)).to.equal(false);
66+
expect(numbers.dataStore.length).to.equal(2);
67+
expect(numbers.dataStore).to.deep.equal([2,3]);
68+
done();
69+
});
70+
71+
describe('=> union, intersection, difference', function() {
72+
before(function() {
73+
numbers2 = new Set();
74+
numbers.add(1);
75+
numbers2.add(1);
76+
numbers2.add(2);
77+
numbers2.add(4);
78+
numbers2.add(5);
79+
numbers2.add(6);
80+
});
81+
82+
it('should compute Union of 2 Sets', function(done) {
83+
union = numbers.union(numbers2);
84+
expect(union.dataStore.length).to.equal(2);
85+
expect(union.contains(1)).to.equal(true);
86+
expect(union.contains(2)).to.equal(true);
87+
done();
88+
});
89+
90+
it('should compute Intersection of 2 Sets', function(done) {
91+
intersect = numbers.intersect(numbers2);
92+
expect(intersect.dataStore.length).to.equal(2);
93+
expect(intersect.contains(1)).to.equal(true);
94+
expect(intersect.contains(2)).to.equal(true);
95+
done();
96+
});
97+
98+
it('should compute Difference of 2 Sets', function(done) {
99+
difference = numbers.difference(numbers2);
100+
expect(difference.dataStore.length).to.equal(1);
101+
expect(difference.contains(3)).to.equal(true);
102+
done();
103+
});
104+
105+
});
106+
107+
});

0 commit comments

Comments
 (0)