/
ch-1.raku
227 lines (175 loc) · 6.4 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
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
use v6d;
################################################################################
=begin comment
Perl Weekly Challenge 256
=========================
TASK #1
-------
*Maximum Pairs*
Submitted by: Mohammad Sajid Anwar
You are given an array of distinct words, @words.
Write a script to find the maximum pairs in the given array. The words $words[i]
and $words[j] can be a pair one is reverse of the other.
Example 1
Input: @words = ("ab", "de", "ed", "bc")
Output: 1
There is one pair in the given array: "de" and "ed"
Example 2
Input: @words = ("aa", "ba", "cd", "ed")
Output: 0
Example 3
Input: @words = ("uv", "qp", "st", "vu", "mn", "pq"))
Output: 2
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=begin comment
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. If --case-sensitive is omitted or set to True, "xy" and "XY" (for example)
are treated as different words, so "xy" and "YX" do NOT form a pair; but if
--case-sensitive=False is entered on the command-line, "xy" DOES pair with
"YX".
3. If --palindromes is entered on the command-line, palindromes such as "a",
"aba", and "CDEDC" pair with themselves; otherwise, they do not, as per
Example 2.
4. If VERBOSE is set to True, the required output (number of pairs found) is
followed by a list of the pairs themselves.
=end comment
#===============================================================================
use Test;
my Bool constant CASE-SENSITIVE = True;
my Bool constant PALINDROMES = False;
my Bool constant VERBOSE = True;
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
"\nChallenge 256, Task #1: Maximum Pairs (Raku)\n".put;
}
#===============================================================================
multi sub MAIN
(
Bool:D :$case-sensitive = CASE-SENSITIVE,
Bool:D :$palindromes = PALINDROMES,
#| A non-empty array of distinct words
*@words where { .elems > 0 && are-distinct( @words, $case-sensitive ) }
)
#===============================================================================
{
"Input: \@words = (%s)\n".printf: @words.map( { qq["$_"] } ).join: ', ';
my Pair @pairs = count-pairs( @words, $case-sensitive, $palindromes );
"Output: %d\n".printf: @pairs.elems;
if VERBOSE
{
"\nPair%s%s\n".printf: @pairs.elems == 1 ?? ': ' !! 's: ',
@pairs.map( { .key ~ '|' ~ .value } ).join: ', ';
}
}
#===============================================================================
multi sub MAIN() # No input: run the test suite
#===============================================================================
{
run-tests();
}
#-------------------------------------------------------------------------------
sub count-pairs
(
List:D[Str:D] $words,
Bool:D $case-sensitive,
Bool:D $palindromes
--> List:D[Pair:D]
)
#-------------------------------------------------------------------------------
{
my Pair @pairs;
my Bool %seen = @$words.map: { $_ => False };
for @$words.sort -> Str $lhs
{
if !%seen{ $lhs }
{
for @$words.sort -> Str $rhs
{
next if %seen{ $rhs } || (!$palindromes && $lhs eq $rhs);
if ($case-sensitive && $lhs eq $rhs.flip) ||
(!$case-sensitive && $lhs.lc eq $rhs.lc.flip)
{
@pairs.push: $lhs => $rhs;
%seen{ $lhs } = True;
%seen{ $rhs } = True;
}
}
}
}
return @pairs;
}
#-------------------------------------------------------------------------------
sub are-distinct( List:D[Str:D] $words, Bool:D $case-sensitive --> Bool:D )
#-------------------------------------------------------------------------------
{
my UInt %dict;
for @$words
{
my Str $word = $case-sensitive ?? $_ !! .lc;
return False if ++%dict{ $word } > 1;
}
return True;
}
#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
'Running the test suite'.put;
for test-data.lines -> Str $line
{
my Str ($test-name, $words-str, $expected, $case-sensitive-str,
$palindromes-str) = $line.split: / \| /;
for $test-name, $words-str, $expected, $case-sensitive-str,
$palindromes-str
{
s/ ^ \s+ //;
s/ \s+ $ //;
}
my Bool $case-sensitive = $case-sensitive-str eq 'True';
my Bool $palindromes = $palindromes-str eq 'True';
my Str @words = $words-str.split: / \s+ /, :skip-empty;
my Pair @pairs = count-pairs( @words, $case-sensitive, $palindromes );
is @pairs.elems, $expected.Int, $test-name;
}
done-testing;
}
#-------------------------------------------------------------------------------
sub error( Str:D $message )
#-------------------------------------------------------------------------------
{
"ERROR: $message".put;
USAGE();
exit 0;
}
#-------------------------------------------------------------------------------
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 |ab de ed bc |1| |
Example 2 |aa ba cd ed |0| |
Example 3 |uv qp st vu mn pq|2| |
Palindromes 1|aba ab ba cd ef g|3| |True
Palindromes 2|aba ab ba cd ef g|1| |False
Case 1 |ab BA cD dC ef gh|2|False|
Case 2 |ab BA cD dC ef gh|0|True |
END
}
################################################################################