blob: 99eadcb5fe7d82f75ae179cffe0b594bc7990612 (
plain)
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
|
#!/usr/bin/env perl
# Challenge 059
#
# TASK #1 � Linked List
# Reviewed by Ryan Thompson
# 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.
use Modern::Perl;
use HOP::Stream qw( list_to_stream iterator_to_stream head tail );
my($k, @n) = @ARGV;
my $in = list_to_stream(@n);
my $out = iterator_to_stream(partition_it($k, $in));
my @out;
while ($out) {
my $head = head($out);
$out = tail($out);
push @out, $head;
}
say join(" -> ", @out);
sub partition_it {
my($k, $in) = @_;
my @pending;
return sub {
while ($in) {
my $head = head($in);
$in = tail($in);
if ($head < $k) {
return $head;
}
else {
push @pending, $head;
}
}
while (@pending) {
my $head = shift @pending;
return $head;
}
return;
};
}
|