From 82fa5491fd4215717ac39aee6556c8de5491f491 Mon Sep 17 00:00:00 2001 From: KjetilS Date: Sun, 7 Jul 2024 23:01:31 +0200 Subject: https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ --- challenge-273/kjetillll/perl/ch-1.pl | 21 +++++++++++++++++++++ challenge-273/kjetillll/perl/ch-2.pl | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 challenge-273/kjetillll/perl/ch-1.pl create mode 100644 challenge-273/kjetillll/perl/ch-2.pl 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"; +} -- cgit