/
ch-1.raku
198 lines (150 loc) · 5.63 KB
/
ch-1.raku
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
use v6d;
################################################################################
=begin 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
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=begin comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. If VERBOSE is set to True (the default), the output is followed by a break-
down 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 True, 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.
=end comment
#===============================================================================
use Test;
my Bool constant ASCIIBETICAL = False;
my Bool constant VERBOSE = True;
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
"\nChallenge 215, Task #1: Odd One Out (Raku)\n".put;
}
#===============================================================================
multi sub MAIN
(
#| Non-empty list of same-size words (chars A-Z and a-z only)
*@words where { .elems > 0 && @words[ 0 ].chars == .all.chars }
)
#===============================================================================
{
"Input: \@words = (%s)\n".printf: @words.map( { qq['$_'] } ).join: ', ';
my Array[Str] ($sorted, $unsorted) = partition( @words );
'Sorting ASCIIbetically...'.put if ASCIIBETICAL;
"Output: %d\n"\.printf: $unsorted.elems;
if VERBOSE
{
"\nSorted: (%s)\n".printf: $sorted.map( { qq['$_'] } ).join: ', ';
"Unsorted: (%s)\n".printf: $unsorted.map( { qq['$_'] } ).join: ', ';
}
}
#===============================================================================
multi sub MAIN() # No input: run the test suite
#===============================================================================
{
run-tests();
}
#-------------------------------------------------------------------------------
sub partition( Array:D[Str:D] $words --> List:D[List:D[Str:D], List:D[Str:D]] )
#-------------------------------------------------------------------------------
{
my Str (@sorted, @unsorted);
for @$words -> Str $word
{
my Bool $sorted = True;
my Str $previous = '';
for $word.split: '', :skip-empty
{
my Str $letter = ASCIIBETICAL ?? $_ !! .lc;
if $letter lt $previous
{
$sorted = False;
last;
}
$previous = $letter;
}
($sorted ?? @sorted !! @unsorted).push: $word;
}
return @sorted, @unsorted;
}
#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
'Running the test suite'.put;
for test-data.lines -> Str $line
{
my Str ($test-name, $words, $expected) = $line.split: / \| /;
if ASCIIBETICAL && $test-name ~~ / case /
{
skip 'This test requires "ASCIIBETICAL" to be False';
}
else
{
s/ \s+ $ // for $test-name, $words; # Trim whitespace
my Str @words = $words.split: ',';
my Array[Str] ($sorted, $unsorted) = partition( @words );
is $unsorted.elems, $expected.Int, $test-name;
}
}
done-testing;
}
#-------------------------------------------------------------------------------
sub USAGE()
#-------------------------------------------------------------------------------
{
my Str $usage = $*USAGE;
$usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
$usage.put;
}
#-------------------------------------------------------------------------------
sub test-data( --> Str:D )
#-------------------------------------------------------------------------------
{
return q:to/END/;
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
END
}
################################################################################