aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-06-30 23:43:46 +1000
committerPok <pok@goyangi>2025-06-30 23:43:46 +1000
commitfc87e2e40438cd906cd1d82089842081163ad644 (patch)
tree51f8515c96859f55a40bd0eba074a0e88bfc2faf
parent7daf92e1dd4a2726fc578e87b6364ba4db3d5ad9 (diff)
downloadperlweeklychallenge-club-fc87e2e40438cd906cd1d82089842081163ad644.tar.gz
perlweeklychallenge-club-fc87e2e40438cd906cd1d82089842081163ad644.tar.bz2
perlweeklychallenge-club-fc87e2e40438cd906cd1d82089842081163ad644.zip
pwc328 solution in python
-rw-r--r--challenge-328/pokgopun/python/ch-1.py71
-rw-r--r--challenge-328/pokgopun/python/ch-2.py67
2 files changed, 138 insertions, 0 deletions
diff --git a/challenge-328/pokgopun/python/ch-1.py b/challenge-328/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..442de91ec6
--- /dev/null
+++ b/challenge-328/pokgopun/python/ch-1.py
@@ -0,0 +1,71 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-328/
+"""
+
+Task 1: Replace all ?
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only lower case English letters and
+ ?.
+
+ Write a script to replace all ? in the given string so that the string
+ doesn’t contain consecutive repeating characters.
+
+Example 1
+
+Input: $str = "a?z"
+Output: "abz"
+
+There can be many strings, one of them is "abz".
+The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?'
+.
+
+Example 2
+
+Input: $str = "pe?k"
+Output: "peak"
+
+Example 3
+
+Input: $str = "gra?te"
+Output: "grabte"
+
+Task 2: Good String
+"""
+### solution by pokgopun@gmail.com
+
+def ra(string: str) -> str:
+ chrs = list(string)
+ l = len(chrs)
+ first = ord('a')
+ last = ord('z')
+ for i in range(l):
+ if chrs[i] == '?':
+ lst: list[str] = []
+ if i > 0:
+ prv = chrs[i-1]
+ if prv.isalpha():
+ lst.append(ord(prv))
+ if i < l - 1:
+ nxt = chrs[i+1]
+ if nxt.isalpha():
+ lst.append(ord(nxt))
+ for j in range(first,last+1):
+ if j not in lst:
+ chrs[i] = chr(j)
+ break
+ return "".join(chrs)
+
+import unittest
+
+class TestRa(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "a?z": "abz",
+ "pe?k": "peak",
+ "gra?te": "grabte",
+ }.items():
+ self.assertEqual(ra(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-328/pokgopun/python/ch-2.py b/challenge-328/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..11020d69a1
--- /dev/null
+++ b/challenge-328/pokgopun/python/ch-2.py
@@ -0,0 +1,67 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-328/
+"""
+
+Task 2: Good String
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string made up of lower and upper case English letters
+ only.
+
+ Write a script to return the good string of the given string. A string
+ is called good string if it doesn’t have two adjacent same characters,
+ one in upper case and other is lower case.
+
+Example 1
+
+Input: $str = "WeEeekly"
+Output: "Weekly"
+
+We can remove either, "eE" or "Ee" to make it good.
+
+Example 2
+
+Input: $str = "abBAdD"
+Output: ""
+
+We remove "bB" first: "aAdD"
+Then we remove "aA": "dD"
+Finally remove "dD".
+
+Example 3
+
+Input: $str = "abc"
+Output: "abc"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 6th July 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def gs(string: str) -> str:
+ chrs = list(string)
+ while True:
+ for i in range(1,len(chrs)):
+ if abs(ord(chrs[i])-ord(chrs[i-1])) == 32:
+ chrs = chrs[:i-1] + chrs[i+1:]
+ break
+ else:
+ break
+ return "".join(chrs)
+
+import unittest
+
+class TestGs(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "WeEeekly": "Weekly",
+ "abBAdD": "",
+ "abc": "abc",
+ }.items():
+ self.assertEqual(gs(inpt),otpt)
+
+unittest.main()