Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9fde159

Browse files
committed
Set 🎉
1 parenta47fbd7 commit9fde159

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

‎examples/datastructures/set.js

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

‎test/datastructures/set.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
constexpect=require('chai').expect;
2+
constSet=require('../../examples/datastructures/set');
3+
4+
describe('=> SET',function(){
5+
letnumbers;
6+
before(function(){
7+
numbers=newSet();
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=newSet();
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=newSet();
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)

[8]ページ先頭

©2009-2025 Movatter.jp