aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/dave-jacoby/perl/ch-1.pl
blob: 03264098929342043bb71d78d3564864005db4b4 (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
#!/usr/bin/env perl

use strict;
use warnings;
use utf8;
use feature qw{ postderef say signatures state switch };
no warnings qw{ experimental };

my $k     = 3;
my $input = [ 1, 4, 3, 2, 5, 2 ];

say display_ll($input);
my $output = task_1( $k, $input );
say display_ll($output);

# the "simple" version, not using actual linked lists
# but not doing anything you can't regularly due with 
# singly-linked lists, basically shift/remove-first
# and push/add-last. Adding the first element of the 
# second linked list to the end of the first linked 
# list makes it a longer linked list.

# I think I'll have to make my Node code into an actual
# linked list eventually

sub task_1 ( $k, $array ) {
    my $output = [];
    my @below ;
    my @above ;
    while ( $array->@* ) {
        my $l = shift $array->@*;
        if ( $l < $k ) {
            push @below, $l;
            next;
        }
        push @above, $l;
    }
    push $output->@*, @below, @above;
    return $output;
}

sub display_ll($array) {
    return join ' -> ', $array->@*;
}