aboutsummaryrefslogtreecommitdiff
path: root/challenge-081
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-08 14:30:02 +0100
committerGitHub <noreply@github.com>2020-10-08 14:30:02 +0100
commit51daffdce5159b3a435351a4b22dff2f61885e3a (patch)
treeae51339ad5eeddf37c771260318b1b09098a030f /challenge-081
parent651819edcb48d39cc2fdaec1d0eed76ccf264e13 (diff)
parent1c2a38e61c7dcf97185d5835b9f08e9dbf17179b (diff)
downloadperlweeklychallenge-club-51daffdce5159b3a435351a4b22dff2f61885e3a.tar.gz
perlweeklychallenge-club-51daffdce5159b3a435351a4b22dff2f61885e3a.tar.bz2
perlweeklychallenge-club-51daffdce5159b3a435351a4b22dff2f61885e3a.zip
Merge pull request #2476 from nunovieira220/challenge-081
Add solution to challenge 081
Diffstat (limited to 'challenge-081')
-rw-r--r--challenge-081/nunovieira220/js/ch-1.js17
-rw-r--r--challenge-081/nunovieira220/js/ch-2.js25
-rw-r--r--challenge-081/nunovieira220/js/input.txt3
-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
6 files changed, 106 insertions, 0 deletions
diff --git a/challenge-081/nunovieira220/js/ch-1.js b/challenge-081/nunovieira220/js/ch-1.js
new file mode 100644
index 0000000000..2582e1b9a3
--- /dev/null
+++ b/challenge-081/nunovieira220/js/ch-1.js
@@ -0,0 +1,17 @@
+// Input
+const A = 'abcdabcd';
+const B = 'abcdabcdabcdabcd';
+
+// Common Base String
+const res = [];
+const len = Math.min(A.length, B.length);
+
+for (let i = 0; i < len; i++) {
+ const base = A.substring(0, i + 1);
+ const regex = new RegExp(`^(${base})+$`);
+
+ if(A.match(regex) && B.match(regex)) res.push(base);
+}
+
+// Output
+console.log(res); \ No newline at end of file
diff --git a/challenge-081/nunovieira220/js/ch-2.js b/challenge-081/nunovieira220/js/ch-2.js
new file mode 100644
index 0000000000..9d76ffd1a5
--- /dev/null
+++ b/challenge-081/nunovieira220/js/ch-2.js
@@ -0,0 +1,25 @@
+const fs = require('fs');
+
+// Input
+const sums = {};
+const freqs = {};
+const data = fs.readFileSync(`${__dirname}/input.txt`, { encoding: 'utf8' });
+const words = data.replace(/--|\n/g, ' ').split(/ /);
+
+// Get word frenquencies from word list
+words.forEach(token => {
+ if(token) {
+ const word = token.replace(/\.|"|'s|,|\(|\)/g, '');
+ sums[word] = sums[word] ? sums[word] + 1 : 1;
+ }
+});
+
+// Join words by frequency
+Object.keys(sums).sort().forEach(word => {
+ if(!freqs[sums[word]]) freqs[sums[word]] = [];
+
+ freqs[sums[word]].push(word);
+});
+
+// Output
+Object.keys(freqs).sort().forEach(freq => console.log(freq + ': ' + freqs[freq].join(', '))); \ No newline at end of file
diff --git a/challenge-081/nunovieira220/js/input.txt b/challenge-081/nunovieira220/js/input.txt
new file mode 100644
index 0000000000..d2bb45d308
--- /dev/null
+++ b/challenge-081/nunovieira220/js/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
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