From ad08b2b87cc19ab0320df5ed8f72cb7471edb078 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Thu, 8 Oct 2020 00:21:23 +0100 Subject: Add nunovieira220 perl solution to challenge 081 --- challenge-081/nunovieira220/perl/ch-1.pl | 22 ++++++++++++++++++ challenge-081/nunovieira220/perl/ch-2.pl | 36 ++++++++++++++++++++++++++++++ challenge-081/nunovieira220/perl/input.txt | 3 +++ 3 files changed, 61 insertions(+) create mode 100644 challenge-081/nunovieira220/perl/ch-1.pl create mode 100644 challenge-081/nunovieira220/perl/ch-2.pl create mode 100644 challenge-081/nunovieira220/perl/input.txt (limited to 'challenge-081') 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 -- cgit From 1c2a38e61c7dcf97185d5835b9f08e9dbf17179b Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Thu, 8 Oct 2020 00:21:39 +0100 Subject: Add nunovieira220 js solution to challenge 081 --- challenge-081/nunovieira220/js/ch-1.js | 17 +++++++++++++++++ challenge-081/nunovieira220/js/ch-2.js | 25 +++++++++++++++++++++++++ challenge-081/nunovieira220/js/input.txt | 3 +++ 3 files changed, 45 insertions(+) create mode 100644 challenge-081/nunovieira220/js/ch-1.js create mode 100644 challenge-081/nunovieira220/js/ch-2.js create mode 100644 challenge-081/nunovieira220/js/input.txt (limited to 'challenge-081') 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 -- cgit