diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-25 13:27:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-25 13:27:52 +0000 |
| commit | a119308199bd8642cec114a112af9d9c3f400270 (patch) | |
| tree | a7c213d693fb9fdf600a331fb635ad8170cabaaa /challenge-140/sgreen/python | |
| parent | ac6fe877c68a4d057f8904aa8611affee296c403 (diff) | |
| parent | 1eaf078bb734a5160b8b57ac8e491a91b4cf710b (diff) | |
| download | perlweeklychallenge-club-a119308199bd8642cec114a112af9d9c3f400270.tar.gz perlweeklychallenge-club-a119308199bd8642cec114a112af9d9c3f400270.tar.bz2 perlweeklychallenge-club-a119308199bd8642cec114a112af9d9c3f400270.zip | |
Merge pull request #5278 from simongreen-net/swg-140
sgreen solutions to challenge 140
Diffstat (limited to 'challenge-140/sgreen/python')
| -rwxr-xr-x | challenge-140/sgreen/python/ch-1.py | 46 | ||||
| -rwxr-xr-x | challenge-140/sgreen/python/ch-2.py | 25 |
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:]) |
