aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-25 21:20:15 +0000
committerGitHub <noreply@github.com>2023-12-25 21:20:15 +0000
commit334ddbdc1e52305d6461c1d9621fcf6e20d463f3 (patch)
tree229404bc15c50fec57a1302b5a3db984fad5b5cb
parentb18ebd6f1801cab760d75199ee25f0e290f002d1 (diff)
parentfe7c62233cb5309173a1f5744a522635f7448131 (diff)
downloadperlweeklychallenge-club-334ddbdc1e52305d6461c1d9621fcf6e20d463f3.tar.gz
perlweeklychallenge-club-334ddbdc1e52305d6461c1d9621fcf6e20d463f3.tar.bz2
perlweeklychallenge-club-334ddbdc1e52305d6461c1d9621fcf6e20d463f3.zip
Merge pull request #9289 from pokgopun/pwc249
pwc249 solution
-rw-r--r--challenge-249/pokgopun/python/ch-1.py57
-rw-r--r--challenge-249/pokgopun/python/ch-2.py73
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-249/pokgopun/python/ch-1.py b/challenge-249/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..cef9601557
--- /dev/null
+++ b/challenge-249/pokgopun/python/ch-1.py
@@ -0,0 +1,57 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-249/
+"""
+
+Task 1: Shortest Distance
+
+Submitted by: [66]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers with even number of elements.
+
+ Write a script to divide the given array into equal pairs such that:
+a) Each element belongs to exactly one pair.
+b) The elements present in a pair are equal.
+
+Example 1
+
+Input: @ints = (3, 2, 3, 2, 2, 2)
+Output: (2, 2), (3, 3), (2, 2)
+
+There are 6 elements in @ints.
+They should be divided into 6 / 2 = 3 pairs.
+@ints is divided into the pairs (2, 2), (3, 3), and (2, 2) satisfying all the co
+nditions.
+
+Example 2
+
+Input: @ints = (1, 2, 3, 4)
+Output: ()
+
+There is no way to divide @ints 2 pairs such that the pairs satisfy every condit
+ion.
+
+Task 2: DI String Match
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import chain
+
+def equalPairs(tup: tuple):
+ return tuple(
+ chain.from_iterable(
+ ( (e,e) for c in range(tup.count(e)//2 ) )
+ for e in set(tup)
+ )
+ )
+
+import unittest
+
+class TestEqualPairs(unittest.TestCase):
+ def test1(self):
+ for inpt,otpt in {
+ (3, 2, 3, 2, 2, 2): ((2, 2), (3, 3), (2, 2)),
+ (1, 2, 3, 4): (),
+ }.items():
+ self.assertEqual(sorted(equalPairs(inpt)),sorted(otpt))
+
+unittest.main()
diff --git a/challenge-249/pokgopun/python/ch-2.py b/challenge-249/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..f535069d2e
--- /dev/null
+++ b/challenge-249/pokgopun/python/ch-2.py
@@ -0,0 +1,73 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-249/
+"""
+
+Task 2: DI String Match
+
+Submitted by: [67]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given a string s, consisting of only the characters "D" and
+ "I".
+
+ Find a permutation of the integers [0 .. length(s)] such that for each
+ character s[i] in the string:
+s[i] == 'I' ⇒ perm[i] < perm[i + 1]
+s[i] == 'D' ⇒ perm[i] > perm[i + 1]
+
+Example 1
+
+Input: $str = "IDID"
+Output: (0, 4, 1, 3, 2)
+
+Example 2
+
+Input: $str = "III"
+Output: (0, 1, 2, 3)
+
+Example 3
+
+Input: $str = "DDI"
+Output: (3, 2, 0, 1)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 31st December
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import permutations
+
+def dism(s: str):
+ t = tuple()
+ l = len(s)
+ for perm in permutations(tuple(range(l+1)),l+1):
+ #print(perm)
+ for i in range(l):
+ #print(f"{s[i]},{perm[i]},{perm[i+1]}")
+ if s[i]=="I":
+ if perm[i] >= perm[i+1]:
+ break
+ elif s[i]=="D":
+ if perm[i] <= perm[i+1]:
+ break
+ else:
+ #print("=>",perm)
+ t += (perm,)
+ #print(t)
+ return t
+
+import unittest
+
+class TestDism(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ "IDID": (0, 4, 1, 3, 2),
+ "III": (0, 1, 2, 3),
+ "DDI": (3, 2, 0, 1),
+ }.items():
+ self.assertEqual(dism(inpt).count(otpt)>0,True)
+
+unittest.main()