aboutsummaryrefslogtreecommitdiff
path: root/challenge-140/sgreen/python
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2021-11-25 23:42:34 +1100
committerSimon Green <mail@simon.green>2021-11-25 23:42:34 +1100
commit1eaf078bb734a5160b8b57ac8e491a91b4cf710b (patch)
tree3c1f13a932e488ed06fbc8ac42a5a20aee4d0e6e /challenge-140/sgreen/python
parente57e8ba97ca974deeadbd7137390e99a38d8304d (diff)
downloadperlweeklychallenge-club-1eaf078bb734a5160b8b57ac8e491a91b4cf710b.tar.gz
perlweeklychallenge-club-1eaf078bb734a5160b8b57ac8e491a91b4cf710b.tar.bz2
perlweeklychallenge-club-1eaf078bb734a5160b8b57ac8e491a91b4cf710b.zip
sgreen solutions to challenge 140
Diffstat (limited to 'challenge-140/sgreen/python')
-rwxr-xr-xchallenge-140/sgreen/python/ch-1.py46
-rwxr-xr-xchallenge-140/sgreen/python/ch-2.py25
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-140/sgreen/python/ch-1.py b/challenge-140/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..6309e048bd
--- /dev/null
+++ b/challenge-140/sgreen/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+import re
+import sys
+
+
+class BinaryDecimal:
+ def __init__(self, value):
+ # Check we were supplied with a binary string
+ if not re.match(r'^[01]+$', value):
+ raise Exception(f'The value {value} is not binary')
+ self.value = int(value)
+
+ def __add__(self, other):
+ value1 = self.value
+ value2 = other.value
+ result = 0
+ power = 0
+ carry = 0
+
+ while (carry or value1 or value2):
+ digit = value1 % 10 + value2 % 10 + carry
+ if digit >= 2:
+ carry = 1
+ digit -= 2
+ else:
+ carry = 0
+
+ if digit == 1:
+ result += 10 ** power
+
+ value1 = int(value1 / 10)
+ value2 = int(value2 / 10)
+ power += 1
+
+ return result
+
+
+def main(inputs):
+ value1 = BinaryDecimal(inputs[0])
+ value2 = BinaryDecimal(inputs[1])
+ print(value1 + value2)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-140/sgreen/python/ch-2.py b/challenge-140/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..d27ce9f168
--- /dev/null
+++ b/challenge-140/sgreen/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(inputs):
+ i = int(inputs[0])
+ j = int(inputs[1])
+ k = int(inputs[2])
+ numbers = []
+
+ # Ensure we don't do something stupid
+ if (k > i * j):
+ raise Exception('Not enough elements')
+
+ for m in range(1, i+1):
+ for n in range(1, j+1):
+ numbers.append(m*n)
+
+ numbers.sort()
+ print(numbers[k - 1])
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])