-
Notifications
You must be signed in to change notification settings - Fork 315
/
ch-1.raku
177 lines (131 loc) · 5.07 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
use v6d;
################################################################################
=begin comment
Perl Weekly Challenge 255
=========================
TASK #1
-------
*Odd Character*
Submitted by: Mohammad Sajid Anwar
You are given two strings, $s and $t. The string $t is generated using the
shuffled characters of the string $s with an additional character.
Write a script to find the additional character in the string $t.
Example 1
Input: $s = "Perl" $t = "Preel"
Output: "e"
Example 2
Input: $s = "Weekly" $t = "Weeakly"
Output: "a"
Example 3
Input: $s = "Box" $t = "Boxy"
Output: "y"
=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 set to True, "P" and "p" (for example) are treated as
different characters; otherwise, they are treated as the same character.
=end comment
#===============================================================================
use Test;
subset Char of Str where { .chars == 1 };
my Bool constant ASSERT = True; # Perform optional sanity checks
my Bool constant CASE-SENSITIVE = True;
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
"\nChallenge 255, Task #1: Odd Character (Raku)\n".put;
}
#===============================================================================
multi sub MAIN
(
Str:D $s, #= A string
Str:D $t #= A string containing the chars of s with 1 additional char
)
#===============================================================================
{
qq[Input: \$s = "$s" \$t = "$t"].put;
my Char $odd-char = find-odd-character( $s, $t );
qq[Output: "$odd-char"].put;
}
#===============================================================================
multi sub MAIN() # No input: run the test suite
#===============================================================================
{
run-tests();
}
#-------------------------------------------------------------------------------
sub find-odd-character( Str:D $s, Str:D $t --> Char:D )
#-------------------------------------------------------------------------------
{
$t.chars == $s.chars + 1
or error( '$t must be exactly 1 character longer than $s' );
my BagHash[Char] $s-chars = BagHash[Char].new:
$s.split( '', :skip-empty ).map: { CASE-SENSITIVE ?? $_ !! .lc };
my BagHash[Char] $t-chars = BagHash[Char].new:
$t.split( '', :skip-empty ).map: { CASE-SENSITIVE ?? $_ !! .lc };
$s-chars ⊂ $t-chars # Strict subset
or error( 'The chars in $s are not a subset of the chars in $t' );
my Pair @odd-chars = $t-chars (-) $s-chars; # Set difference
if ASSERT # Sanity checks
{
@odd-chars.elems > 0 or error( 'No odd character found' );
@odd-chars.elems == 1 or error( 'More than 1 odd char found' );
@odd-chars[ 0 ].value == 1 or error( 'More than 1 odd character "' ~
@odd-chars[ 0 ].key ~ '" found' );
}
return @odd-chars[ 0 ].key;
}
#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
'Running the test suite'.put;
for test-data.lines -> Str $line
{
my Str ($test-name, $s, $t, $expected) = $line.split: / \| /;
for $test-name, $s, $t, $expected
{
s/ ^ \s+ //;
s/ \s+ $ //;
}
my Char $odd-char = find-odd-character( $s, $t );
is $odd-char, $expected, $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|Perl |Preel |e
Example 2|Weekly|Weeakly|a
Example 3|Box |Boxy |y
Empty | |q |q
END
}
################################################################################