-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-1.raku
193 lines (148 loc) · 6.15 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
use v6d;
################################################################################
=begin comment
Perl Weekly Challenge 266
=========================
TASK #1
-------
*Uncommon Words*
Submitted by: Mohammad Sajid Anwar
You are given two sentences, $line1 and $line2.
Write a script to find all uncommmon words in any order in the given two sen-
tences. Return ('') if none found.
A word is uncommon if it appears exactly once in one of the sentences and
doesn't appear in other sentence.
Example 1
Input: $line1 = 'Mango is sweet'
$line2 = 'Mango is sour'
Output: ('sweet', 'sour')
Example 2
Input: $line1 = 'Mango Mango'
$line2 = 'Orange'
Output: ('Orange')
Example 3
Input: $line1 = 'Mango is Mango'
$line2 = 'Orange is Orange'
Output: ('')
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=begin comment
Assumptions
-----------
1. Word-matching within and between sentences is case-insensitive.
2. Singular and plural forms are treated as distinct words.
3. Two or more hyphens together are treated as a word-separating dash.
4. A single hyphen is assumed to be "hard" and is retained within a word.
5. The possessive forms ending in "'s" or "'" are ignored when matching words.
6. Other punctuation symbols (commas, full stops, colons, semicolons, question
marks, exclamation marks, single quotes, and double quotes) are treated as
word-separators but otherwise ignored.
Interface
---------
1. If no command-line arguments are given, the test suite is run. Otherwise:
2. The input sentences are entered as two strings on the command-line.
=end comment
#===============================================================================
use Test;
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
"\nChallenge 266, Task #1: Uncommon Words (Raku)\n".put;
}
#===============================================================================
multi sub MAIN
(
Str:D $line1, #= The first sentence
Str:D $line2 #= The second sentence
)
#===============================================================================
{
"Input: \$line1 = '$line1'".put;
" \$line2 = '$line2'".put;
my Str @uncommon-words = find-uncommon-words( $line1, $line2 );
"Output: (%s)\n".printf: @uncommon-words.elems == 0 ?? "''" !!
@uncommon-words.map( { "'$_'" } ).join: ', ';
}
#===============================================================================
multi sub MAIN() # No input: run the test suite
#===============================================================================
{
run-tests();
}
#-------------------------------------------------------------------------------
sub find-uncommon-words( Str:D $line1, Str:D $line2 --> List:D[Str:D] )
#-------------------------------------------------------------------------------
{
my Str @words = filter-words( $line1 );
@words.push: |filter-words( $line2 );
my UInt %dict;
++%dict{ $_ } for @words.map: { .lc };
my Str @uncommon;
for @words -> Str $word
{
@uncommon.push: $word if %dict{ $word.lc } == 1;
}
return @uncommon;
}
#-------------------------------------------------------------------------------
sub filter-words( Str:D $line --> List:D[Str:D] )
#-------------------------------------------------------------------------------
{
my Str $l = $line; # Make a local copy
$l ~~ s:g/ \- ** 2..* / /; # Convert dashes to spaces
$l ~~ s:g/ <?after \w > \' s? //; # Remove possessive markers
$l ~~ s:g/ <[.,;:!?'"]> / /; # Convert punctuation to spaces
my Str @words = $l.split: / \s+ /, :skip-empty; # Find (non-empty) words
return @words;
}
#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
'Running the test suite'.put;
for test-data.lines -> Str $line
{
my Str ($test-name, $line1, $line2, $expected-str) =
$line.split: / \| /;
for $test-name, $line1, $line2, $expected-str
{
s/ ^ \s+ //;
s/ \s+ $ //;
}
my Str @uncommon-words = find-uncommon-words( $line1, $line2 );
my Str @expected-words = $expected-str.split: / \s+ /, :skip-empty;
is-deeply @uncommon-words, @expected-words, $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 |Mango is sweet |Mango is sour |sweet sour
Example 2 |Mango Mango |Orange |Orange
Example 3 |Mango is Mango |Orange is Orange |
Case |The cat is on the mat|A dog is on a log |cat mat dog log
Plurals |This dog likes dogs |This dog likes cats |dogs cats
Possessives|Pete is my son's son |Pete is my grandson |grandson
Dash |Sue--my favourite. |Is Sue my favourite? |Is
Hyphen |Implement: egg-beater|Egg implement |egg-beater Egg
Punctuation|Sue has a book. |Does Sue have a book?|has Does have
Quotes |"Mango" is sweet |Mango is 'sour' |sweet sour
END
}
################################################################################