aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-08-07 13:25:50 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-08-07 13:25:50 +0200
commite321394215212f81f3c7cedf82ea86ce9c492dff (patch)
treeae13a2db8d83f47b6699a048a419fe5f28638f03
parentac173eb5298f8c1dfed037418c64e654f2442bbf (diff)
downloadperlweeklychallenge-club-e321394215212f81f3c7cedf82ea86ce9c492dff.tar.gz
perlweeklychallenge-club-e321394215212f81f3c7cedf82ea86ce9c492dff.tar.bz2
perlweeklychallenge-club-e321394215212f81f3c7cedf82ea86ce9c492dff.zip
feat(challenge-192/lubos-kolouch/perl,python/): Challenge 192 LK Perl Python
-rw-r--r--challenge-192/lubos-kolouch/perl/ch-1.pl18
-rw-r--r--challenge-192/lubos-kolouch/perl/ch-2.pl48
-rw-r--r--challenge-192/lubos-kolouch/python/ch-1.py22
-rw-r--r--challenge-192/lubos-kolouch/python/ch-2.py45
4 files changed, 133 insertions, 0 deletions
diff --git a/challenge-192/lubos-kolouch/perl/ch-1.pl b/challenge-192/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..d22fd5689f
--- /dev/null
+++ b/challenge-192/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+sub binary_flip {
+ my ($n) = @_;
+ my $binary = sprintf "%b", $n; # Convert to binary
+ $binary =~ tr/01/10/; # Flip the bits
+ return oct("0b$binary"); # Convert back to decimal
+}
+
+is( binary_flip(5), 2, 'Example 1' );
+is( binary_flip(4), 3, 'Example 2' );
+is( binary_flip(6), 1, 'Example 3' );
+
+done_testing();
diff --git a/challenge-192/lubos-kolouch/perl/ch-2.pl b/challenge-192/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..98bec2523b
--- /dev/null
+++ b/challenge-192/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+sub equal_distribution {
+ my @list = @_;
+ my $sum = 0;
+ $sum += $_ for @list;
+
+ return -1 if $sum % @list != 0; # Return -1 if not divisible evenly
+
+ my $target = $sum / @list;
+ my $moves = 0;
+
+ # Repeat until the distribution is complete
+ while (1) {
+ my $done = 1;
+
+ # Distribute the difference to adjacent cells
+ for my $i ( 0 .. $#list - 1 ) {
+ if ( $list[$i] < $target ) {
+ $list[$i]++;
+ $list[ $i + 1 ]--;
+ $moves++;
+ $done = 0;
+ }
+ elsif ( $list[$i] > $target ) {
+ $list[$i]--;
+ $list[ $i + 1 ]++;
+ $moves++;
+ $done = 0;
+ }
+ }
+
+ # Break if the distribution is complete
+ last if $done;
+ }
+
+ return $moves;
+}
+
+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' );
+
+done_testing();
diff --git a/challenge-192/lubos-kolouch/python/ch-1.py b/challenge-192/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..e35447b77a
--- /dev/null
+++ b/challenge-192/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest
+
+
+def binary_flip(n):
+ binary = bin(n)[2:] # Convert to binary
+ flipped_binary = binary.translate(
+ str.maketrans('01', '10')) # Flip the bits
+ return int(flipped_binary, 2) # Convert back to decimal
+
+
+class TestBinaryFlip(unittest.TestCase):
+ def test_examples(self):
+ self.assertEqual(binary_flip(5), 2)
+ self.assertEqual(binary_flip(4), 3)
+ self.assertEqual(binary_flip(6), 1)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-192/lubos-kolouch/python/ch-2.py b/challenge-192/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..ce61d15e0d
--- /dev/null
+++ b/challenge-192/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,45 @@
+import unittest
+
+
+def equal_distribution(list_):
+ total_sum = sum(list_)
+
+ if total_sum % len(list_) != 0:
+ return -1
+
+ target = total_sum // len(list_)
+ moves = 0
+
+ # Repeat until the distribution is complete
+ while True:
+ done = True
+
+ # Distribute the difference to adjacent cells
+ for i in range(len(list_) - 1):
+ if list_[i] < target:
+ list_[i] += 1
+ list_[i+1] -= 1
+ moves += 1
+ done = False
+ elif list_[i] > target:
+ list_[i] -= 1
+ list_[i+1] += 1
+ moves += 1
+ done = False
+
+ # Break if the distribution is complete
+ if done:
+ break
+
+ return moves
+
+
+class TestEqualDistribution(unittest.TestCase):
+ def test_examples(self):
+ self.assertEqual(equal_distribution([1, 0, 5]), 4)
+ self.assertEqual(equal_distribution([0, 2, 0]), -1)
+ self.assertEqual(equal_distribution([0, 3, 0]), 2)
+
+
+if __name__ == '__main__':
+ unittest.main()