diff options
| author | KjetilS <kjetilskotheim@gmail.com> | 2024-07-07 23:01:31 +0200 |
|---|---|---|
| committer | KjetilS <kjetilskotheim@gmail.com> | 2024-07-07 23:01:31 +0200 |
| commit | 82fa5491fd4215717ac39aee6556c8de5491f491 (patch) | |
| tree | d9f629fd5523e007e18fb8f4f222c8b38ac7de93 | |
| parent | 1324e20e76758ce2446c89c3dbdedc7f080266c9 (diff) | |
| download | perlweeklychallenge-club-82fa5491fd4215717ac39aee6556c8de5491f491.tar.gz perlweeklychallenge-club-82fa5491fd4215717ac39aee6556c8de5491f491.tar.bz2 perlweeklychallenge-club-82fa5491fd4215717ac39aee6556c8de5491f491.zip | |
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"; +} |
