diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-06-10 20:25:38 +0200 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-06-10 20:25:38 +0200 |
| commit | c1f56716a6b7db5c435b20a154e903535f1d710d (patch) | |
| tree | 219ff2acf65068aff53332879fe0b1d1418a34da | |
| parent | 514530d6be7cc5067a95a11ebedff9e0d4d46cfe (diff) | |
| download | perlweeklychallenge-club-c1f56716a6b7db5c435b20a154e903535f1d710d.tar.gz perlweeklychallenge-club-c1f56716a6b7db5c435b20a154e903535f1d710d.tar.bz2 perlweeklychallenge-club-c1f56716a6b7db5c435b20a154e903535f1d710d.zip | |
Add solution 273.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-273/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-273/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-273/jeanluc2020/perl/ch-1.pl | 74 | ||||
| -rwxr-xr-x | challenge-273/jeanluc2020/perl/ch-2.pl | 68 |
4 files changed, 144 insertions, 0 deletions
diff --git a/challenge-273/jeanluc2020/blog-1.txt b/challenge-273/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..7ee7b3dc94 --- /dev/null +++ b/challenge-273/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-273-1.html diff --git a/challenge-273/jeanluc2020/blog-2.txt b/challenge-273/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..9bcbd79a15 --- /dev/null +++ b/challenge-273/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-273-2.html diff --git a/challenge-273/jeanluc2020/perl/ch-1.pl b/challenge-273/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..cec9e25e69 --- /dev/null +++ b/challenge-273/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/#TASK1 +# +# Task 1: Percentage of Character +# =============================== +# +# You are given a string, $str and a character $char. +# +# Write a script to return the percentage, nearest whole, of given character in +# the given string. +# +## Example 1 +## +## Input: $str = "perl", $char = "e" +## Output: 25 +# +## Example 2 +## +## Input: $str = "java", $char = "a" +## Output: 50 +# +## Example 3 +## +## Input: $str = "python", $char = "m" +## Output: 0 +# +## Example 4 +## +## Input: $str = "ada", $char = "a" +## Output: 67 +# +## Example 5 +## +## Input: $str = "ballerina", $char = "l" +## Output: 22 +# +## Example 6 +## +## Input: $str = "analitik", $char = "k" +## Output: 13 +# +############################################################ +## +## discussion +## +############################################################ +# +# We split $str into its characters and count both the overall +# sum and the sum of the characters that match $char. We calculate +# the percentage, add 0.5 and convert to an integer. This way, we +# obtain the nearest whole percentage. + +use strict; +use warnings; + +percentage_of_char("perl", "e"); +percentage_of_char("java", "a"); +percentage_of_char("python", "m"); +percentage_of_char("ada", "a"); +percentage_of_char("ballerina", "l"); +percentage_of_char("analitik", "k"); + +sub percentage_of_char { + my ($str, $char) = @_; + print "Input: '$str', '$char'\n"; + my $overall_char_count = 0; + my $this_char_count = 0; + foreach my $c (split //, $str) { + $overall_char_count++; + $this_char_count++ if $c eq $char; + } + my $percentage = int(100*$this_char_count/$overall_char_count + 0.5); + print "Output: $percentage\n"; +} diff --git a/challenge-273/jeanluc2020/perl/ch-2.pl b/challenge-273/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..fcf2ad08c2 --- /dev/null +++ b/challenge-273/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/#TASK2 +# +# Task 2: B After A +# ================= +# +# You are given a string, $str. +# +# Write a script to return true if there is at least one b, and no a appears +# after the first b. +# +## Example 1 +## +## Input: $str = "aabb" +## Output: true +# +## Example 2 +## +## Input: $str = "abab" +## Output: false +# +## Example 3 +## +## Input: $str = "aaa" +## Output: false +# +## Example 4 +## +## Input: $str = "bbb" +## Output: true +# +############################################################ +## +## discussion +## +############################################################ +# +# We walk the string character for character. If we find a "b" +# we remember this fact. If we encounter an "a" and have already +# seen a "b", we return false. If we're at the end of the string +# our output depends on whether or not we have seen a "b": we +# didn't bail out for seeing an "a" after a "b", so if we didn't +# see any "b" at all, we return false, otherwise true. + +use strict; +use warnings; + +b_after_a("aabb"); +b_after_a("abab"); +b_after_a("aaa"); +b_after_a("bbb"); + +sub b_after_a { + my $str = shift; + print "Input: '$str'\n"; + my $b_seen = 0; + foreach my $char (split //, $str) { + $b_seen = 1 if $char eq "b"; + if($b_seen) { + return print "Output: false\n" if $char eq "a"; + } + } + if($b_seen) { + print "Output: true\n"; + } else { + print "Output: false\n"; + } +} |
