aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-19 20:46:13 +0100
committerGitHub <noreply@github.com>2024-09-19 20:46:13 +0100
commit7c7ea0c93086bd4d0709fdffe7422a7f3a9bfdca (patch)
tree7d0d62dc4c61643a7055f97d9c9b61c3d00ac532
parent982acf4bf18dd4c4a07c25d9bb7cbf2dc73b3a87 (diff)
parent4282aa11920a4187a1f43423b1a6cf90bbd7d692 (diff)
downloadperlweeklychallenge-club-7c7ea0c93086bd4d0709fdffe7422a7f3a9bfdca.tar.gz
perlweeklychallenge-club-7c7ea0c93086bd4d0709fdffe7422a7f3a9bfdca.tar.bz2
perlweeklychallenge-club-7c7ea0c93086bd4d0709fdffe7422a7f3a9bfdca.zip
Merge pull request #10870 from pokgopun/pwc287
Pwc287 - revised ch-1
-rw-r--r--challenge-287/pokgopun/go/ch-1.go123
-rw-r--r--challenge-287/pokgopun/python/ch-1.py35
2 files changed, 80 insertions, 78 deletions
diff --git a/challenge-287/pokgopun/go/ch-1.go b/challenge-287/pokgopun/go/ch-1.go
index 3f43aad971..0679047954 100644
--- a/challenge-287/pokgopun/go/ch-1.go
+++ b/challenge-287/pokgopun/go/ch-1.go
@@ -61,89 +61,80 @@ import (
"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
+func consecutiveCharCount(str string, l int) int {
+ if len(str) < l {
+ return 0
}
- 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()
+ var (
+ count, cnt int
+ prev rune
+ )
+ for _, r := range str {
+ if cnt == 0 {
+ prev = r
+ cnt++
+ continue
+ }
+ if r == prev {
+ cnt++
+ if cnt == l {
+ count++
+ cnt = 0
+ }
+ } else {
+ prev = r
+ cnt = 1
}
- } else {
- rc.r.set(r)
- rc.consecutiveCount = 1
}
+ return count
}
-type runeRange struct {
+type charset 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 countRequiredCharset(str string, charsets []charset) int {
+ count := len(charsets)
+ m := make(map[charset]bool, count)
+ for _, v := range charsets {
+ m[v] = true
}
-}
-
-func (rrs runeRanges) countMissing() int {
- c := 0
- for _, rr := range rrs {
- if !rr.inExist {
- c++
+ //var n int
+ for _, c := range str {
+ //n++
+ for k, v := range m {
+ if v && c >= k.first && c <= k.last {
+ m[k] = false
+ count -= 1
+ break
+ }
+ }
+ if count == 0 {
+ break
}
}
- return c
+ //fmt.Println(n, "char(s) checked for required types")
+ return count
}
func strongPassword(str string) int {
+ requiredLength := 6
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)
+ return requiredLength
}
- //fmt.Println(rrs.countMissing(), rc.countConsecutive, 6-min(6, l))
- return max(rrs.countMissing(), rc.countConsecutive, 6-min(6, l))
+ requiredCharsetCount := countRequiredCharset(
+ str,
+ []charset{
+ charset{'0', '9'},
+ charset{'A', 'Z'},
+ charset{'a', 'z'},
+ },
+ )
+ consecutiveCharLength := 3
+ countConsecutiveChar := consecutiveCharCount(str, consecutiveCharLength)
+ //fmt.Println(requiredCharsetCount, countConsecutiveChar,requiredLength -min(requiredLength, l))
+ return max(requiredCharsetCount, countConsecutiveChar, requiredLength-min(requiredLength, l))
}
func main() {
diff --git a/challenge-287/pokgopun/python/ch-1.py b/challenge-287/pokgopun/python/ch-1.py
index 48421e37f3..70ce8f75a5 100644
--- a/challenge-287/pokgopun/python/ch-1.py
+++ b/challenge-287/pokgopun/python/ch-1.py
@@ -54,22 +54,33 @@ Task 2: Valid Number
def strongPassword(pwd: str):
requiredCharset = dict.fromkeys(("09","AZ","az"), True)
- countConsecutiveChar = 0
- prev = ""
- count = 0
+ countRequiredCharset = len(requiredCharset)
+ #n = 0
for c in pwd:
+ #n += 1
for charset in requiredCharset.keys():
if requiredCharset[charset] and c >= charset[0] and c <= charset[1]:
requiredCharset[charset] = False
- if prev == c:
- count += 1
- if count == 3:
- countConsecutiveChar += 1
- prev = ""
- else:
- prev = c
- count = 1
- countRequiredCharset = sum(requiredCharset.values())
+ countRequiredCharset -= 1
+ break
+ if countRequiredCharset==0:
+ break
+ #print(n,"char(s) checked for required charsets")
+ countConsecutiveChar = 0
+ consecutiveCharLength = 3
+ if len(pwd) >= consecutiveCharLength:
+ prev = ""
+ count = 0
+ for c in pwd:
+ #print(c,"<=>",prev)
+ if prev == c:
+ count += 1
+ if count == consecutiveCharLength:
+ countConsecutiveChar += 1
+ prev = ""
+ else:
+ prev = c
+ count = 1
countRequiredAddition = 6 - min(6, len(pwd))
#print(countRequiredAddition, countConsecutiveChar, countRequiredCharset)
return max(countRequiredAddition, countConsecutiveChar, countRequiredCharset)