aboutsummaryrefslogtreecommitdiff
path: root/challenge-253/athanasius/perl/ch-2.pl
blob: d4cb8ae43706e17d58f5b5fb6c0ab2c8e2937325 (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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!perl

################################################################################
=comment

Perl Weekly Challenge 253
=========================

TASK #2
-------
*Weakest Row*

Submitted by: Mohammad S Anwar

You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear
before 0.

A row i is weaker than a row j if one of the following is true:

  a) The number of 1s in row i is less than the number of 1s in row j.
  b) Both rows have the same number of 1 and i < j.

Write a script to return the order of rows from weakest to strongest.

Example 1

  Input: $matrix = [
                     [1, 1, 0, 0, 0],
                     [1, 1, 1, 1, 0],
                     [1, 0, 0, 0, 0],
                     [1, 1, 0, 0, 0],
                     [1, 1, 1, 1, 1]
                   ]
  Output: (2, 0, 3, 1, 4)

  The number of 1s in each row is:
  - Row 0: 2
  - Row 1: 4
  - Row 2: 1
  - Row 3: 2
  - Row 4: 5

Example 2

  Input: $matrix = [
                     [1, 0, 0, 0],
                     [1, 1, 1, 1],
                     [1, 0, 0, 0],
                     [1, 0, 0, 0]
                   ]
  Output: (0, 2, 3, 1)

  The number of 1s in each row is:
  - Row 0: 1
  - Row 1: 4
  - Row 2: 1
  - Row 3: 1

=cut
################################################################################

#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#

#===============================================================================
=comment

Interface
---------
If no command-line arguments are given, the test suite is run.

=cut
#===============================================================================

use v5.32.1;       # Enables strictures
use warnings;
use Const::Fast;
use Test::More;

const my $USAGE => <<END;
Usage:
  perl $0 [<matrix> ...]
  perl $0

    [<matrix> ...]    Non-empty binary matrix in which each row begins with 1
                      e.g., 11000 11110 10000 11000 11111

END

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 253, Task #2: Weakest Row (Perl)\n\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
    if (scalar @ARGV == 0)
    {
        run_tests();
    }
    else
    {
        my $matrix = parse_matrix( \@ARGV );

        print_matrix( 'Input:  $matrix = ', $matrix );

        my $ranked = rank_rows( $matrix );

        printf "Output: (%s)\n", join ', ', @$ranked;
    }
}

#-------------------------------------------------------------------------------
sub rank_rows
#-------------------------------------------------------------------------------
{
    my ($matrix) = @_;
    my  @ranked  = sort
        {
            count_ones( $matrix->[ $a ] ) <=> count_ones( $matrix->[ $b ] )
                                          ||
                                       $a <=> $b
        } 0 .. $#$matrix;

    return \@ranked;
}

#-------------------------------------------------------------------------------
sub count_ones
#-------------------------------------------------------------------------------
{
    my ($row)   = @_;
    my  $count  = 0;
        $count += $_ for @$row;

    return $count;
}

#-------------------------------------------------------------------------------
sub parse_matrix
#-------------------------------------------------------------------------------
{
    my ($matrix_strs) = @_;
    my  @matrix;
    my  $num_cols;
    
    for my $row (@$matrix_strs)
    {
        $row =~ / ^ 1 [01]* $ /x
                or error( qq["$row" is not a valid row] );

        my @row = split '', $row;

        if (defined $num_cols)
        {
            scalar @row == $num_cols
                or error( 'The input matrix is not rectangular' );
        }
        else
        {
            $num_cols = scalar @row;
        }

        push @matrix, \@row;
    }

    return \@matrix;
}

#-------------------------------------------------------------------------------
sub print_matrix
#-------------------------------------------------------------------------------
{
    my ($prefix, $matrix) = @_;
    my  $tab = ' ' x length $prefix;

    print "$prefix\[ ";

    for my $i (0 .. $#$matrix)
    {
        my $row = $matrix->[ $i ];

        printf '%s[%s]', $i == 0 ? '' : "$tab  ", join ', ', @$row;

        print  "\n" unless $i == $#$matrix;
    }

    print " ]\n";
}

#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
    print "Running the test suite\n";

    while (my $line = <DATA>)
    {
        chomp $line;

        my  ($test_name, $matrix_strs, $expected_str) = split / \| /x, $line;

        for ($test_name, $matrix_strs, $expected_str)
        {
            s/ ^ \s+   //x;
            s/   \s+ $ //x;
        }

        my @rows     = split / \s+ /x, $matrix_strs;
        my $matrix   = parse_matrix( \@rows );
        my $ranked   = rank_rows( $matrix );
        my @expected = split / \s+ /x, $expected_str;

        is_deeply $ranked, \@expected, $test_name;
    }

    done_testing;
}

#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
    my ($message) = @_;

    die "ERROR: $message\n$USAGE";
}

################################################################################

__DATA__
Example 1|11000 11110 10000 11000 11111|2 0 3 1 4
Example 2|1000  1111  1000  1000       |0 2 3 1
Singleton|1                            |0