Skip to content

Commit 402ab5c

Browse files
committed
Migrate doctest for BeadSort.js
(also remove inline test driver code)
1 parent e7836e4 commit 402ab5c

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

Sorts/BeadSort.js

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
/**
2-
* Bead Sort, also known as Gravity sort, this algorithm was
3-
* inspired from natural phenomenons and was designed keeping in mind objects(or beads)
4-
* falling under the influence of gravity.
2+
* Bead Sort, also known as Gravity sort.
3+
*
4+
* This algorithm was inspired from natural phenomena and was designed keeping in mind objects (or beads) falling under
5+
* the influence of gravity.
56
*
67
* NOTE: It only works for arrays of positive integers.
78
*
89
* Wikipedia: https://en.wikipedia.org/wiki/Bead_sort
910
*/
10-
11-
/**
12-
* Doctests
13-
*
14-
* > beadSort([5, 4, 3, 2, 1])
15-
* [1, 2, 3, 4, 5]
16-
* > beadSort([7, 9, 4, 3, 5])
17-
* [3, 4, 5, 7, 9]
18-
* > beadSort([-1, 5, 8, 4, 3, 19])
19-
* ! RangeError: Sequence must be a list of positive integers!
20-
*/
21-
22-
function beadSort (sequence) {
11+
export function beadSort (sequence) {
2312
/* Let's ensure our sequence has only Positive Integers */
2413
if (sequence.some((integer) => integer < 0)) {
2514
throw RangeError('Sequence must be a list of Positive integers Only!')
@@ -60,23 +49,8 @@ function beadSort (sequence) {
6049
}
6150

6251
/* Finally, let's turn our Bead rows into their Respective Numbers */
63-
const sortedSequence = grid.map((beadArray) => {
52+
return grid.map((beadArray) => {
6453
const beadsArray = beadArray.filter(bead => bead === '*')
65-
6654
return beadsArray.length
6755
})
68-
69-
return sortedSequence
7056
}
71-
72-
/**
73-
* Implementation of Bead Sort
74-
*/
75-
const array = [5, 4, 3, 2, 1]
76-
// Before Sort
77-
console.log('\n- Before Sort | Implementation of Bead Sort -')
78-
console.log(array)
79-
// After Sort
80-
console.log('- After Sort | Implementation of Bead Sort -')
81-
console.log(beadSort(array))
82-
console.log('\n')

Sorts/test/BeadSort.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { beadSort } from '../BeadSort'
2+
3+
describe('BeadSort', () => {
4+
it('should sort arrays correctly', () => {
5+
expect(beadSort([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5])
6+
expect(beadSort([7, 9, 4, 3, 5])).toEqual([3, 4, 5, 7, 9])
7+
})
8+
9+
it('should throw a RangeError when the array contains negative integers', () => {
10+
expect(() => beadSort([-1, 5, 8, 4, 3, 19])).toThrow(RangeError)
11+
})
12+
})

0 commit comments

Comments
 (0)