-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_assignments.js
More file actions
231 lines (204 loc) · 10.4 KB
/
test_assignments.js
File metadata and controls
231 lines (204 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const assert = require("assert");
const lib = require("./source_code_of_assignments.js");
//test for the function which picks odd numbers from an array
// a odd number that should give me back that number in an array
assert.deepEqual(lib.pickOddNumbers([1]),[1]);
// a 5 numbers that should give me back odd numbers from that
assert.deepEqual(lib.pickOddNumbers([1,2,3,2,4]),[1,3]);
// many numbers that should give me back the odd numbers
assert.deepEqual(lib.pickOddNumbers([1,2,3,2,3,4,2,3]),[1,3,3,3]);
//test for the function which picks even numbers from an array
// one odd number that should give me back an empty array
assert.deepEqual(lib.pickEvenNumbers([1]),[]);
// 5 numbers that should give me back even numbers only
assert.deepEqual(lib.pickEvenNumbers([1,2,3,2,4]),[2,2,4]);
// many numbers that should give me back the even numbers
assert.deepEqual(lib.pickEvenNumbers([1,2,3,2,3,4,2,3]),[2,2,4,2]);
//tests for the functiom which adds all the numbers of list
// one empty list should give me back nothing
assert.deepEqual(lib.calculateSum([]),0);
// 1 element array should give me back that element
assert.deepEqual(lib.calculateSum([1]),1);
// 2 element array should give me back the sum of them
assert.deepEqual(lib.calculateSum([1,2]),3);
//tests for reversing an array
// empty array should give me back an empty array
assert.deepEqual(lib.reverse([]),[]);
// 1 element array should give back that element
assert.deepEqual(lib.reverse([2]),[2]);
// 2 element array should give back the reversed array
assert.deepEqual(lib.reverse([2,3]),[3,2]);
//tests for picking every 2nd element of array
//empty array should give me back
assert.deepEqual(lib.pickAlternateElements([]),[]);
//one element array should return me one element array
assert.deepEqual(lib.pickAlternateElements([1]),[1]);
//two element array should give me odd position element back
assert.deepEqual(lib.pickAlternateElements([1,2]),[1]);
//tests for reverse fibonacci
//0 should give me back an empty array
assert.deepEqual(lib.reverseFibonacci(0),[]);
//1 should give me back an array consisting only 0
assert.deepEqual(lib.reverseFibonacci(1),[0]);
//2 should give me back an array [1,0]
assert.deepEqual(lib.reverseFibonacci(2),[1,0]);
//tests for finding greatest number from a list
//empty list should give me undefined
//assert.deepEqual(lib.findGreatestNumber([]),0);
//one element array should give me back that number only
assert.deepEqual(lib.findGreatestNumber([1]),1);
//two number array should give me back the largest number
assert.deepEqual(lib.findGreatestNumber([1,2]),2);
//tests for finding smallest number from a list
//empty list should give me undefined
//assert.deepEqual(lib.findSmallestNumber([]),0);
//1 element array should give me back that element
assert.deepEqual(lib.findSmallestNumber([1]),1);
//2 element array should give me back smaller element
//assert.deepEqual(lib.findSmallestNumber([1,2]),1);
//test for average of a list
//empty array should give me 0
//assert.deepEqual(lib.calculateAverage([]),0);
//one element array should return me that element
assert.deepEqual(lib.calculateAverage([1]),1);
//two element array should return the average
assert.deepEqual(lib.calculateAverage([1,2]),1.5);
//test for calculating lengths of elements
//empty array should give an array holding 0
//assert.deepEqual(lib.mapLength([]),[0]);
//one string array should give me back the length of that element in an array
assert.deepEqual(lib.mapLength(["ankon"]),[5]);
//2 elements should give me back an array of the length of those elements
assert.deepEqual(lib.mapLength(["ankon","dheeraj"]),[5,7]);
//test for counting odd numbers
//an empty array should give me back 0
assert.deepEqual(lib.countOddNumbers([]),0);
//an array holding one odd number should return me 1
assert.deepEqual(lib.countOddNumbers([1]),1);
//an array holding one even number should return me 0
assert.deepEqual(lib.countOddNumbers([2]),0);
//an array holding one odd and one even number should return me 1
assert.deepEqual(lib.countOddNumbers([1,2]),1);
//test for counting even numbers
//an empty array should give me back 0
assert.deepEqual(lib.countEvenNumbers([]),0);
//an array holding one odd number should return me 0
assert.deepEqual(lib.countEvenNumbers([1]),0);
//an array holding one even number should return me 1
assert.deepEqual(lib.countEvenNumbers([2]),1);
//an array holding one odd and one even number should return me 1
assert.deepEqual(lib.countEvenNumbers([1,2]),1);
//tests for counting number above a threshold
//empty array and a threshold 0 should give me back 0
//assert.deepEqual(lib.countNumbersAbove([],0),0);
//1 element array and a threshold 0 should give me back 0
assert.deepEqual(lib.countNumbersAbove([1],0),1);
//2 element array one above and one below that threshold should give me 1
assert.deepEqual(lib.countNumbersAbove([0,2],1),1);
//tests for counting numbers below threshold
//empty array and a threshold 0 should give me back 0
assert.deepEqual(lib.countNumbersBelow([],0),0);
//1 element array and a threshold 0 should give me back 0
assert.deepEqual(lib.countNumbersBelow([1],0),0);
//2 element array one above and one below that threshold should give me 1
assert.deepEqual(lib.countNumbersBelow([0,2],1),1);
//tests for finding the index of a number
//element which is not present should give me back -1
assert.deepEqual(lib.findIndex(1,[2]),-1);
//element which is present should give me back the index of that item
assert.deepEqual(lib.findIndex(1,[1]),0);
//one duplicate element array should give me back the first index
assert.deepEqual(lib.findIndex(2,[2,2]),0);
//tests for extracting the digits of a number
//one digit number will give me back that number
assert.deepEqual(lib.extractDigits(1),[1]);
//two digit number should give me back two elements in the array
assert.deepEqual(lib.extractDigits(12),[1,2]);
//tests for finding unique elements of an array
//empty array should give me back that array
assert.deepEqual(lib.pickUnique([]),[]);
//one element array should give me back that array
assert.deepEqual(lib.pickUnique([1]),[1]);
//one duplicate element should give me back unique one
assert.deepEqual(lib.pickUnique([1,1]),[1]);
//one duplicate and one unique element should give me back two unique elements
assert.deepEqual(lib.pickUnique([1,1,2]),[1,2]);
//test for finding union of two arrays
//two empty array should return me one empty array
assert.deepEqual(lib.findUnion([],[]),[]);
//two arrays holding different element should return me array of two elements
assert.deepEqual(lib.findUnion([1],[2]),[1,2]);
//two arrays holding same element should return me array of only one elemenet
assert.deepEqual(lib.findUnion([1],[1]),[1]);
//tests for finding intersection of two arrays
//two empty array should return me one empty array
assert.deepEqual(lib.findIntersection([],[]),[]);
//two arrays holding different element should return me an empty array
assert.deepEqual(lib.findIntersection([1],[2]),[]);
//two arrays having common elements should return me only one element
assert.deepEqual(lib.findIntersection([1],[1]),[1]);
//two arrays having same and different elements should return me unique common elements
assert.deepEqual(lib.findIntersection([1,2],[2,3]),[2]);
//tests for finding difference of two arrays
//two empty array should return me an empty array
assert.deepEqual(lib.findDifference([],[]),[]);
//two arrays having different element should return me the element of the first array
assert.deepEqual(lib.findDifference([1],[2]),[1]);
//two arrays with same element should return me an empty array
assert.deepEqual(lib.findDifference([1],[1]),[]);
//two arrays with same and different elements should return me the unique elements of the first array
assert.deepEqual(lib.findDifference([1,2],[2,3]),[1]);
//tests for finding an array is subset of another array or not
//two empty array should return me true
assert.deepEqual(lib.isSubset([],[]),true);
//two arrays with same elements should return me true
assert.deepEqual(lib.isSubset([1],[1]),true);
//two arrays with different elements should return me false
assert.deepEqual(lib.isSubset([1],[2]),false);
//two arrays with same and different elements should return me false
assert.deepEqual(lib.isSubset([1,2],[2,3]),false);
//first array having the same elements of second array but in different order should return me true
assert.deepEqual(lib.isSubset([1,2,3],[3,1]),true);
//tests for zip of two arrays
//two empty array should give me back an empty array
assert.deepEqual(lib.zip([],[]),[]);
//two same length array should return me the zip of those
assert.deepEqual(lib.zip([1],[1]),[[1,1]]);
//first array of shorter length should return me the zip array of first array length
assert.deepEqual(lib.zip([1],[1,2]),[[1,1]]);
//second array of shoter length should return me the zip array of second array length
assert.deepEqual(lib.zip([1,2],[1]),[[1,1]]);
//tests for rotating an array
//an empty array being rotated by 0 should return me empty array
assert.deepEqual(lib.rotateSource([],0),[]);
//one element array rotated by 1 should return me that array
assert.deepEqual(lib.rotateSource([1],1),[1]);
//two element array rotated by 1 should return me reverse of that array
assert.deepEqual(lib.rotateSource([1,2],1),[2,1]);
//tests for partitioning array above a threshold
//an empty array should give me back an empty array
assert.deepEqual(lib.partition([],0),[[],[]]);
//an one element array below the threshold should return me that element in array
assert.deepEqual(lib.partition([1],2),[[1],[]]);
//an one element array above the threshold should return me that element in array
assert.deepEqual(lib.partition([2],1),[[],[2]]);
//array holding one element above and one element below the threshold should return me the partitioned array
assert.deepEqual(lib.partition([1,3],2),[[1],[3]]);
//test for an array is in ascending order or not
//an empty array should return me true
assert.deepEqual(lib.isAscending([]),true);
//an one element array should return me true
assert.deepEqual(lib.isAscending([1]),true);
//an ascending array should return me true
assert.deepEqual(lib.isAscending([1,2]),true);
//an descending array should return me false
assert.deepEqual(lib.isAscending([2,1]),false);
//test for an array is in descending order or not
//an empty array should return me true
assert.deepEqual(lib.isDescending([]),true);
//an one element array should return me true
assert.deepEqual(lib.isDescending([1]),true);
//an ascending array should return me false
assert.deepEqual(lib.isDescending([1,2]),false);
//an descending array should return me true
assert.deepEqual(lib.isDescending([2,1]),true);