aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-13 17:31:11 +0000
committerGitHub <noreply@github.com>2023-11-13 17:31:11 +0000
commit81aef23b12cfbba69bf7fc2cb8493eadb2423722 (patch)
treebe0b8769c748e4aae570608f67cae8417b9938f7
parent212c89e2ae9b6289f3d23288314da6803e33453f (diff)
parent707e1eb33b95344b35271dc223f3939738187acb (diff)
downloadperlweeklychallenge-club-81aef23b12cfbba69bf7fc2cb8493eadb2423722.tar.gz
perlweeklychallenge-club-81aef23b12cfbba69bf7fc2cb8493eadb2423722.tar.bz2
perlweeklychallenge-club-81aef23b12cfbba69bf7fc2cb8493eadb2423722.zip
Merge pull request #9059 from oWnOIzRi/week243
add solutions week 243 in python
-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()