aboutsummaryrefslogtreecommitdiff
path: root/challenge-163
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-05-06 23:12:59 +0100
committerGitHub <noreply@github.com>2022-05-06 23:12:59 +0100
commitb665c417c5dec690dbb153e1dfd5562989ca6fc9 (patch)
tree60d64966f15cda57ecf2bc2a4ed126502f1884ab /challenge-163
parentb1cf061a22bc67c3cce66c2fae8427212cf76cc3 (diff)
parent8421885cd720ef2d7f9c9b545b449daa4c9d7e98 (diff)
downloadperlweeklychallenge-club-b665c417c5dec690dbb153e1dfd5562989ca6fc9.tar.gz
perlweeklychallenge-club-b665c417c5dec690dbb153e1dfd5562989ca6fc9.tar.bz2
perlweeklychallenge-club-b665c417c5dec690dbb153e1dfd5562989ca6fc9.zip
Merge pull request #6051 from simbabque/challenge-163
Challenge 163
Diffstat (limited to 'challenge-163')
-rw-r--r--challenge-163/julien-fiegehenn/perl/ch-1.pl35
-rw-r--r--challenge-163/julien-fiegehenn/perl/ch-2.pl63
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-163/julien-fiegehenn/perl/ch-1.pl b/challenge-163/julien-fiegehenn/perl/ch-1.pl
new file mode 100644
index 0000000000..069f671c26
--- /dev/null
+++ b/challenge-163/julien-fiegehenn/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util 'sum0';
+
+# Task 1: Sum Bitwise Operator
+
+# You are given list positive numbers, @n.
+# Write script to calculate the sum of bitwise & operator for all unique pairs.
+
+# Input: @n = (1, 2, 3)
+# Output: 3
+#
+# Since (1 & 2) + (2 & 3) + (1 & 3) => 0 + 2 + 1 => 3.
+
+sub sum_bitwise {
+ my @num = @_;
+
+ my %seen;
+ return sum0 map {
+ my $first = $_;
+ map {
+ $first == $_ ? 0
+ : $seen{ sprintf '%d_%d', sort $first, $_ }++ ? 0
+ : ( $first & $_ )
+ } @num;
+ } @num;
+}
+
+use Test::More;
+
+is sum_bitwise( 1, 2, 3 ), 3;
+is sum_bitwise( 2, 3, 4 ), 2;
+
+done_testing;
diff --git a/challenge-163/julien-fiegehenn/perl/ch-2.pl b/challenge-163/julien-fiegehenn/perl/ch-2.pl
new file mode 100644
index 0000000000..d1b93340b3
--- /dev/null
+++ b/challenge-163/julien-fiegehenn/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util 'sum';
+
+# You are given a list of positive numbers, @n.
+
+# Write a script to find out the summations as described below.
+
+# Example 1
+# Input: @n = (1, 2, 3, 4, 5)
+# Output: 42
+
+# 1 2 3 4 5
+# 2 5 9 14
+# 5 14 28
+# 14 42
+# 42
+
+# The nth Row starts with the second element of the (n-1)th row.
+# The following element is sum of all elements except first element of previous row.
+# You stop once you have just one element in the row.
+# Example 2
+# Input: @n = (1, 3, 5, 7, 9)
+# Output: 70
+
+# 1 3 5 7 9
+# 3 8 15 24
+# 8 23 47
+# 23 70
+# 70
+
+sub foo {
+ my @rows;
+ $rows[0] = [@_];
+
+ # we'll keep going as long as we have more than 2 elements left
+ while ( @{ $rows[-1] } > 2 ) {
+ push @rows, [
+
+ # we always start with the 2nd element of the previous row
+ $rows[-1]->[1],
+
+ # each following element is the sum of all numbers from the
+ # 2nd element until the position (not index, but position in
+ # the tabular view above) we are currently filling, of
+ # the previous row.
+ map { sum @{ $rows[-1] }[ 1 .. $_ ] } 2 .. $#{ $rows[-1] }
+ ];
+ }
+
+ # We're supposed to stop and return when we only have one element left,
+ # but it is actually easier to stop at two elements left. The last row
+ # is just the 2nd element of the row before, so we can return that directly.
+ return $rows[-1]->[-1];
+}
+
+use Test::More;
+
+is foo( 1, 2, 3, 4, 5 ), 42;
+is foo( 1, 3, 5, 7, 9 ), 70;
+
+done_testing;