diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-29 18:12:18 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-29 18:12:18 +0100 |
| commit | aea8592c381796b11d5ff8531f0730d03adc285b (patch) | |
| tree | d278945a6c31de8388f288bc0e2eeb1f5b965171 /challenge-280 | |
| parent | 2f03062f0c16f2a539eff07cc0b77170edacea94 (diff) | |
| parent | 788b78b9dfb1dab102893603bd4e083edacc5be1 (diff) | |
| download | perlweeklychallenge-club-aea8592c381796b11d5ff8531f0730d03adc285b.tar.gz perlweeklychallenge-club-aea8592c381796b11d5ff8531f0730d03adc285b.tar.bz2 perlweeklychallenge-club-aea8592c381796b11d5ff8531f0730d03adc285b.zip | |
Merge pull request #10516 from pme/challenge-280
challenge-280
Diffstat (limited to 'challenge-280')
| -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; + |
