-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-1.pl
executable file
·64 lines (43 loc) · 1.14 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
#!/usr/bin/perl -s
use v5.16;
use Test2::V0;
use experimental 'signatures';
our ($tests, $examples);
run_tests() if $tests || $examples; # does not return
die <<EOS unless @ARGV;
usage: $0 [-examples] [-tests] [STR]
-examples
run the examples from the challenge
-tests
run some tests
STR
string to be examined
EOS
### Input and Output
say first_uniq(shift);
### Implementation
sub first_uniq ($str) {
# Count each character's occurrences.
my %cnt;
$cnt{$_}++ for split //, $str;
# Return the index of the first unique character.
$cnt{substr $str, $_, 1} == 1 && return $_ for 0 .. length($str) - 1;
# If there is no unique character in the string:
-1;
}
### Examples and tests
sub run_tests {
SKIP: {
skip "examples" unless $examples;
is first_uniq('Perl Weekly Challenge'), 0, 'example 1';
is first_uniq('Long Live Perl'), 1, 'example 2';
}
SKIP: {
skip "tests" unless $tests;
is first_uniq('aa'), -1, 'no unique character';
is first_uniq(''), -1, 'empty string';
is first_uniq('aa0'), 2, 'find zero';
}
done_testing;
exit;
}