aboutsummaryrefslogtreecommitdiff
path: root/challenge-081
diff options
context:
space:
mode:
authorNuno Vieira <nunovieira220@gmail.com>2020-10-08 00:21:39 +0100
committerNuno Vieira <nunovieira220@gmail.com>2020-10-08 00:21:39 +0100
commit1c2a38e61c7dcf97185d5835b9f08e9dbf17179b (patch)
treeae51339ad5eeddf37c771260318b1b09098a030f /challenge-081
parentad08b2b87cc19ab0320df5ed8f72cb7471edb078 (diff)
downloadperlweeklychallenge-club-1c2a38e61c7dcf97185d5835b9f08e9dbf17179b.tar.gz
perlweeklychallenge-club-1c2a38e61c7dcf97185d5835b9f08e9dbf17179b.tar.bz2
perlweeklychallenge-club-1c2a38e61c7dcf97185d5835b9f08e9dbf17179b.zip
Add nunovieira220 js 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
3 files changed, 45 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