-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathch-1.pl
188 lines (148 loc) · 4.43 KB
/
ch-1.pl
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
#!perl
###############################################################################
=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
=cut
###############################################################################
#--------------------------------------#
# Copyright © 2022 PerlMonk Athanasius #
#--------------------------------------#
#==============================================================================
=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 a true value, a short explanation is
appended to the output (as per the Examples).
=cut
#==============================================================================
use strict;
use warnings;
use Const::Fast;
use Test::More;
const my $VERBOSE => 1;
const my $USAGE =>
"Usage:
perl $0 <s>
perl $0
<s> A non-empty string containing at least one letter\n";
#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 180, Task #1: First Unique Character (Perl)\n\n";
}
#==============================================================================
MAIN:
#==============================================================================
{
my $args = scalar @ARGV;
if ($args == 0)
{
run_tests();
}
elsif ($args == 1)
{
my $s = $ARGV[ 0 ];
$s =~ / [[:alpha:]] /x or error( 'No letters in the input string' );
solve( $s );
}
else
{
error( "Expected 0 or 1 command line arguments, found $args" );
}
}
#------------------------------------------------------------------------------
sub solve
#------------------------------------------------------------------------------
{
my ($s) = @_;
print qq[Input: \$s = "$s"\n];
my $index = find_index( $s );
if ($VERBOSE)
{
if (defined $index)
{
printf qq[Output: %s (as "%s" is the first unique letter)\n],
$index, substr( $s, $index, 1 );
}
else
{
print "Output: None (as no letters are unique)\n";
}
}
else
{
printf "Output: %s\n", defined $index ? $index : 'None';
}
}
#------------------------------------------------------------------------------
sub find_index
#------------------------------------------------------------------------------
{
my ($s) = @_;
my %chars;
for my $char (split //, $s)
{
++$chars{ lc $char } if $char =~ / ^ [[:alpha:]] $ /x;
}
my $index;
for my $i (0 .. length( $s ) - 1)
{
my $key = lc substr( $s, $i, 1 );
if (exists $chars{ $key } && $chars{ $key } == 1)
{
$index = $i;
last;
}
}
return $index;
}
#------------------------------------------------------------------------------
sub run_tests
#------------------------------------------------------------------------------
{
my @tests =
(
[ 'Perl Weekly Challenge', 0 ],
[ 'Long Live Perl', 1 ],
[ 'AaBbCcDdEeFfGgHhIiJj', undef ], # All letters are duplicated
[ 'AaBbCcDdEeFfGgHhIiJjK', 20 ],
[ ' abB', 1 ], # Ignore space because not letter
);
for my $test (@tests)
{
my $test_name = '"' . $test->[ 0 ] . '"';
is find_index( $test->[ 0 ] ), $test->[ 1 ], $test_name;
}
done_testing;
}
#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
###############################################################################