diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-07 22:05:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-07 22:05:05 +0100 |
| commit | 757bec959ede495aea7053ebae49541691b14466 (patch) | |
| tree | 0435619919fda1a94e33039e996ff1daaf2353d1 | |
| parent | d79d16dfcc3c1f21e0699644f4cdb08401f4a2fe (diff) | |
| parent | 82fa5491fd4215717ac39aee6556c8de5491f491 (diff) | |
| download | perlweeklychallenge-club-757bec959ede495aea7053ebae49541691b14466.tar.gz perlweeklychallenge-club-757bec959ede495aea7053ebae49541691b14466.tar.bz2 perlweeklychallenge-club-757bec959ede495aea7053ebae49541691b14466.zip | |
Merge pull request #10384 from kjetillll/challenge-273-kjetillll
https://theweeklychallenge.org/blog/perl-weekly-challenge-273/
| -rw-r--r-- | challenge-273/kjetillll/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-273/kjetillll/perl/ch-2.pl | 18 |
2 files changed, 39 insertions, 0 deletions
diff --git a/challenge-273/kjetillll/perl/ch-1.pl b/challenge-273/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..405468fd37 --- /dev/null +++ b/challenge-273/kjetillll/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; use warnings; use Test::More tests=>6; + +sub percentage_of_char { + my( $str, $char ) = @_; + my $count = () = $str =~ /\Q$char/g; + int( 0.5 + 100 * $count / length $str ); +} + +my @tests = <<'' =~ /"(.*?)".*?"(.*?)".*?(\d+)/g; + Input: $str = "perl", $char = "e" Output: 25 + Input: $str = "java", $char = "a" Output: 50 + Input: $str = "python", $char = "m" Output: 0 + Input: $str = "ada", $char = "a" Output: 67 + Input: $str = "ballerina", $char = "l" Output: 22 + Input: $str = "analitik", $char = "k" Output: 13 + +while( @tests ){ + my( $str, $char, $output) = splice @tests, 0, 3; + my $p = percentage_of_char($str,$char); + is $p, $output, sprintf "test str: %15s char: %s want: %2d got: %2d", $str, $char, $output, $p; +} diff --git a/challenge-273/kjetillll/perl/ch-2.pl b/challenge-273/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..7e0cb71e38 --- /dev/null +++ b/challenge-273/kjetillll/perl/ch-2.pl @@ -0,0 +1,18 @@ +use strict; use warnings; use Test::More tests=>4; + +sub b_after_a { + local $_ = shift; + s/^.*?b// and /b/ and !/a/ +} + +my @tests = <<'' =~ /"(.*?)".*?(true|false)/g; + Input: $str = "aabb" Output: true + Input: $str = "abab" Output: false + Input: $str = "aaa" Output: false + Input: $str = "bbb" Output: true + +while( @tests ){ + my( $str, $output ) = splice @tests, 0, 2; + my $got = b_after_a( $str ) ? 'true' : 'false'; + is $got, $output, "want: $output got: $got"; +} |
