aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/noud/raku/ch-1.p6
blob: 570ab00efe54604cb6b04e5e6752e40182827d60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# You are given a linked list and a value k. Write a script to partition the
# linked list such that all nodes less than k come before nodes greater than or
# equal to k. Make sure you preserve the original relative order of the nodes
# in each of the two partitions.
#
# For example:
#
# Linked List: 1 → 4 → 3 → 2 → 5 → 2
#
# k = 3
#
# Expected Output: 1 → 2 → 2 → 4 → 3 → 5.

sub partition(@l, $k) {
    return [|(@l.grep({ $_ < $k })), |(@l.grep({ $_ >= $k }))];
}

say partition([1, 4, 3, 2, 5, 2], 3).join(' → ');