diff options
| -rwxr-xr-x | challenge-280/peter-meszaros/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-280/peter-meszaros/perl/ch-2.pl | 61 |
2 files changed, 119 insertions, 0 deletions
diff --git a/challenge-280/peter-meszaros/perl/ch-1.pl b/challenge-280/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..7548986710 --- /dev/null +++ b/challenge-280/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Twice Appearance + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str, containing lowercase English letters only. + +Write a script to print the first letter that appears twice. + +=head2 Example 1 + + Input: $str = "acbddbca" + Output: "d" + +=head2 Example 2 + + Input: $str = "abccd" + Output: "c" + +=head2 Example 3 + + Input: $str = "abcdabbb" + Output: "a" + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ['acbddbca', 'd', 'Example 1'], + ['abccd', 'c', 'Example 2'], + ['abcdabbb', 'a', 'Example 3'], +]; + +sub twice_apperance +{ + my $str = shift; + + my %h; + for my $c (split //, $str) { + if (defined $h{$c}) { + return $c; + } else { + $h{$c} = 1; + } + } +} + +for (@$cases) { + is(twice_apperance($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-280/peter-meszaros/perl/ch-2.pl b/challenge-280/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..f3bbf65cd5 --- /dev/null +++ b/challenge-280/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Count Asterisks + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str, where every two consecutive vertical bars are +grouped into a pair. + +Write a script to return the number of asterisks, *, excluding any between each +pair of vertical bars. + +=head2 Example 1 + + Input: $str = "p|*e*rl|w**e|*ekly|" + Ouput: 2 + +The characters we are looking here are "p" and "w**e". + +=head2 Example 2 + + Input: $str = "perl" + Ouput: 0 + +=head2 Example 3 + + Input: $str = "th|ewe|e**|k|l***ych|alleng|e" + Ouput: 5 + +The characters we are looking here are "th", "e**", "l***ych" and "e". + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + + +my $cases = [ + ['p|*e*rl|w**e|*ekly|', 2, 'Example 1'], + ['perl', 0, 'Example 2'], + ['th|ewe|e**|k|l***ych|alleng|e', 5, 'Example 3'], +]; + +sub count_asterisks +{ + my $str = shift; + + my @str = split(/\|/, $str); + $str = join '', @str[grep { not $_ % 2 } 0 .. $#str]; + return $str =~ tr/*//; +} + +for (@$cases) { + is(count_asterisks($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
