-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathch-1.raku
163 lines (124 loc) · 4.19 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
use v6d;
###############################################################################
=begin comment
Perl Weekly Challenge 180
=========================
TASK #1
-------
*First Unique Character*
Submitted by: Mohammad S Anwar
You are given a string, $s.
Write a script to find out the first unique character in the given string and
print its index (0-based).
Example 1
Input: $s = "Perl Weekly Challenge"
Output: 0 as 'P' is the first unique character
Example 2
Input: $s = "Long Live Perl"
Output: 1 as 'o' is the first unique character
=end comment
###############################################################################
#--------------------------------------#
# Copyright © 2022 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=begin comment
Assumptions
-----------
1. "Characters" are LETTERS only; whitespace, punctuation, and digits are
ignored.
2. Matching of characters (i.e., letters) is case-INsensitive.
Interface
---------
1. If no string argument is provided on the command line, a small test suite is
run.
2. If the constant $VERBOSE is set to True, a short explanation is appended to
the output (as per the Examples).
Note
----
Use of /[<:ASCII> & <.alpha>]/ in place of /<[A..Za..z]>/ is documented here:
https://www.codesections.com/blog/raku-unicode/
=end comment
#==============================================================================
use Test;
subset TestT of List where (Str, UInt);
my Bool constant $VERBOSE = True;
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
"\nChallenge 180, Task #1: First Unique Character (Raku)\n".put;
}
#==============================================================================
multi sub MAIN
(
#| A non-empty string containing at least one letter
Str:D $s where { / [<:ASCII> & <.alpha>] / }
)
#==============================================================================
{
qq[Input: \$s = "$s"].put;
my UInt $index = find-index( $s );
if $VERBOSE
{
if $index.defined
{
qq[Output: %s (as "%s" is the first unique character)\n].printf:
$index, $s.substr( $index, 1 );
}
else
{
'Output: None (as no characters are unique)'.put;
}
}
else
{
"Output: %s\n".printf: $index.defined ?? $index !! 'None';
}
}
#==============================================================================
multi sub MAIN() # Run tests
#==============================================================================
{
my TestT @tests = [ 'Perl Weekly Challenge', 0 ],
[ 'Long Live Perl', 1 ],
[ 'AaBbCcDdEeFfGgHhIiJj', UInt ], # All duplicated
[ 'AaBbCcDdEeFfGgHhIiJjK', 20 ],
[ ' abB', 1 ]; # Ignore space
for @tests -> TestT $test
{
my Str $test-name = '"' ~ $test[ 0 ] ~ '"';
is find-index( $test[ 0 ] ), $test[ 1 ], $test-name;
}
done-testing;
}
#------------------------------------------------------------------------------
sub find-index( Str:D $s --> UInt )
#------------------------------------------------------------------------------
{
my UInt %chars;
for $s.split( '', :skip-empty ) -> Str $char
{
++%chars{ $char.lc } if $char ~~ / ^ [<:ASCII> & <.alpha>] $ /;
}
my UInt $index;
for 0 .. $s.chars - 1 -> UInt $i
{
my Str $key = $s.substr( $i, 1 ).lc;
if %chars{ $key }:exists && %chars{ $key } == 1
{
$index = $i;
last;
}
}
return $index;
}
#------------------------------------------------------------------------------
sub USAGE()
#------------------------------------------------------------------------------
{
my Str $usage = $*USAGE;
$usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
$usage.put;
}
###############################################################################