diff options
| author | Alexander Pankoff <ccntrq@screenri.de> | 2022-05-03 12:30:25 +0200 |
|---|---|---|
| committer | Alexander Pankoff <ccntrq@screenri.de> | 2022-05-03 12:30:25 +0200 |
| commit | 6aa6f29bde36433f239d0e9110af83bda78f1299 (patch) | |
| tree | 3b8f658f1b81eea3488af3ebcb9fd61b56e282c1 | |
| parent | b13444bb5307ad7c7051ace6031dd063bac3cc32 (diff) | |
| download | perlweeklychallenge-club-6aa6f29bde36433f239d0e9110af83bda78f1299.tar.gz perlweeklychallenge-club-6aa6f29bde36433f239d0e9110af83bda78f1299.tar.bz2 perlweeklychallenge-club-6aa6f29bde36433f239d0e9110af83bda78f1299.zip | |
Add solutions for challenge 163
| -rw-r--r-- | challenge-163/alexander-pankoff/haskell/ch-1.hs | 16 | ||||
| -rw-r--r-- | challenge-163/alexander-pankoff/haskell/ch-2.hs | 9 | ||||
| -rwxr-xr-x | challenge-163/alexander-pankoff/perl/ch-1.pl | 73 | ||||
| -rwxr-xr-x | challenge-163/alexander-pankoff/perl/ch-2.pl | 87 |
4 files changed, 185 insertions, 0 deletions
diff --git a/challenge-163/alexander-pankoff/haskell/ch-1.hs b/challenge-163/alexander-pankoff/haskell/ch-1.hs new file mode 100644 index 0000000000..395da2d4b0 --- /dev/null +++ b/challenge-163/alexander-pankoff/haskell/ch-1.hs @@ -0,0 +1,16 @@ +module Main where + +import Data.Bits ((.&.)) +import Data.List (subsequences) + +main :: IO () +main = print $ sumBitwiseOperator [1 .. 3] + +sumBitwiseOperator :: [Int] -> Int +sumBitwiseOperator = sum . map (uncurry (.&.)) . uniquePairs + +uniquePairs :: [a] -> [(a, a)] +uniquePairs = map (\[a, b] -> (a, b)) . combinations 2 + +combinations :: Int -> [a] -> [[a]] +combinations k = filter ((== k) . length) . subsequences diff --git a/challenge-163/alexander-pankoff/haskell/ch-2.hs b/challenge-163/alexander-pankoff/haskell/ch-2.hs new file mode 100644 index 0000000000..7368d8c52b --- /dev/null +++ b/challenge-163/alexander-pankoff/haskell/ch-2.hs @@ -0,0 +1,9 @@ +module Main where + +main :: IO () +main = print $ summations [1 .. 5] + +summations :: [Int] -> Int +summations [] = 0 +summations [x] = x +summations (_ : xs) = summations $ scanl1 (+) xs
\ No newline at end of file diff --git a/challenge-163/alexander-pankoff/perl/ch-1.pl b/challenge-163/alexander-pankoff/perl/ch-1.pl new file mode 100755 index 0000000000..ec0658f823 --- /dev/null +++ b/challenge-163/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl + +=pod +Task 1: Sum Bitwise Operator +Submitted by: Mohammad S Anwar + +You are given list positive numbers, @n. + +Write script to calculate the sum of bitwise & operator for all unique pairs. + +Example 1 + + Input: @n = (1, 2, 3) + Output: 3 + + Since (1 & 2) + (2 & 3) + (1 & 3) => 0 + 2 + 1 => 3. + +Example 2 + + Input: @n = (2, 3, 4) + Output: 2 + + Since (2 & 3) + (2 & 4) + (3 & 4) => 2 + 0 + 0 => 2. +=cut + +package challenge163::ch1; + +use strict; +use warnings; +use autodie; +use feature qw'say state signatures'; +no warnings qw'experimental::signatures'; + +use FindBin (); +use File::Spec (); + +use List::Util qw(all sum0); + +use lib File::Spec->catdir( $FindBin::RealBin, + qw'.. .. .. challenge-001 alexander-pankoff perl lib' ); + +run() unless caller(); + +sub run() { + my @n = @ARGV; + die "Expect a list of if of positive intergers" + unless all { $_ =~ m/^\d+$/ && $_ > 0 } @n; + + say sum_bitwise_operator(@n); +} + +sub sum_bitwise_operator(@n) { + sum0( map { $_->[0] & $_->[1] } unique_pairs(@n) ); +} + +sub unique_pairs(@xs) { + return combinations( 2, @xs ); +} + +sub combinations ( $size, @xs ) { + return if !@xs; + return map { [$_] } @xs if $size == 1; + + my @out; + for my $index ( 0 .. $#xs ) { + my @rest = @xs[ $index + 1 .. $#xs ]; + for my $comb ( combinations( $size - 1, @rest ) ) { + push @out, [ $xs[$index], @$comb ]; + } + } + + return @out; +} diff --git a/challenge-163/alexander-pankoff/perl/ch-2.pl b/challenge-163/alexander-pankoff/perl/ch-2.pl new file mode 100755 index 0000000000..366165e5b6 --- /dev/null +++ b/challenge-163/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,87 @@ +#!/usr/bin/env perl + +=pod +Task 2: Summations +Submitted by: Mohammad S Anwar + +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 + +=cut + +package challenge163::ch2; + +use strict; +use warnings; +use autodie; +use feature qw'say state signatures'; +no warnings qw'experimental::signatures'; + +use FindBin (); +use File::Spec (); + +use List::Util qw(all sum0); + +use lib File::Spec->catdir( $FindBin::RealBin, + qw'.. .. .. challenge-001 alexander-pankoff perl lib' ); + +run() unless caller(); + +sub run() { + my @n = @ARGV; + + # TODO: extract me + die "Expect a list of if of positive intergers" + unless all { $_ =~ m/^\d+$/ && $_ > 0 } @n; + + say summations(@n); + +} + +sub summations(@n) { + return 0 if !@n; + return $n[0] if @n == 1; + + my @next = ( scanl( sub ( $acc, $cur ) { $acc + $cur }, @n[ 1 .. $#n ] ) ); + + return summations(@next); +} + +# https://hackage.haskell.org/package/base-4.16.1.0/docs/Prelude.html#v:scanl +sub scanl ( $fn, $start, @xs ) { + my $acc = $start; + my @out = ($start); + for my $x (@xs) { + $acc = $fn->( $acc, $x ); + push @out, $acc; + } + + return @out; +} |
