diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-02-06 12:39:31 +0100 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2024-02-06 12:39:31 +0100 |
| commit | a04122d295a6ad6a86418a4399be4af375e195d4 (patch) | |
| tree | 38cea61662bd955745f38c14b90443e0ca850399 | |
| parent | d58258463850d2949fe13a4097a83a5d17951548 (diff) | |
| download | perlweeklychallenge-club-a04122d295a6ad6a86418a4399be4af375e195d4.tar.gz perlweeklychallenge-club-a04122d295a6ad6a86418a4399be4af375e195d4.tar.bz2 perlweeklychallenge-club-a04122d295a6ad6a86418a4399be4af375e195d4.zip | |
Add PWC 255 and fix PWC 253 (ch-2.pl)
| -rw-r--r-- | challenge-253/spadacciniweb/perl/ch-2.pl | 9 | ||||
| -rw-r--r-- | challenge-255/spadacciniweb/perl/ch-1.pl | 50 | ||||
| -rw-r--r-- | challenge-255/spadacciniweb/perl/ch-2.pl | 56 |
3 files changed, 110 insertions, 5 deletions
diff --git a/challenge-253/spadacciniweb/perl/ch-2.pl b/challenge-253/spadacciniweb/perl/ch-2.pl index e9de1b2539..3781c66a98 100644 --- a/challenge-253/spadacciniweb/perl/ch-2.pl +++ b/challenge-253/spadacciniweb/perl/ch-2.pl @@ -75,11 +75,10 @@ sub order_rows { $sum_rows{$i} = sum(@{ $matrix->[$i] }); } - printf "Output: (%s)\n", join ', ' , map { $_ } - sort { $sum_rows{$a} <=> $sum_rows{$b} - || - $a <=> $b - } keys %sum_rows; + printf "Output: (%s)\n", join ', ' , sort { $sum_rows{$a} <=> $sum_rows{$b} + || + $a <=> $b + } keys %sum_rows; return 0; } diff --git a/challenge-255/spadacciniweb/perl/ch-1.pl b/challenge-255/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..114ffd9294 --- /dev/null +++ b/challenge-255/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# Task 1: Odd Character +# Submitted by: Mohammad Sajid Anwar +# +# You are given two strings, $s and $t. The string $t is generated using the shuffled characters of the string $s with an additional character. +# Write a script to find the additional character in the string $t.. +# +# Example 1 +# Input: $s = "Perl" $t = "Preel" +# Output: "e" +# +# Example 2 +# Input: $s = "Weekly" $t = "Weeakly" +# Output: "a" +# +# Example 3 +# Input: $s = "Box" $t = "Boxy" +# Output: "y" + +use strict; +use warnings; + +my $s = "Perl"; my $t = "Preel"; +odd_character($s, $t); + +$s = "Weekly"; $t = "Weeakly"; +odd_character($s, $t); + +$s = "Box"; $t = "Boxy"; +odd_character($s, $t); + +exit 0; + +sub odd_character { + my $s = shift; + my $t = shift; + + my %freq; + foreach my $c (split //, $s) { + $freq{$c}++; + } + foreach my $c (split //, $t) { + $freq{$c}--; + } + + printf "%s | %s -> %s\n", $s, $t, map { $_ } grep { $freq{$_} == -1 }keys %freq; + + return undef; +} diff --git a/challenge-255/spadacciniweb/perl/ch-2.pl b/challenge-255/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..af98414973 --- /dev/null +++ b/challenge-255/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +# Task 2: Most Frequent Word +# Submitted by: Mohammad Sajid Anwar +# +# You are given a paragraph $p and a banned word $w. +# Write a script to return the most frequent word that is not banned. +# +# Example 1 +# Input: $p = "Joe hit a ball, the hit ball flew far after it was hit." +# $w = "hit" +# Output: "ball" +# +# The banned word "hit" occurs 3 times. +# The other word "ball" occurs 2 times. +# +# Example 2 +# Input: $p = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge." +# $w = "the" +# Output: "Perl" +# +# The banned word "the" occurs 3 times. +# The other word "Perl" occurs 2 times. + +use strict; +use warnings; + +my $paragraph = "Joe hit a ball, the hit ball flew far after it was hit."; +my $banned = "hit"; +banned_word($paragraph, $banned); + + +$paragraph = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."; +$banned = "the"; +banned_word($paragraph, $banned); + +exit 0; + +sub banned_word { + my $paragraph = shift; + my $banned = shift; + + $paragraph =~ s/[^[:word:] ]//g; + + my %freq; + foreach my $word (split / /, $paragraph) { + $freq{$word}++ + unless $word eq $banned; + } + + printf "%s | %s -> %s\n", + $paragraph, $banned, [ sort { $freq{$b} <=> $freq{$a} } + keys %freq + ]->[0]; + return undef; +} |
