aboutsummaryrefslogtreecommitdiff
path: root/challenge-201/athanasius/perl/ch-1.pl
blob: 82f6ae226902914dfe926af5b95dc2205e72125f (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
#!perl

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

Perl Weekly Challenge 201
=========================

TASK #1
-------
*Missing Numbers*

Submitted by: Mohammad S Anwar

You are given an array of unique numbers.

Write a script to find out all missing numbers in the range 0..$n where $n is
the array size.

Example 1

  Input: @array = (0,1,3)
  Output: 2

  The array size i.e. total element count is 3, so the range is 0..3.
  The missing number is 2 in the given array.

Example 2

  Input: @array = (0,1)
  Output: 2

  The array size is 2, therefore the range is 0..2.
  The missing number is 2.

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

#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#

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

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

Assumption
----------
Numbers in the input array are integers.

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

use strict;
use warnings;
use Const::Fast;
use Regexp::Common qw( number );
use Test::More;

const my $USAGE =>
"Usage:
  perl $0 [<array> ...]
  perl $0

    [<array> ...]    A list of 1 or more unique integers\n";

#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 201, Task #1: Missing Numbers (Perl)\n\n";
}

#==============================================================================
MAIN:
#==============================================================================
{
    if (scalar @ARGV == 0)
    {
        run_tests();
    }
    else
    {
        my @array = validate_input();

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

        my $missing = find_missing_numbers( \@array );

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

#------------------------------------------------------------------------------
sub validate_input
#------------------------------------------------------------------------------
{
    my %element_counts;

    for (@ARGV)
    {
        / ^ $RE{num}{int} $ /x                       # Entry is a valid integer
            or error( qq["$_" is not a valid integer] );

        ++$element_counts{ $_ } == 1                          # Entry is unique
            or error( "Duplicate ${_}s in the input array" )
    }

    return @ARGV;
}

#------------------------------------------------------------------------------
sub find_missing_numbers
#------------------------------------------------------------------------------
{
    my ($array) = @_;
    my  $n      = scalar @$array;
    my  @missing;

    L_OUTER:
    for my $m (0 .. $n)
    {
        for my $i (0 .. $#$array)
        {
            next L_OUTER if $array->[ $i ] == $m;
        }

        push @missing, $m;
    }

    return \@missing;
}

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

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

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

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

        my ($test_name, $input, $expected) = split / \| /x, $line;

        my @array   = split / , \s* /x, $input;
        my $missing = find_missing_numbers( \@array );
        my $got     = join ',', @$missing;

        is $got, $expected, $test_name;
    }

    done_testing;
}

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

__DATA__
Example 1| 0, 1, 3          |2
Example 2| 0, 1             |2
Multi    | 0, 1, 3, 5, 9, 10|2,4,6
Negatives|-4,-3,-5, 4       |0,1,2,3
Single   | 0                |1
Evens    | 1, 3, 5, 7       |0,2,4