aboutsummaryrefslogtreecommitdiff
path: root/challenge-081
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-10-07 22:23:17 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-10-07 22:23:17 +0100
commit796001952cdb0d6517968b065cfdb18f08dee950 (patch)
treed89c33c9cd5bf9832520226b7e24ac99a3f695b6 /challenge-081
parent1e141eaf9b2c0b2d765e33ee40f59b89dee1627f (diff)
downloadperlweeklychallenge-club-796001952cdb0d6517968b065cfdb18f08dee950.tar.gz
perlweeklychallenge-club-796001952cdb0d6517968b065cfdb18f08dee950.tar.bz2
perlweeklychallenge-club-796001952cdb0d6517968b065cfdb18f08dee950.zip
- Added Perl solution to Frequency Sort task.
Diffstat (limited to 'challenge-081')
-rw-r--r--challenge-081/mohammad-anwar/perl/ch-2.pl58
-rw-r--r--challenge-081/mohammad-anwar/perl/input3
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-081/mohammad-anwar/perl/ch-2.pl b/challenge-081/mohammad-anwar/perl/ch-2.pl
new file mode 100644
index 0000000000..2f9a27fbe5
--- /dev/null
+++ b/challenge-081/mohammad-anwar/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+
+#
+# Perl Weekly Challenge - 081
+#
+# Task #2: Frequency Count
+#
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-081
+#
+
+use strict;
+use warnings;
+
+frequency_count(fetch_words($ARGV[0]));
+
+#
+#
+# SUBROUTINES
+
+sub fetch_words {
+ my ($file) = @_;
+
+ open(my $fh, "<:encoding(UTF-8)", $file)
+ or die "ERROR: Unable to open $file: $!\n";
+
+ my %words = ();
+ while (my $line = <$fh>) {
+ chomp $line;
+ $line =~ s/\.//g;
+ $line =~ s/\"//g;
+ $line =~ s/\(//g;
+ $line =~ s/\)//g;
+ $line =~ s/\,//g;
+ $line =~ s/\'s//g;
+ $line =~ s/\-\-/ /g;
+ foreach my $word (split /\s/, $line) {
+ $words{$word} += 1;
+ }
+ }
+
+ close($fh);
+
+ return \%words;
+}
+
+sub frequency_count {
+ my ($words) = @_;
+
+ my %frequency = ();
+ foreach my $word (keys %$words) {
+ $frequency{$words->{$word}} .= " " . $word;
+ }
+
+ foreach my $count (sort { $a <=> $b } keys %frequency) {
+ my @words = split / /, $frequency{$count};
+ printf("%d%s\n", $count, join(" ", sort @words));
+ }
+}
diff --git a/challenge-081/mohammad-anwar/perl/input b/challenge-081/mohammad-anwar/perl/input
new file mode 100644
index 0000000000..37001629ad
--- /dev/null
+++ b/challenge-081/mohammad-anwar/perl/input
@@ -0,0 +1,3 @@
+West Side Story
+
+The award-winning adaptation of the classic romantic tragedy "Romeo and Juliet". The feuding families become two warring New York City gangs, the white Jets led by Riff and the Latino Sharks, led by Bernardo. Their hatred escalates to a point where neither can coexist with any form of understanding. But when Riff's best friend (and former Jet) Tony and Bernardo's younger sister Maria meet at a dance, no one can do anything to stop their love. Maria and Tony begin meeting in secret, planning to run away. Then the Sharks and Jets plan a rumble under the highway--whoever wins gains control of the streets. Maria sends Tony to stop it, hoping it can end the violence. It goes terribly wrong, and before the lovers know what's happened, tragedy strikes and doesn't stop until the climactic and heartbreaking ending.