Skip to content

Commit ffa4846

Browse files
trasherdkRuSaG0
andauthored
merge: Add FindMinIterator algorithm (TheAlgorithms#928)
* FindMinIterator Do the `standard` thing. Rename `FindMin` to `FindMinIterator` Rename to `FindMinIterator` Pull `FindMin` from `master` * Remove these separator comments. Co-authored-by: RuSaG0 <mirzoev-ruslan-2000@mail.ru>
1 parent 1992425 commit ffa4846

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Maths/FindMinIterator.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @function FindMinIterator
3+
* @description Function to find the minimum number given in an array.
4+
*/
5+
6+
const FindMinIterator = (_iterable, _selector = undefined) => {
7+
let min
8+
9+
const iterator = _iterable[Symbol.iterator]()
10+
if (!_selector) {
11+
let current = iterator.next()
12+
if (current.done) { return undefined }
13+
min = current.value
14+
15+
current = iterator.next()
16+
while (!current.done) {
17+
const x = current.value
18+
if (x < min) { min = x }
19+
current = iterator.next()
20+
}
21+
} else {
22+
let current = iterator.next()
23+
if (current.done) { return undefined }
24+
min = _selector(current.value)
25+
26+
current = iterator.next()
27+
while (!current.done) {
28+
const x = _selector(current.value)
29+
if (x < min) { min = x }
30+
current = iterator.next()
31+
}
32+
}
33+
return min
34+
}
35+
36+
export { FindMinIterator }

Maths/test/FindMinIterator.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { FindMinIterator } from '../FindMinIterator'
2+
3+
describe('FindMinIterator', () => {
4+
test('given empty array then min is undefined', () => {
5+
expect(FindMinIterator([])).toBeUndefined()
6+
})
7+
8+
test('given single value array then min is found', () => {
9+
expect(FindMinIterator([1])).toBe(1)
10+
expect(FindMinIterator([-1])).toBe(-1)
11+
expect(FindMinIterator([0])).toBe(0)
12+
})
13+
14+
test('given array then min is found', () => {
15+
expect(FindMinIterator([1, 2])).toBe(1)
16+
expect(FindMinIterator([-1, 10])).toBe(-1)
17+
expect(FindMinIterator([0, 100])).toBe(0)
18+
expect(FindMinIterator([100, 0])).toBe(0)
19+
expect(FindMinIterator([100, 50, 20, 0, -100, 0, 2, 30, 45, 99, 104, 23])).toBe(-100)
20+
})
21+
22+
test('given empty generator then min is undefined', () => {
23+
const src = function* () { } // eslint-disable-line
24+
expect(FindMinIterator(src())).toBeUndefined()
25+
})
26+
27+
test('given generator then min is found', () => {
28+
const src = function* () { // eslint-disable-line
29+
yield 1
30+
yield -1
31+
yield 0
32+
}
33+
expect(FindMinIterator(src())).toBe(-1)
34+
})
35+
36+
test('given string generator then min string length is found', () => {
37+
const src = function* () { // eslint-disable-line
38+
yield 'abc'
39+
yield 'de'
40+
yield 'qwerty'
41+
}
42+
expect(FindMinIterator(src(), _x => _x.length)).toBe(2)
43+
})
44+
45+
test('given array of objects then min accessor is found', () => {
46+
const array = [
47+
{ name: 'Item #1', price: 1.0 },
48+
{ name: 'Item #2', price: 0.0 },
49+
{ name: 'Item #3', price: -1.0 }
50+
]
51+
expect(FindMinIterator(array, _x => _x.price)).toBe(-1)
52+
expect(FindMinIterator(array, _x => _x.name)).toBe('Item #1')
53+
})
54+
})

0 commit comments

Comments
 (0)