diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2022-09-04 13:51:05 +0100 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2022-09-04 13:51:05 +0100 |
| commit | bd7299957268fdcda38c666bdeeb9e4a377e36f5 (patch) | |
| tree | ce13f5904fea10d5e16f15399da1b1183ae0e072 | |
| parent | d4fb734fa6a97b50c3cf0655475fe3c34801ce55 (diff) | |
| download | perlweeklychallenge-club-bd7299957268fdcda38c666bdeeb9e4a377e36f5.tar.gz perlweeklychallenge-club-bd7299957268fdcda38c666bdeeb9e4a377e36f5.tar.bz2 perlweeklychallenge-club-bd7299957268fdcda38c666bdeeb9e4a377e36f5.zip | |
add solution week 180 task 1 in perl
| -rw-r--r-- | challenge-180/steven-wilson/perl/ch-01.pl | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-180/steven-wilson/perl/ch-01.pl b/challenge-180/steven-wilson/perl/ch-01.pl new file mode 100644 index 0000000000..60bf86968e --- /dev/null +++ b/challenge-180/steven-wilson/perl/ch-01.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +# Week 180 Task 1 +# Write a script to find out the first unique character in the given string +# and print its index (0-based). + +use strict; +use warnings; +use Test::More; + +ok(first_unique_character("Perl Weekly Challenge") == 0, "First"); +ok(first_unique_character("Long Live Perl") == 1, "Second"); +ok(first_unique_character("LOLOLx") == 5, "Last"); +ok(!defined(first_unique_character("LOLO")), "None"); +done_testing(); + +sub first_unique_character { + my $s = shift; + my $len = length $s; + my $index = 0; + while ( $index < $len) { + my $c = substr $s, $index, 1; + my $number = () = $s =~ /$c/gi; + if ( $number == 1){ + last; + } + $index++; + if ($index == $len) { + undef $index; + last; + } + } + return $index; +}
\ No newline at end of file |
