aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-09-23 01:45:36 +0100
committerGitHub <noreply@github.com>2020-09-23 01:45:36 +0100
commitc80545ab57819abb652b57683b68de5962a3de2c (patch)
tree54615003c337309e5292cdc6d5b33f727ff12ef7
parentc579d650f514f00599f4926994887addcd01a780 (diff)
parent876afaf800b4d1290ace6500ddad34ca726de9d7 (diff)
downloadperlweeklychallenge-club-c80545ab57819abb652b57683b68de5962a3de2c.tar.gz
perlweeklychallenge-club-c80545ab57819abb652b57683b68de5962a3de2c.tar.bz2
perlweeklychallenge-club-c80545ab57819abb652b57683b68de5962a3de2c.zip
Merge pull request #2353 from nunovieira220/challenge-079
Add solution to challenge 079
-rw-r--r--challenge-079/nunovieira220/js/ch-1.js12
-rw-r--r--challenge-079/nunovieira220/js/ch-2.js17
-rw-r--r--challenge-079/nunovieira220/perl/ch-1.pl19
-rw-r--r--challenge-079/nunovieira220/perl/ch-2.pl22
4 files changed, 70 insertions, 0 deletions
diff --git a/challenge-079/nunovieira220/js/ch-1.js b/challenge-079/nunovieira220/js/ch-1.js
new file mode 100644
index 0000000000..9216296d84
--- /dev/null
+++ b/challenge-079/nunovieira220/js/ch-1.js
@@ -0,0 +1,12 @@
+// Input
+const N = 4;
+
+// Count set bits
+let counter = 0;
+
+Array.from({ length: N }, (_, i) => i + 1).forEach(n => {
+ const bin = parseInt(n, 10).toString(2);
+ counter += bin.split('1').length - 1;
+});
+
+console.log(counter); \ No newline at end of file
diff --git a/challenge-079/nunovieira220/js/ch-2.js b/challenge-079/nunovieira220/js/ch-2.js
new file mode 100644
index 0000000000..6fe1744ee6
--- /dev/null
+++ b/challenge-079/nunovieira220/js/ch-2.js
@@ -0,0 +1,17 @@
+// Input
+const A = [3, 1, 3, 1, 1, 5];
+
+// Trapped rain water counter
+let counter = 0;
+let max = 0;
+
+A.forEach(item => {
+ if(item >= max) {
+ max = item;
+ } else {
+ counter += max - item;
+ }
+});
+
+// Output
+console.log(counter); \ No newline at end of file
diff --git a/challenge-079/nunovieira220/perl/ch-1.pl b/challenge-079/nunovieira220/perl/ch-1.pl
new file mode 100644
index 0000000000..239e31f27c
--- /dev/null
+++ b/challenge-079/nunovieira220/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# Input
+my $N = $ARGV[0] || 7;
+
+# Count set bits
+my $counter = 0;
+
+for ((1..$N)) {
+ my $bin = sprintf ("%b", $_);
+ $counter += () = $bin =~ /1/g;
+}
+
+# Output
+my $res = $counter % 1000000007;
+print $res."\n"; \ No newline at end of file
diff --git a/challenge-079/nunovieira220/perl/ch-2.pl b/challenge-079/nunovieira220/perl/ch-2.pl
new file mode 100644
index 0000000000..2b6fff5c3b
--- /dev/null
+++ b/challenge-079/nunovieira220/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# Input
+my @list = scalar @ARGV ? @ARGV : (3, 1, 3, 1, 1, 5);
+
+# Trapped rain water counter
+my $counter = 0;
+my $max = 0;
+
+for my $item (@list) {
+ if($item >= $max) {
+ $max = $item;
+ } else {
+ $counter += $max - $item;
+ }
+}
+
+# Output
+print $counter."\n"; \ No newline at end of file