aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-17 10:18:12 +0100
committerGitHub <noreply@github.com>2024-09-17 10:18:12 +0100
commit3faccf43af8f3a2c7df4e9bab6f49994336e8b11 (patch)
tree9e75eac8c686b9f7b2fb232bf2f290fb2886ebb7
parentd9816a1cc5823117597d5e48aad03e01ef198424 (diff)
parent0107fbd7b814cabe19b82c89b4115282d85df92b (diff)
downloadperlweeklychallenge-club-3faccf43af8f3a2c7df4e9bab6f49994336e8b11.tar.gz
perlweeklychallenge-club-3faccf43af8f3a2c7df4e9bab6f49994336e8b11.tar.bz2
perlweeklychallenge-club-3faccf43af8f3a2c7df4e9bab6f49994336e8b11.zip
Merge pull request #10856 from pokgopun/pwc287
Pwc287
-rw-r--r--challenge-287/pokgopun/go/ch-1.go172
-rw-r--r--challenge-287/pokgopun/go/ch-2.go106
-rw-r--r--challenge-287/pokgopun/python/ch-1.py106
-rw-r--r--challenge-287/pokgopun/python/ch-2.py96
4 files changed, 480 insertions, 0 deletions
diff --git a/challenge-287/pokgopun/go/ch-1.go b/challenge-287/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..3f43aad971
--- /dev/null
+++ b/challenge-287/pokgopun/go/ch-1.go
@@ -0,0 +1,172 @@
+//# 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
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type myRune struct {
+ r *rune
+}
+
+func (mr *myRune) reset() {
+ mr.r = nil
+}
+
+func (mr *myRune) set(r rune) {
+ mr.r = &r
+}
+
+func (mr *myRune) equal(r rune) bool {
+ if mr.r == nil {
+ return false
+ }
+ return *mr.r == r
+}
+
+type runeChecker struct {
+ consecutiveCount, consecutiveLimit, countConsecutive int
+ r myRune
+}
+
+func (rc *runeChecker) check(r rune) {
+ if rc.r.equal(r) {
+ rc.consecutiveCount++
+ if rc.consecutiveCount == rc.consecutiveLimit {
+ rc.countConsecutive++
+ rc.r.reset()
+ }
+ } else {
+ rc.r.set(r)
+ rc.consecutiveCount = 1
+ }
+}
+
+type runeRange struct {
+ first, last rune
+ inExist bool
+}
+
+func (rr *runeRange) check(r rune) {
+ if !rr.inExist && r >= rr.first && r <= rr.last {
+ rr.inExist = true
+ }
+}
+
+type runeRanges []*runeRange
+
+func (rrs runeRanges) check(r rune) {
+ for _, rr := range rrs {
+ rr.check(r)
+ }
+}
+
+func (rrs runeRanges) countMissing() int {
+ c := 0
+ for _, rr := range rrs {
+ if !rr.inExist {
+ c++
+ }
+ }
+ return c
+}
+
+func strongPassword(str string) int {
+ l := len(str)
+ if l == 0 {
+ return 6
+ }
+ rrs := runeRanges{
+ &runeRange{first: '0', last: '9'},
+ &runeRange{first: 'A', last: 'Z'},
+ &runeRange{first: 'a', last: 'z'},
+ }
+ rc := runeChecker{consecutiveLimit: 3}
+ for _, v := range str {
+ rrs.check(v)
+ rc.check(v)
+ }
+ //fmt.Println(rrs.countMissing(), rc.countConsecutive, 6-min(6, l))
+ return max(rrs.countMissing(), rc.countConsecutive, 6-min(6, l))
+}
+
+func main() {
+ for _, data := range []struct {
+ input string
+ output int
+ }{
+ {"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},
+ } {
+ //fmt.Println(data.input, data.output)
+ io.WriteString(os.Stdout, cmp.Diff(strongPassword(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-287/pokgopun/go/ch-2.go b/challenge-287/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..9631fbc614
--- /dev/null
+++ b/challenge-287/pokgopun/go/ch-2.go
@@ -0,0 +1,106 @@
+//# 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
+
+package main
+
+import (
+ "io"
+ "os"
+ "strconv"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func validNumber(str string) bool {
+ if _, err := strconv.ParseFloat(str, 64); err != nil {
+ return false
+ }
+ return true
+}
+
+func main() {
+ for _, data := range []struct {
+ input string
+ output bool
+ }{
+ {"1", true},
+ {"a", false},
+ {".", false},
+ {"1.2e4.2", false},
+ {"-1.", true},
+ {"+1E-8", true},
+ {".44", true},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(validNumber(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
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()