aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-243')
-rw-r--r--challenge-243/steven-wilson/python/ch-01.py25
-rw-r--r--challenge-243/steven-wilson/python/ch-02.py26
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-243/steven-wilson/python/ch-01.py b/challenge-243/steven-wilson/python/ch-01.py
new file mode 100644
index 0000000000..59f53277bf
--- /dev/null
+++ b/challenge-243/steven-wilson/python/ch-01.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+from itertools import permutations
+
+
+def reverse_pairs(*elements):
+ '''
+ return the number of reverse pairs in the given array
+ >>> reverse_pairs(1, 3, 2, 3, 1)
+ 2
+ >>> reverse_pairs(2, 4, 3, 5, 1)
+ 3
+ '''
+ count = 0
+ perms = permutations(range(len(elements)), r=2)
+ for i, j in perms:
+ if elements[i] > 2 * elements[j] and 0 <= i and i < j and j < len(elements):
+ count += 1
+ return count
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()
diff --git a/challenge-243/steven-wilson/python/ch-02.py b/challenge-243/steven-wilson/python/ch-02.py
new file mode 100644
index 0000000000..4dc1b96750
--- /dev/null
+++ b/challenge-243/steven-wilson/python/ch-02.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+from itertools import product
+from math import floor
+
+
+def floor_sum(*elements):
+ '''
+ return the sum of floor(nums[i] / nums[j]) where 0 <= i,j < nums.length
+ >>> floor_sum(2, 5, 9)
+ 10
+ >>> floor_sum(7, 7, 7, 7, 7, 7, 7)
+ 49
+ '''
+ sum = 0
+ prod = product(range(len(elements)), repeat=2)
+ for i, j in prod:
+ if 0 <= i and j < len(elements):
+ sum += floor(elements[i] / elements[j])
+ return sum
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()