aboutsummaryrefslogtreecommitdiff
path: root/challenge-263
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-01 17:53:17 +0100
committerGitHub <noreply@github.com>2024-04-01 17:53:17 +0100
commitfe2e0887f7cf0840a2bdf3f28f45e09ff2437e0b (patch)
tree73bc884a4e7e7fc4a06df74cc0367d78c4e6ee5d /challenge-263
parent544a134ff3b3251cfd3284fbf99cbfacaec5f80f (diff)
parent396e9f2d45acf65e2dd8dbde30a77a6aa0ef227a (diff)
downloadperlweeklychallenge-club-fe2e0887f7cf0840a2bdf3f28f45e09ff2437e0b.tar.gz
perlweeklychallenge-club-fe2e0887f7cf0840a2bdf3f28f45e09ff2437e0b.tar.bz2
perlweeklychallenge-club-fe2e0887f7cf0840a2bdf3f28f45e09ff2437e0b.zip
Merge pull request #9850 from oWnOIzRi/week263
add solutions week 263 in python
Diffstat (limited to 'challenge-263')
-rw-r--r--challenge-263/steven-wilson/python/ch-1.py21
-rw-r--r--challenge-263/steven-wilson/python/ch-2.py28
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-263/steven-wilson/python/ch-1.py b/challenge-263/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..c5812d1069
--- /dev/null
+++ b/challenge-263/steven-wilson/python/ch-1.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+
+def target_index(*elements, target):
+ ''' Given an array of integers and a target element, return the list of
+ indices in the sorted array where the element is same as the given target
+ element.
+ >>> target_index(1, 5, 3, 2, 4, 2, target=2)
+ [1, 2]
+ >>> target_index(1, 2, 4, 3, 5, target = 6)
+ []
+ >>> target_index(5, 3, 2, 4, 2, 1, target = 4)
+ [4]
+ '''
+ return [i for i, elem in enumerate(sorted(elements)) if elem == target]
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()
diff --git a/challenge-263/steven-wilson/python/ch-2.py b/challenge-263/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..40895fcd98
--- /dev/null
+++ b/challenge-263/steven-wilson/python/ch-2.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+from collections import Counter
+
+
+def merge_items(items1, items2):
+ ''' Given two 2-D array of positive integers, items1 and items2 where
+ element is pair of (item_id, item_quantity), return the merged items.
+ >>> merge_items([ [1,1], [2,1], [3,2] ], [ [2,2], [1,3] ])
+ [[1, 4], [2, 3], [3, 2]]
+ >>> merge_items([[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]])
+ [[1, 8], [2, 3], [3, 3]]
+ >>> merge_items([[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]])
+ [[1, 1], [2, 9], [3, 3]]
+ '''
+ counter = Counter()
+ all_items = items1 + items2
+
+ for item in all_items:
+ counter[item[0]] += item[1]
+
+ return [[item_id, counter[item_id]] for item_id in sorted(counter.keys())]
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()