-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-the-minimum.js
65 lines (52 loc) · 2.42 KB
/
remove-the-minimum.js
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
/**
* https://www.codewars.com/kata/563cf89eb4747c5fb100001b
*
Description:
The museum of incredible dull things
The museum of incredible dull things wants to get rid of some exhibitions.
Miriam, the interior architect, comes up with a plan to remove the most
boring exhibitions. She gives them a rating, and then removes the one with
the lowest rating.
However, just as she finished rating all exhibitions, she's off to an important
fair, so she asks you to write a program that tells her the ratings of the items
after one removed the lowest one. Fair enough.
Task
Given an array of integers, remove the smallest value. If there are multiple
elements with the same value, remove the one with a lower index. If you get
an empty array/list, return an empty array/list.
Don't change the order of the elements that are left.
*/
let tests = require('./lib/framework.js');
let Test = tests.Test, describe = tests.describe, it = tests.it, before = tests.before, after = tests.after;
function removeSmallest(numbers) {
let min, index;
if (numbers.length) {
min = Math.min.apply(null, numbers);
numbers.splice(numbers.indexOf(min), 1);
}
return numbers;
}
Test.describe("removeSmallest", function() {
Test.it("works for the examples", function() {
Test.assertSimilar(removeSmallest([1, 2, 3, 4, 5]), [2, 3, 4, 5], "Wrong result for [1, 2, 3, 4, 5]");
Test.assertSimilar(removeSmallest([5, 3, 2, 1, 4]), [5, 3, 2, 4], "Wrong result for [5, 3, 2, 1, 4]");
Test.assertSimilar(removeSmallest([2, 2, 1, 2, 1]), [2, 2, 2, 1], "Wrong result for [2, 2, 1, 2, 1]");
Test.assertSimilar(removeSmallest([]), [], "Wrong result for []");
});
Test.it("returns [] if the list has only one element", function() {
for (let i = 0; i < 10; ++i) {
let x = ~~(Math.random() * 400);
Test.assertSimilar(removeSmallest([x]), [], `Wrong result for ${[x]}`);
}
});
function randomArray(length) {
return Array.from({length: length}, () => ~~(Math.random() * 400));
}
Test.it("returns a list that misses only one element", function() {
for(let i = 0; i < 10; ++i) {
let arr = randomArray(~~(Math.random() * 10) + 1);
let l = arr.length;
Test.assertSimilar(removeSmallest(arr).length, l - 1, `Wrong result for ${arr}`);
}
});
});