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

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

Perl Weekly Challenge 215
=========================

TASK #1
-------
*Odd One Out*

Submitted by: Mohammad S Anwar

You are given a list of words (alphabetic characters only) of same size.

Write a script to remove all words not sorted alphabetically and print the
number of words in the list that are not alphabetically sorted.

Example 1

  Input: @words = ('abc', 'xyz', 'tsu')
  Output: 1

  The words 'abc' and 'xyz' are sorted and can't be removed.
  The word 'tsu' is not sorted and hence can be removed.

Example 2

  Input: @words = ('rat', 'cab', 'dad')
  Output: 3

  None of the words in the given list are sorted.
  Therefore all three needs to be removed.

Example 3

  Input: @words = ('x', 'y', 'z')
  Output: 0

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

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

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

Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. If $VERBOSE is set to a true value (the default), the output is followed by a
   breakdown of the sorted and unsorted words.

Assumptions
-----------
1. "Alphabetic characters" are A-Z and a-z only.
2. "Sorted alphabetically" means sorted in monotonically ascending alphabetical
   order.
3. Treatment of uppercase letters:
   a. If the constant $ASCIIBETICAL is set to a true value, uppercase letters
      rank below lowercase letters; so, e.g., "Bade" IS alphabetically sorted.
   b. Otherwise (the default), each uppercase letter is considered identical to
      its lowercase counterpart; so, "Bade" is equivalent to "bade", which is
      NOT alphabetically sorted.

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

use strict;
use warnings;
use Const::Fast;
use Test::More;

const my $ASCIIBETICAL => 0;
const my $VERBOSE      => 1;
const my $USAGE        =>
"Usage:
  perl $0 [<words> ...]
  perl $0

    [<words> ...]    Non-empty list of same-size words (chars A-Z and a-z only)
";

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 215, Task #1: Odd One Out (Perl)\n\n";
}

#===============================================================================
MAIN:
#===============================================================================
{
    if (scalar @ARGV == 0)
    {
        run_tests();
    }
    else
    {
        my $words = parse_command_line();

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

        my ($sorted, $unsorted) = partition( $words );

        print  "Sorting ASCIIbetically...\n" if $ASCIIBETICAL;

        printf "Output: %d\n", scalar @$unsorted;

        if ($VERBOSE)
        {
            printf "\nSorted:   (%s)\n", join ', ', map { qq['$_'] } @$sorted;
            printf "Unsorted: (%s)\n",   join ', ', map { qq['$_'] } @$unsorted;
        }
    }
}

#-------------------------------------------------------------------------------
sub partition
#-------------------------------------------------------------------------------
{
    my ($words) = @_;
    my (@sorted, @unsorted);

    for my $word (@$words)
    {
        my $sorted   = 1;
        my $previous = '';

        for (split //, $word)
        {
            my  $letter = $ASCIIBETICAL ? $_ : lc;

            if ($letter lt $previous)
            {
                $sorted = 0;
                last;
            }

            $previous = $letter;
        }

        push @{ $sorted ? \@sorted : \@unsorted }, $word;
    }

    return (\@sorted, \@unsorted);
}

#-------------------------------------------------------------------------------
sub parse_command_line
#-------------------------------------------------------------------------------
{
    my $first = $ARGV[ 0 ];

    for (@ARGV)
    {
        length == length $first
            or error( 'The input words are not all of the same size' );
    }

    return \@ARGV;
}

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

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

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

        SKIP:
        {
            skip 'This test requires $ASCIIBETICAL to be False'
                if $ASCIIBETICAL && $test_name =~ / case /x;

            s/ \s+ $ //x for $test_name, $words;       # Trim whitespace

            my  @words              = split / , /x, $words;
            my ($sorted, $unsorted) = partition( \@words );

            is scalar( @$unsorted ), $expected, $test_name;
        }
    }

    done_testing;
}

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

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

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

__DATA__
Example 1 |abc,xyz,tsu                 |1
Example 2 |rat,cab,dad                 |3
Example 3 |x,y,z                       |0
Repeats   |beet,allow,abbot,boot,redder|1
Capitals  |ABC,XYZ,TSU                 |1
Mixed case|Bade,abcd,Abcd              |1