aboutsummaryrefslogtreecommitdiff
path: root/challenge-142/sgreen/python
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2021-12-12 14:45:17 +1100
committerSimon Green <mail@simon.green>2021-12-12 14:45:17 +1100
commit277a6b908be328067cc11fb8353d5a88f91db2d8 (patch)
tree6e4e5c0cb588b005bccb5ad0b99bb41e7967fa04 /challenge-142/sgreen/python
parenta64c96703f1714a1598fe7fb90450a2763122643 (diff)
downloadperlweeklychallenge-club-277a6b908be328067cc11fb8353d5a88f91db2d8.tar.gz
perlweeklychallenge-club-277a6b908be328067cc11fb8353d5a88f91db2d8.tar.bz2
perlweeklychallenge-club-277a6b908be328067cc11fb8353d5a88f91db2d8.zip
sgreen solutions to challenge 142
Diffstat (limited to 'challenge-142/sgreen/python')
-rwxr-xr-xchallenge-142/sgreen/python/ch-1.py40
-rwxr-xr-xchallenge-142/sgreen/python/ch-2.py29
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-142/sgreen/python/ch-1.py b/challenge-142/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..7f45ee1156
--- /dev/null
+++ b/challenge-142/sgreen/python/ch-1.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def _get_divisors(number):
+ # Every number is divisible by 1
+ divisors = [1]
+
+ # One only has one divisor
+ if number == 1:
+ return divisors
+
+ # Find other divisors
+ for i in range(2, int(number / 2) + 1):
+ if number % i == 0:
+ divisors.append(i)
+
+ # ... including the number itself
+ divisors.append(number)
+
+ return divisors
+
+
+def main(inputs):
+ m = int(inputs[0])
+ n = int(inputs[1])
+
+ if m < 1:
+ raise ValueError('The first number must be a positive integer')
+ if n < 1 or n > 9:
+ raise ValueError('The second number must be between 1 and 9')
+
+ # Get a list of divisors and find those that end with n.
+ solution = [x for x in _get_divisors(m) if x % 10 == n]
+ print(len(solution))
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-142/sgreen/python/ch-2.py b/challenge-142/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..1aebb131da
--- /dev/null
+++ b/challenge-142/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+import sys
+from time import sleep
+from threading import Thread
+
+
+def _sleep_sort(seconds):
+ sleep(float(seconds))
+ print(seconds)
+
+
+def main(inputs):
+ threads = []
+
+ if any(float(x) < 0 for x in inputs):
+ raise ValueError('You can sort negative numbers')
+
+ for seconds in inputs:
+ new_thread = Thread(target=_sleep_sort, args=(seconds,))
+ threads.append(new_thread)
+ new_thread.start()
+
+ for t in threads:
+ t.join()
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])