aboutsummaryrefslogtreecommitdiff
path: root/challenge-245/pokgopun/python
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-12-04 15:09:23 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-12-04 15:09:23 +0800
commit1b0f0c9cf0a1295b8bf9bef8d902f9a351120785 (patch)
tree538dfb19ef0c66b56d4d743ed37868350b463a0f /challenge-245/pokgopun/python
parent6f78aae3efa4642ffa271896203a09afce53e407 (diff)
parentb41848354dc1a09ae68e01e91b551a36d13f8bda (diff)
downloadperlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.gz
perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.tar.bz2
perlweeklychallenge-club-1b0f0c9cf0a1295b8bf9bef8d902f9a351120785.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-245/pokgopun/python')
-rw-r--r--challenge-245/pokgopun/python/ch-1.py42
-rw-r--r--challenge-245/pokgopun/python/ch-2.py77
2 files changed, 119 insertions, 0 deletions
diff --git a/challenge-245/pokgopun/python/ch-1.py b/challenge-245/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..566c3310b1
--- /dev/null
+++ b/challenge-245/pokgopun/python/ch-1.py
@@ -0,0 +1,42 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-245/
+"""
+
+Task 1: Sort Language
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given two array of languages and its popularity.
+
+ Write a script to sort the language based on popularity.
+
+Example 1
+
+Input: @lang = ('perl', 'c', 'python')
+ @popularity = (2, 1, 3)
+Output: ('c', 'perl', 'python')
+
+Example 2
+
+Input: @lang = ('c++', 'haskell', 'java')
+ @popularity = (1, 3, 2)
+Output: ('c++', 'java', 'haskell')
+
+Task 2: Largest of Three
+"""
+### solution by pokgopun@gmail.com
+
+def sortL(tup1,tup2: tuple):
+ return tuple(
+ tup[0] for tup in
+ sorted(
+ map(lambda e1,e2: (e1,e2), tup1, tup2),
+ key=lambda tup: tup[1]
+ )
+ )
+
+for (inpt1,inpt2), otpt in {
+ (('perl', 'c', 'python'), (2, 1, 3)): ('c', 'perl', 'python'),
+ (('c++', 'haskell', 'java'), (1, 3, 2)): ('c++', 'java', 'haskell'),
+ }.items():
+ print(otpt==sortL(inpt1, inpt2))
diff --git a/challenge-245/pokgopun/python/ch-2.py b/challenge-245/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..c1f88a7398
--- /dev/null
+++ b/challenge-245/pokgopun/python/ch-2.py
@@ -0,0 +1,77 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-245/
+"""
+
+Task 2: Largest of Three
+
+Submitted by: [46]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers >= 0.
+
+ Write a script to return the largest number formed by concatenating
+ some of the given integers in any order which is also multiple of 3.
+ Return -1 if none found.
+
+Example 1
+
+Input: @ints = (8, 1, 9)
+Output: 981
+
+981 % 3 == 0
+
+Example 2
+
+Input: @ints = (8, 6, 7, 1, 0)
+Output: 8760
+
+Example 3
+
+Input: @ints = (1)
+Output: -1
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 3rd December
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import permutations, chain
+
+def permutes(tup: tuple):
+ for n in range(1,len(tup)+1): ### need permutations(tup,n) loop instead of permutations(tup) alone to avoid a bug in itertools/permutations
+ yield permutations(tup, n)
+
+def intsOfThree(tup: tuple):
+ for ints in chain.from_iterable(permutes(tup)):
+ if sum(ints) % 3 == 0: ### If a + b = c , then a ( mod N ) + b ( mod N ) ≡ c ( mod N )
+ yield ints
+
+def intOfThree(tup: tuple):
+ for iStrs in ((str(i) for i in ints) for ints in intsOfThree(tup)): ### int-to-string conversion for ints so we can further concat and convert them to an int
+ yield int("".join(iStrs)) ### concat and convert int strings to an int
+
+def largestOfThree(tup: tuple):
+ return max( ### max() can avoid an error from null permute iterator as it operates on chain.from_iterables that include default (-1,)
+ chain.from_iterable(
+ (
+ intOfThree(tup),
+ (-1,) ### add (-1,) to chain.from_iterable to avoid max() error from null iterator from permutations (i.e. when nothing qualifies sum % 3 == 0
+ )
+ )
+ )
+
+for inpt, otpt in {
+ (8, 1, 9): 981,
+ (8, 6, 7, 1, 0): 8760,
+ (1,): -1,
+ (0,0,0): 0,
+ (4, 8, 911): 9114,
+ (8, 85, 0): 8850,
+ (8, 89, 2): 8982,
+ (8, 76, 0): 8760,
+ (8, 94, 0): 9480,
+ }.items():
+ print(otpt==largestOfThree(inpt))