aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2020-04-10 13:34:02 +0200
committerLubos Kolouch <lubos@kolouch.net>2020-04-10 13:34:02 +0200
commit6d19ddb19661dec5945fa5ab508621dfbbc317be (patch)
tree1674b09bf0194886787bf744d0a854d39050876e
parentc26e1dd569f7ca19898d15f07d71e3852c5e62c8 (diff)
downloadperlweeklychallenge-club-6d19ddb19661dec5945fa5ab508621dfbbc317be.tar.gz
perlweeklychallenge-club-6d19ddb19661dec5945fa5ab508621dfbbc317be.tar.bz2
perlweeklychallenge-club-6d19ddb19661dec5945fa5ab508621dfbbc317be.zip
Challenge 1 Python
-rw-r--r--challenge-055/lubos-kolouch/python/ch-1.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/challenge-055/lubos-kolouch/python/ch-1.py b/challenge-055/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..aefcb44676
--- /dev/null
+++ b/challenge-055/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,77 @@
+##!/usr/bin/env python
+""" https://perlweeklychallenge.org/blog/perl-weekly-challenge-055/
+ Challenge 1 - Flip binary """
+
+from collections import defaultdict
+import sys
+
+
+class BinFlip:
+ """ Does the flip according to the excercise """
+
+ def __init__(self, to_flip: str):
+ self.to_flip = to_flip
+ self.current_flip = ""
+ self.all_flips = defaultdict(list)
+ self.max_ones = 0
+ self.l_count = None
+ self.r_count = None
+
+ def process_all_flips(self):
+ """ process all possible combinations """
+
+ for l_count in range(len(self.to_flip)):
+ for r_count in range(l_count, len(self.to_flip)):
+ self.l_count = l_count
+ self.r_count = r_count
+ self.current_flip = self.do_flip()
+ self.process_current()
+
+ return self.max_ones, self.all_flips[self.max_ones]
+
+ def do_flip(self):
+ """ do the flip and return the result """
+
+ result = list(self.to_flip)
+ for str_pos in range(self.l_count, self.r_count + 1):
+ result[str_pos] = "0" if self.to_flip[str_pos] == "1" else "1"
+
+ return ''.join(result)
+
+ def process_current(self):
+ """ Process the current flip result """
+
+ ones_count = self.current_flip.count("1")
+ self.all_flips[ones_count].append((self.l_count, self.r_count))
+ if ones_count > self.max_ones:
+ self.max_ones = ones_count
+
+
+class BinFlipTest:
+ """ run the tests """
+
+ def __init__(self):
+ self.test_flipper = None
+
+ def test_all(self):
+ """ Do the tests """
+
+ self.test_flipper = BinFlip("010")
+ max_ones, all_flips = self.test_flipper.process_all_flips()
+ assert max_ones == 2
+ assert all_flips == [(0, 0), (0, 2), (2, 2)]
+
+
+def main():
+ """ main """
+ num_flipper = BinFlip(sys.argv[1])
+
+ max_ones, all_flips = num_flipper.process_all_flips()
+ print(f'Max: {max_ones}, l,r: {all_flips}')
+
+
+if __name__ == "__main__":
+ assert len(sys.argv) == 2
+ main()
+ flip_tester = BinFlipTest()
+ flip_tester.test_all()