aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/saiftynet/perl/ch-1.pl
blob: 31e750d415d560963cf5d1ae08e4b8fcf81d77e8 (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
#!/usr/env/perl
# Task 1 Challenge 059 Solution by saiftynet

# Linked List: PWC 059
# k = 3# Expected Output: 1 → 2 → 2 → 4 → 3 → 5.
# given a "linked list" partition about a number, but retain
# relative order of other numbers

my @l=qw/1 4 3 2 5 2/;

print  join "->",partition(3);

sub partition{

# get the pivot and list
  my ($k,@list)=@_;   

# prepare anonymnous lists containing equal, after and before numbers
  my @seq=([],[],[]); 

# <=> return -1 if less (before), 0 if equal (pivot) and 1 of greater (after);
# use this result as an index to push the numbers into one of these lists
  foreach my $t (@l){
    push @{$seq[$t<=>$k]},$t
  };

# return partitioned data
  return @{$seq[-1]},@{$seq[0]},@{$seq[1]};

}