JavaScript
- 1 ответ
- 0 вопросов
0
Вклад в тег
let insert = n => (a, r = [ ], [h, ...t] = a) =>
a.length == 0 ? [n]
: n > a[a.length - 1] ? [...a, n]
: n <= h ? [...r, n, ...a]
: insert(n)(t, [...r, h])
let f = insert(0)
let g = insert(5)
let h = insert(11)
let a = [ ]
let b = [2, 4, 7, 10]
f(a) //=> [<0>]
f(b) //=> [<0>, 2, 4, 7, 10]
g(b) //=> [2, 4, <5>, 7, 10]
h(b) //=> [2, 4, 7, 10, <11>]