aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-192/alexander-pankoff/haskell/ch-1.hs19
-rwxr-xr-xchallenge-192/alexander-pankoff/haskell/ch-2.hs29
-rw-r--r--challenge-192/alexander-pankoff/perl/ch-1.pl21
-rw-r--r--challenge-192/alexander-pankoff/perl/ch-2.pl28
-rw-r--r--challenge-192/alexander-pankoff/raku/ch-1.raku12
-rw-r--r--challenge-192/alexander-pankoff/raku/ch-2.raku24
6 files changed, 133 insertions, 0 deletions
diff --git a/challenge-192/alexander-pankoff/haskell/ch-1.hs b/challenge-192/alexander-pankoff/haskell/ch-1.hs
new file mode 100755
index 0000000000..b82af058fe
--- /dev/null
+++ b/challenge-192/alexander-pankoff/haskell/ch-1.hs
@@ -0,0 +1,19 @@
+#!/usr/bin/env stack
+{- stack --resolver lts-20.1 runghc --package HUnit -}
+module Main where
+
+import Test.HUnit
+import Data.Bits
+import Data.Word (Word64)
+
+main = runTestTT
+ $ TestList
+ [ TestCase $ assertEqual "Example 1" 2 (binaryFlip 5)
+ , TestCase $ assertEqual "Example 2" 3 (binaryFlip 4)
+ , TestCase $ assertEqual "Example 3" 1 (binaryFlip 6)
+ , TestCase $ assertEqual "Example [4 ~= 0b111" 0 (binaryFlip 7)]
+
+binaryFlip :: Word64 -> Word64
+binaryFlip n = n `xor` shiftR oneBits (countLeadingZeros n)
+
+
diff --git a/challenge-192/alexander-pankoff/haskell/ch-2.hs b/challenge-192/alexander-pankoff/haskell/ch-2.hs
new file mode 100755
index 0000000000..c436550291
--- /dev/null
+++ b/challenge-192/alexander-pankoff/haskell/ch-2.hs
@@ -0,0 +1,29 @@
+#!/usr/bin/env stack
+{- stack --resolver lts-20.1 runghc --package HUnit -}
+module Main where
+
+import Test.HUnit
+import Data.List (singleton)
+
+main = runTestTT
+ $ TestList
+ [ TestCase $ assertEqual "Example 1" (Just 4) (equalDistribution [1, 0, 5])
+ , TestCase $ assertEqual "Example 2" Nothing (equalDistribution [0, 2, 0])
+ , TestCase $ assertEqual "Example 3" (Just 2) (equalDistribution [0, 3, 0])
+ , TestCase
+ $ assertEqual "Example (empty list)" (Just 0) (equalDistribution mempty)
+ , TestCase
+ $ assertEqual
+ "Example (singleton list)"
+ (Just 0)
+ (equalDistribution $ singleton 4)]
+
+equalDistribution :: [Int] -> Maybe Int
+equalDistribution [] = Just 0
+equalDistribution xs =
+ let total = sum xs
+ target = total `div` length xs
+ isDistributable = total `mod` length xs == 0
+ in if isDistributable
+ then Just $ sum $ map abs $ scanl (\a b -> a + b - target) 0 xs
+ else Nothing \ No newline at end of file
diff --git a/challenge-192/alexander-pankoff/perl/ch-1.pl b/challenge-192/alexander-pankoff/perl/ch-1.pl
new file mode 100644
index 0000000000..b6dfa0613f
--- /dev/null
+++ b/challenge-192/alexander-pankoff/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+package challenge192::ch1;
+
+use strict;
+use warnings;
+use autodie;
+use feature qw'say state signatures';
+no warnings qw'experimental::signatures';
+
+use POSIX qw(ceil);
+
+use Test::More tests => 4;
+
+is( binary_flip(5), 2, "Example 1" );
+is( binary_flip(4), 3, "Example 2" );
+is( binary_flip(6), 1, "Example 3" );
+is( binary_flip(7), 0, "Example [7 ~= 0b111]" );
+
+sub binary_flip ($n) {
+ $n ^ ( -1 + 2**ceil( log( $n + 1 ) / log(2) ) );
+}
diff --git a/challenge-192/alexander-pankoff/perl/ch-2.pl b/challenge-192/alexander-pankoff/perl/ch-2.pl
new file mode 100644
index 0000000000..d0c752213b
--- /dev/null
+++ b/challenge-192/alexander-pankoff/perl/ch-2.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+package challenge192::ch2;
+
+use strict;
+use warnings;
+use autodie;
+use feature qw'say state signatures';
+no warnings qw'experimental::signatures';
+
+use List::Util qw(reductions sum0);
+
+use Test::More tests => 5;
+
+is( equal_distribution( 1, 0, 5 ), 4, "Example 1" );
+is( equal_distribution( 0, 2, 0 ), -1, "Example 2" );
+is( equal_distribution( 0, 3, 0 ), 2, "Example 3" );
+is( equal_distribution(), 0, "Example (empty list)" );
+is( equal_distribution(1), 0, "Example (singleton list)" );
+
+sub equal_distribution (@list) {
+ return 0 if @list <= 1;
+ my $total = sum0(@list);
+ return -1 if $total % @list != 0;
+ my $target = $total / @list;
+
+ sum0 map { abs($_) } reductions { $a + $b - $target } 0, @list;
+}
+
diff --git a/challenge-192/alexander-pankoff/raku/ch-1.raku b/challenge-192/alexander-pankoff/raku/ch-1.raku
new file mode 100644
index 0000000000..4b32433a46
--- /dev/null
+++ b/challenge-192/alexander-pankoff/raku/ch-1.raku
@@ -0,0 +1,12 @@
+#!/usr/bin/env raku
+
+use Test;
+
+is( binary-flip(5), 2, "Example 1" );
+is( binary-flip(4), 3, "Example 2" );
+is( binary-flip(6), 1, "Example 3" );
+is( binary-flip(7), 0, "Example [7 ~= 0b111]" );
+
+sub binary-flip(Int $n --> Int) {
+ return $n +^ ( -1 + 2** ( $n + 1 ).log2.ceiling );
+}
diff --git a/challenge-192/alexander-pankoff/raku/ch-2.raku b/challenge-192/alexander-pankoff/raku/ch-2.raku
new file mode 100644
index 0000000000..e777d113da
--- /dev/null
+++ b/challenge-192/alexander-pankoff/raku/ch-2.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use Test;
+
+is( equal-distribution(Array[Int].new: <1 0 5>), 4, "Example 1" );
+is( equal-distribution(Array[Int].new: <0 2 0>), -1, "Example 2" );
+is( equal-distribution(Array[Int].new: <0 3 0>), 2, "Example 3" );
+is( equal-distribution(Array[Int].new), 0, "Example (empty list)" );
+is( equal-distribution(Array[Int].new: <1>), 0, "Example (singleton list)" );
+
+sub equal-distribution(Int @list --> Int) {
+ if (@list.elems <= 1 ) {
+ return 0;
+ }
+
+ my $total = @list.sum;
+
+ if ($total % @list.elems != 0) {
+ return -1;
+ }
+
+ my $target = $total div @list.elems;
+ return (0, |@list).produce({$^a + $^b - $target}).map(&abs).sum;
+} \ No newline at end of file