diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-06-10 17:54:42 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-06-10 17:54:42 -0400 |
| commit | 2bdd0742da7c7e81a343f506f0f6ba71225fc1fa (patch) | |
| tree | 0acf35956eb124aae7763c56a99555b8d6795e9b | |
| parent | 514530d6be7cc5067a95a11ebedff9e0d4d46cfe (diff) | |
| download | perlweeklychallenge-club-2bdd0742da7c7e81a343f506f0f6ba71225fc1fa.tar.gz perlweeklychallenge-club-2bdd0742da7c7e81a343f506f0f6ba71225fc1fa.tar.bz2 perlweeklychallenge-club-2bdd0742da7c7e81a343f506f0f6ba71225fc1fa.zip | |
DAJ 273
| -rw-r--r-- | challenge-273/dave-jacoby/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-273/dave-jacoby/perl/ch-2.pl | 35 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-273/dave-jacoby/perl/ch-1.pl b/challenge-273/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..eb1de94c52 --- /dev/null +++ b/challenge-273/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +use List::Util qw{max}; + +my @examples = ( + + { str => "perl", char => "e" }, + { str => "java", char => "a" }, + { str => "python", char => "m" }, + { str => "ada", char => "a" }, + { str => "ballerina", char => "l" }, + { str => "analitik", char => "k" }, +); +for my $example (@examples) { + my $char = $example->{char}; + my $str = $example->{str}; + my @str = split //, $str; + my $count = scalar grep { $_ eq $char } @str; + my $total = scalar @str; + my $output = round( 100 * $count / $total ); + say <<"END"; +Input: \$str = "$str", \$char = "$char" +Output: "$output" +END +} + +sub round ($number) { + my $int = int $number; + return $number if $number == $int; + my $r = $number - $int; + return $r < 0.5 ? $int : $int + 1; +} diff --git a/challenge-273/dave-jacoby/perl/ch-2.pl b/challenge-273/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..8d4420e62f --- /dev/null +++ b/challenge-273/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + "aabb", + "abab", + "aaa", + "bbb", +); + +for my $example (@examples) { + my $output = baftera($example); + + say <<"END"; +Input: \$str = "$example" +Output: $output +END +} + +sub baftera ($str) { + return 'false' unless $str =~ /b/mix; + my $has_b = 0; + for my $i ( 0 .. length $str ) { + my $char = substr $str, $i, 1; + $has_b = 1 if $char eq 'b'; + return 'false' if $has_b && $char eq 'a'; + } + return 'true'; +} |
