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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#!perl
###############################################################################
=comment
Perl Weekly Challenge 197
=========================
TASK #1
-------
*Move Zero*
Submitted by: Mohammad S Anwar
You are given a list of integers, @list.
Write a script to move all zero, if exists, to the end while maintaining the
relative order of non-zero elements.
Example 1
Input: @list = (1, 0, 3, 0, 0, 5)
Output: (1, 3, 5, 0, 0, 0)
Example 2
Input: @list = (1, 6, 4)
Output: (1, 6, 4)
Example 3
Input: @list = (0, 1, 0, 2, 0)
Output: (1, 2, 0, 0, 0)
=cut
###############################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=comment
Interface
---------
If no command-line arguments are given, the test suite is run.
Algorithm
---------
1. Traverse the input list, counting zero values but copying non-zero values to
the output list.
2. Append the number of zero values encountered in the input list to the end of
the output list.
-- The repetition operator 'x' in list context "returns a list consisting of
the left operand list repeated the number of times specified by the right
operand" (https://perldoc.perl.org/perlop#Multiplicative-Operators).
=cut
#==============================================================================
use strict;
use warnings;
use Const::Fast;
use Regexp::Common qw( number );
use Test::More;
const my $TEST_FIELDS => 3;
const my $USAGE =>
"Usage:
perl $0 [<list> ...]
perl $0
[<list> ...] A list of 1 or more integers\n";
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 197, Task #1: Move Zero (Perl)\n\n";
}
#==============================================================================
MAIN:
#==============================================================================
{
my $args = scalar @ARGV;
if ($args == 0)
{
run_tests();
}
else
{
my @list = @ARGV;
for (@list)
{
/ ^ $RE{num}{int} $ /x
or die qq[ERROR: "$_" is not a valid integer\n$USAGE];
}
printf "Input: \@list = (%s)\n", join ', ', @list;
printf "Output: (%s)\n", join ', ', move_zero( @list );
}
}
#------------------------------------------------------------------------------
sub move_zero
#------------------------------------------------------------------------------
{
my @list = @_;
my $count = 0;
my @moved;
for my $n (@list)
{
if ($n == 0)
{
++$count;
}
else
{
push @moved, $n;
}
}
push @moved, (0) x $count;
return @moved;
}
#------------------------------------------------------------------------------
sub run_tests
#------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $input, $expected) = split /\|/, $line, $TEST_FIELDS;
$input =~ s/ ^ \s* (.+?) \s* $ /$1/x; # Trim whitespace
$expected =~ s/ ^ \s* (.+?) \s* $ /$1/x;
$expected =~ s/ \s+ / /gx;
my @list = split / , \s+ /x, $input;
my $got = join ', ', move_zero( @list );
is $got, $expected, $test_name;
}
done_testing;
}
###############################################################################
__DATA__
Example 1| 1, 0, 3, 0, 0, 5 | 1, 3, 5, 0, 0, 0
Example 2| 1, 6, 4 | 1, 6, 4
Example 3| 0, 1, 0, 2, 0 | 1, 2, 0, 0, 0
Negatives|-1, 0, -2, -3, 0, 0, -4|-1, -2, -3, -4, 0, 0, 0
|