aboutsummaryrefslogtreecommitdiff
path: root/challenge-333/deadmarshal/perl/ch-2.pl
blob: 5f9aecb8bd7cde98a12ab519e367cec39dfb78ab (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
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub duplicate_zeros{
  my ($arr) = @_;
  my ($i,@stk) = (0);
  while($i < @$arr && @stk != @$arr){
    if($arr->[$i] == 0 && (@stk+1 < @$arr)) {
      push @stk,0 for 0..1;
      $i++
    } else {
      push @stk,$arr->[$i++]
    }
  }
  @stk
}

print show duplicate_zeros([1,0,2,3,0,4,5,0]);
print show duplicate_zeros([1,2,3]);
print show duplicate_zeros([1,2,3,0]);
print show duplicate_zeros([0,0,1,2]);
print show duplicate_zeros([1,2,0,3,4]);