aboutsummaryrefslogtreecommitdiff
path: root/challenge-326/athanasius/perl/ch-2.pl
blob: df3461bc77706599fe195999d0c405b784914f57 (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
#!perl

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

Perl Weekly Challenge 326
=========================

TASK #2
-------
*Decompressed List*

Submitted by: Mohammad Sajid Anwar

You are given an array of positive integers having even elements.

Write a script to return the decompress[ed] list. To decompress, pick adjacent
pair (i, j) and replace it with j, i times.

Example 1

  Input: @ints = (1, 3, 2, 4)
  Output: (3, 4, 4)

  Pair 1: (1, 3) => 3 one time  => (3)
  Pair 2: (2, 4) => 4 two times => (4, 4)

Example 2

  Input: @ints = (1, 1, 2, 2)
  Output: (1, 2, 2)

  Pair 1: (1, 1) => 1 one time  => (1)
  Pair 2: (2, 2) => 2 two times => (2, 2)

Example 3

  Input: @ints = (3, 1, 3, 2)
  Output: (1, 1, 1, 2, 2, 2)

  Pair 1: (3, 1) => 1 three times => (1, 1, 1)
  Pair 2: (3, 2) => 2 three times => (2, 2, 2)

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

#--------------------------------------#
# Copyright © 2025 PerlMonk Athanasius #
#--------------------------------------#

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

Assumption
----------
"Positive" integers are greater than zero.

Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. A non-empty, even-sized list of positive integers is entered on the command-
   line.

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

use v5.32;         # Enables strictures
use warnings;
use Const::Fast;
use List::Util     qw( pairs );
use Regexp::Common qw( number  );
use Test::More;

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

    [<ints> ...]    A non-empty, even-sized list of positive integers
END

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 326, Task #2: Decompressed List (Perl)\n\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
    my $argc = scalar @ARGV;

    if    ($argc == 0)
    {
        run_tests();
    }
    elsif ($argc % 2 == 0)
    {
        my @ints = @ARGV;

        for (@ints)
        {
            / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] );
            $_ >  0                or error( "$_ is not positive" );
        }

        printf "Input:  \@ints = (%s)\n", join ', ', @ints;

        my $decompressed = decompress( \@ints );

        printf "Output: (%s)\n",          join ', ', @$decompressed;
    }
    else
    {
        error( 'The input list is not even in size' );
    }
}

#-------------------------------------------------------------------------------
sub decompress
#-------------------------------------------------------------------------------
{
    my ($ints) = @_;
    my  @decompressed;

    for my $pair (pairs @$ints)
    {
        push @decompressed, ($pair->value) x $pair->key;
    }

    return \@decompressed;
}

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

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

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

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

        my @ints         = split / \s+ /x, $ints_str;
        my $decompressed = decompress( \@ints );
        my @expected     = split / \s+ /x, $expected_str;

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

    done_testing;
}

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

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

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

__DATA__
Example 1|1 3 2 4|3 4 4
Example 2|1 1 2 2|1 2 2
Example 3|3 1 3 2|1 1 1 2 2 2