diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-06-13 09:46:25 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-06-13 09:46:25 +0200 |
| commit | dd679b917c37fbc332647940157e31e431557005 (patch) | |
| tree | c0738ba6f8802b4929bf4c63e394f12492eaad20 | |
| parent | ce654cb8a5a89ad19b858b600e63e606aa8db195 (diff) | |
| download | perlweeklychallenge-club-dd679b917c37fbc332647940157e31e431557005.tar.gz perlweeklychallenge-club-dd679b917c37fbc332647940157e31e431557005.tar.bz2 perlweeklychallenge-club-dd679b917c37fbc332647940157e31e431557005.zip | |
Add ch-1 and ch-2 in Perl
| -rw-r--r-- | challenge-273/spadacciniweb/perl/ch-1.pl | 68 | ||||
| -rw-r--r-- | challenge-273/spadacciniweb/perl/ch-2.pl | 55 |
2 files changed, 123 insertions, 0 deletions
diff --git a/challenge-273/spadacciniweb/perl/ch-1.pl b/challenge-273/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..86d0c9ccc8 --- /dev/null +++ b/challenge-273/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +# Task 1: Percentage of Character +# Submitted by: Mohammad Sajid Anwar +# +# 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 + +use strict; +use warnings; + +my $str = "perl"; my $char = "e"; +percentage($str, $char); + +$str = "java"; $char = "a"; +percentage($str, $char); + +$str = "python"; $char = "m"; +percentage($str, $char); + +$str = "ada"; $char = "a"; +percentage($str, $char); + +$str = "ballerina"; $char = "l"; +percentage($str, $char); + +$str = "analitik"; $char = "k"; +percentage($str, $char); + +exit 0; + +sub percentage { + my $str = shift; + my $char = shift; + + my %freq; + $freq{$_}++ + foreach split //, $str; + printf "str %s char %s -> perc %.0f\n", + $str, $char, + $freq{$char} + ? $freq{$char}/length($str)*100 + : 0; +} diff --git a/challenge-273/spadacciniweb/perl/ch-2.pl b/challenge-273/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..a7c968b0fd --- /dev/null +++ b/challenge-273/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl + +# Task 2: B After A +# Submitted by: Mohammad Sajid Anwar +# +# 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 + +use strict; +use warnings; + +my $str = "aabb"; +position($str); + +$str = "abab"; +position($str); + +$str = "aaa"; +position($str); + +$str = "bbb"; +position($str); + +exit 0; + +sub position { + my $str = shift; + + my $first_b = index($str, 'b'); + my $last_a = index((scalar reverse $str), 'a'); + $last_a = length($str) - $last_a - 1 + if $last_a >= 0; + + printf "%s -> %s\n", + $str, + ($last_a > $first_b) + ? 'false' + : 'true'; +} |
