aboutsummaryrefslogtreecommitdiff
path: root/challenge-244
diff options
context:
space:
mode:
authorSteven <steven1170@zoho.eu>2023-11-20 18:20:08 +0000
committerSteven <steven1170@zoho.eu>2023-11-20 18:20:08 +0000
commitcf6b31395f9b0ea5126e6e902e28b51ea419ec13 (patch)
tree311bef43ff60743ac2dc819a417c884e97fce0a1 /challenge-244
parentd3c171ecb6715a7bd21a52fab583c4171605f5c0 (diff)
downloadperlweeklychallenge-club-cf6b31395f9b0ea5126e6e902e28b51ea419ec13.tar.gz
perlweeklychallenge-club-cf6b31395f9b0ea5126e6e902e28b51ea419ec13.tar.bz2
perlweeklychallenge-club-cf6b31395f9b0ea5126e6e902e28b51ea419ec13.zip
add solutions week 244 in python
Diffstat (limited to 'challenge-244')
-rw-r--r--challenge-244/steven-wilson/python/ch-01.py28
-rw-r--r--challenge-244/steven-wilson/python/ch-02.py25
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-244/steven-wilson/python/ch-01.py b/challenge-244/steven-wilson/python/ch-01.py
new file mode 100644
index 0000000000..8dd62dff4c
--- /dev/null
+++ b/challenge-244/steven-wilson/python/ch-01.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+
+def count_smaller(*elements):
+ '''
+ Calculate the number of integers smaller than the integer at each index
+ >>> count_smaller(8, 1, 2, 2, 3)
+ (4, 0, 1, 1, 3)
+ >>> count_smaller(6, 5, 4, 8)
+ (2, 1, 0, 3)
+ >>> count_smaller(2, 2, 2)
+ (0, 0, 0)
+ '''
+ sorted_elements = sorted(elements)
+ smaller = []
+
+ for elem in elements:
+ for i, sorted_elem in enumerate(sorted_elements):
+ if elem <= sorted_elem:
+ smaller.append(i)
+ break
+ return tuple(smaller)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()
diff --git a/challenge-244/steven-wilson/python/ch-02.py b/challenge-244/steven-wilson/python/ch-02.py
new file mode 100644
index 0000000000..07396a61fa
--- /dev/null
+++ b/challenge-244/steven-wilson/python/ch-02.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+
+from itertools import combinations
+
+
+def group_hero(*elements):
+ '''
+ Return the sum of the powers of all possible combinations; power is
+ defined as the square of the largest number in a sequence,
+ multiplied by the smallest.
+ >>> group_hero(2, 1, 4)
+ 141
+ '''
+ sum_of_powers = 0
+ for i, elem in enumerate(elements, 1):
+ for comb in combinations(elements, i):
+ sum_of_powers += ((max(comb) ** 2) * min(comb))
+ return sum_of_powers
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()