aboutsummaryrefslogtreecommitdiff
path: root/challenge-081
diff options
context:
space:
mode:
authorNuno Vieira <nunovieira220@gmail.com>2020-10-08 00:21:23 +0100
committerNuno Vieira <nunovieira220@gmail.com>2020-10-08 00:21:23 +0100
commitad08b2b87cc19ab0320df5ed8f72cb7471edb078 (patch)
tree092e39d01dac1ce5eaee9b9c690e9f8df9dd006e /challenge-081
parent651819edcb48d39cc2fdaec1d0eed76ccf264e13 (diff)
downloadperlweeklychallenge-club-ad08b2b87cc19ab0320df5ed8f72cb7471edb078.tar.gz
perlweeklychallenge-club-ad08b2b87cc19ab0320df5ed8f72cb7471edb078.tar.bz2
perlweeklychallenge-club-ad08b2b87cc19ab0320df5ed8f72cb7471edb078.zip
Add nunovieira220 perl solution to challenge 081
Diffstat (limited to 'challenge-081')
-rw-r--r--challenge-081/nunovieira220/perl/ch-1.pl22
-rw-r--r--challenge-081/nunovieira220/perl/ch-2.pl36
-rw-r--r--challenge-081/nunovieira220/perl/input.txt3
3 files changed, 61 insertions, 0 deletions
diff --git a/challenge-081/nunovieira220/perl/ch-1.pl b/challenge-081/nunovieira220/perl/ch-1.pl
new file mode 100644
index 0000000000..1c33299ac0
--- /dev/null
+++ b/challenge-081/nunovieira220/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use List::Util qw[min max];
+
+# Input
+my $A = "abcdabcd";
+my $B = "abcdabcdabcdabcd";
+
+# Common Base String
+my @res = ();
+my $len = min(length($A), length($B));
+
+for (my $i = 0; $i < $len; $i++) {
+ my $base = substr($A, 0, $i+1);
+
+ push(@res, $base) if($A =~ /^($base)+$/ && $B =~ /^($base)+$/);
+}
+
+# Output
+print join(', ', @res); \ No newline at end of file
diff --git a/challenge-081/nunovieira220/perl/ch-2.pl b/challenge-081/nunovieira220/perl/ch-2.pl
new file mode 100644
index 0000000000..5bb3b73619
--- /dev/null
+++ b/challenge-081/nunovieira220/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use File::Basename;
+
+# Input
+open(my $fh, "<", dirname(__FILE__)."/input.txt") or die "Can't open < input.txt: $!";
+
+my %sums = ();
+my %freqs = ();
+
+# Get word frenquencies from file
+while(<$fh>) {
+ chomp;
+ s/--/ /g;
+ my @words = split(/ /, $_);
+
+ for(@words) {
+ $_ =~ s/\.|"|'s|,|\(|\)//g;
+ $sums{$_} = $sums{$_} ? $sums{$_} + 1 : 1;
+ }
+}
+
+# Join words by frequency
+for(sort keys %sums) {
+ $freqs{$sums{$_}} = () if(!$freqs{$sums{$_}});
+
+ push @{$freqs{$sums{$_}}}, $_;
+}
+
+# Output
+for(sort keys %freqs) {
+ my $res = join(', ', @{$freqs{$_}});
+ print $_.": ".$res."\n";
+} \ No newline at end of file
diff --git a/challenge-081/nunovieira220/perl/input.txt b/challenge-081/nunovieira220/perl/input.txt
new file mode 100644
index 0000000000..d2bb45d308
--- /dev/null
+++ b/challenge-081/nunovieira220/perl/input.txt
@@ -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. \ No newline at end of file