aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-07-07 22:18:53 +1000
committerPok <pok@goyangi>2025-07-07 22:18:53 +1000
commitb4c18b7b77aaae65d55b5155d29131dccaf54ecb (patch)
tree98f624e18892d181d5882a28d4991c9f2c50147d
parentbd7fce4bd5d085c209a213f2daca1e79799c9e87 (diff)
downloadperlweeklychallenge-club-b4c18b7b77aaae65d55b5155d29131dccaf54ecb.tar.gz
perlweeklychallenge-club-b4c18b7b77aaae65d55b5155d29131dccaf54ecb.tar.bz2
perlweeklychallenge-club-b4c18b7b77aaae65d55b5155d29131dccaf54ecb.zip
pwc329 solution in python
-rw-r--r--challenge-329/pokgopun/python/ch-1.py54
-rw-r--r--challenge-329/pokgopun/python/ch-2.py73
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-329/pokgopun/python/ch-1.py b/challenge-329/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..c3bc755523
--- /dev/null
+++ b/challenge-329/pokgopun/python/ch-1.py
@@ -0,0 +1,54 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-329/
+"""
+
+Task 1: Counter Integers
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only lower case English letters and
+ digits.
+
+ Write a script to replace every non-digit character with a space and
+ then return all the distinct integers left.
+
+Example 1
+
+Input: $str = "the1weekly2challenge2"
+Output: 1, 2
+
+2 is appeared twice, so we count it one only.
+
+Example 2
+
+Input: $str = "go21od1lu5c7k"
+Output: 21, 1, 5, 7
+
+Example 3
+
+Input: $str = "4p3e2r1l"
+Output: 4, 3, 2, 1
+
+Task 2: Nice String
+"""
+### solution by pokgopun@gmail.com
+
+import re
+
+def ci(string: str) -> tuple[int]:
+ seen = dict()
+ return tuple(seen.setdefault(d,int(d)) for d in re.sub(r"\D+", " ", string).strip().split()
+ if seen.get(d) == None)
+
+import unittest
+
+class TestCi(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ "the1weekly2challenge2": (1, 2),
+ "go21od1lu5c7k": (21, 1, 5, 7),
+ "4p3e2r1l": (4, 3, 2, 1),
+ }.items():
+ self.assertEqual(ci(inpt), otpt)
+
+unittest.main()
diff --git a/challenge-329/pokgopun/python/ch-2.py b/challenge-329/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..d1913edfba
--- /dev/null
+++ b/challenge-329/pokgopun/python/ch-2.py
@@ -0,0 +1,73 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-329/
+"""
+
+Task 2: Nice String
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of lower and upper case English letters
+ only.
+
+ Write a script to return the longest substring of the give string which
+ is nice. A string is nice if, for every letter of the alphabet that the
+ string contains, it appears both in uppercase and lowercase.
+
+Example 1
+
+Input: $str = "YaaAho"
+Output: "aaA"
+
+Example 2
+
+Input: $str = "cC"
+Output: "cC"
+
+Example 3
+
+Input: $str = "A"
+Output: ""
+
+No nice string found.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 13th July 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def ns(string: str) -> str:
+ start = 0
+ nice = ""
+ for i in range(1,len(string)):
+ if string[i].lower() == string[i-1].lower():
+ continue
+ else:
+ cdd = string[start:i]
+ if len(set(cdd)) > 1 and len(cdd) > len(nice):
+ nice = cdd
+ start = i
+ else:
+ cdd = string[start:]
+ if len(set(cdd)) > 1 and len(cdd) > len(nice):
+ nice = cdd
+ return nice
+
+import unittest
+
+class TestNs(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "YaaAho": "aaA",
+ "YaaAhoooo": "aaA",
+ "YaaAhoOOo": "oOOo",
+ "YaaAhooO": "aaA",
+ "cC": "cC",
+ "cc": "",
+ "A": "",
+ }.items():
+ self.assertEqual(ns(inpt),otpt)
+
+unittest.main()