aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-09-17 07:28:23 +1000
committerMichael Manring <michael@manring>2024-09-17 07:28:23 +1000
commit817c01278db84de95c258049bba7c2dc1f92e567 (patch)
tree923e03347339db61e5fcb04507f4b4f433d9706e
parent68e321dd32a834f54b55d5e8924f04358e41cf1f (diff)
downloadperlweeklychallenge-club-817c01278db84de95c258049bba7c2dc1f92e567.tar.gz
perlweeklychallenge-club-817c01278db84de95c258049bba7c2dc1f92e567.tar.bz2
perlweeklychallenge-club-817c01278db84de95c258049bba7c2dc1f92e567.zip
pwc287 solution in python
-rw-r--r--challenge-287/pokgopun/python/ch-1.py106
-rw-r--r--challenge-287/pokgopun/python/ch-2.py96
2 files changed, 202 insertions, 0 deletions
diff --git a/challenge-287/pokgopun/python/ch-1.py b/challenge-287/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..c63ef17bee
--- /dev/null
+++ b/challenge-287/pokgopun/python/ch-1.py
@@ -0,0 +1,106 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-287/
+"""
+
+Task 1: Strong Password
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str.
+
+ Write a program to return the minimum number of steps required to make
+ the given string very strong password. If it is already strong then
+ return 0.
+
+ Criteria:
+- It must have at least 6 characters.
+- It must contains at least one lowercase letter, at least one upper case letter
+ and at least one digit.
+- It shouldn't contain 3 repeating characters in a row.
+
+ Following can be considered as one step:
+- Insert one character
+- Delete one character
+- Replace one character with another
+
+Example 1
+
+Input: $str = "a"
+Output: 5
+
+Example 2
+
+Input: $str = "aB2"
+Output: 3
+
+Example 3
+
+Input: $str = "PaaSW0rd"
+Output: 0
+
+Example 4
+
+Input: $str = "Paaasw0rd"
+Output: 1
+
+Example 5
+
+Input: $str = "aaaaa"
+Output: 2
+
+Task 2: Valid Number
+"""
+### solution by pokgopun@gmail.com
+
+def strongPassword(passwd: str):
+ cRpt = 0
+ prev,crpt = "", 0
+ hasDgt,hasUc,hasLc = False, False, False
+ for c in passwd:
+ if ord(c) >= 48 and ord(c) <= 57:
+ hasDgt = True
+ if ord(c) >= 65 and ord(c) <= 90:
+ hasUc = True
+ if ord(c) >= 97 and ord(c) <= 122:
+ hasLc = True
+ if prev == c:
+ crpt += 1
+ if crpt==3:
+ cRpt += 1
+ crpt = 0
+ prev = ""
+ else:
+ prev = c
+ crpt = 1
+ cHas = 0
+ for lgc in (hasDgt,hasUc,hasLc):
+ if lgc == False:
+ cHas += 1
+ mChr = 6 - min(6,len(passwd))
+ #print(mChr,cHas,cRpt)
+ return max(mChr, cHas, cRpt)
+
+import unittest
+
+class TestStringPassword(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "a": 5,
+ "aB2": 3,
+ "PaaSW0rd": 0,
+ "Paaasw0rd": 1,
+ "aaaaa": 2,
+ "aaaaaa": 2,
+ "aaaaaaa": 2,
+ "aaaaaaaa": 2,
+ "aaaaaaaaa": 3,
+ "aaaaaaaaaa": 3,
+ "aaa": 3,
+ "s3cret": 1,
+ "444o333": 2,
+ "44333": 2,
+ }.items():
+ #print(inpt,otpt)
+ self.assertEqual(strongPassword(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-287/pokgopun/python/ch-2.py b/challenge-287/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..2b0e029653
--- /dev/null
+++ b/challenge-287/pokgopun/python/ch-2.py
@@ -0,0 +1,96 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-287/
+"""
+
+Task 2: Valid Number
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str.
+
+ Write a script to find if it is a valid number.
+
+ Conditions for a valid number:
+- An integer number followed by an optional exponent.
+- A decimal number followed by an optional exponent.
+- An integer number is defined with an optional sign '-' or '+' followed by digi
+ts.
+
+ Decimal Number:
+A decimal number is defined with an optional sign '-' or '+' followed by one of
+the following definitions:
+- Digits followed by a dot '.'.
+- Digits followed by a dot '.' followed by digits.
+- A dot '.' followed by digits.
+
+ Exponent:
+An exponent is defined with an exponent notation 'e' or 'E' followed by an integ
+er number.
+
+Example 1
+
+Input: $str = "1"
+Output: true
+
+Example 2
+
+Input: $str = "a"
+Output: false
+
+Example 3
+
+Input: $str = "."
+Output: false
+
+Example 4
+
+Input: $str = "1.2e4.2"
+Output: false
+
+Example 5
+
+Input: $str = "-1."
+Output: true
+
+Example 6
+
+Input: $str = "+1E-8"
+Output: true
+
+Example 7
+
+Input: $str = ".44"
+Output: true
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 22nd September
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def validNumber(string: str):
+ try:
+ assert float(string)
+ except:
+ return False
+ return True
+
+import unittest
+
+class TestValidNumber(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ "1": True,
+ "a": False,
+ ".": False,
+ "1.2e4.2": False,
+ "-1.": True,
+ "+1E-8": True,
+ ".44": True,
+ }.items():
+ self.assertEqual(validNumber(inpt),otpt)
+
+unittest.main()