diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-17 12:54:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-17 12:54:07 +0100 |
| commit | 7eab363c30f0bb0fb5f4a656af08a5e4678f04be (patch) | |
| tree | 32ca95b2bfab3e5fa86fd23b54be10883a282da4 | |
| parent | adf98a527d178b4ae8bf8fc8098fbc49bb0ffdd0 (diff) | |
| parent | c65277c75b4f825c13da82040273b63915ed4a74 (diff) | |
| download | perlweeklychallenge-club-7eab363c30f0bb0fb5f4a656af08a5e4678f04be.tar.gz perlweeklychallenge-club-7eab363c30f0bb0fb5f4a656af08a5e4678f04be.tar.bz2 perlweeklychallenge-club-7eab363c30f0bb0fb5f4a656af08a5e4678f04be.zip | |
Merge pull request #10858 from Firedrake/rogerbw-challenge-287
RogerBW solutions for challenge no. 287
22 files changed, 1415 insertions, 0 deletions
diff --git a/challenge-287/roger-bell-west/crystal/ch-1.cr b/challenge-287/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..fce9127cc4 --- /dev/null +++ b/challenge-287/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,61 @@ +#! /usr/bin/crystal + +def strongpassword(a) + ctypes = Hash(Char, Int32).new(default_value: 0) + reps = 0 + rep = 0 + old = '@' + changes = Array(Int32).new + a.chars.each_with_index do |c, i| + if i > 0 && c == old + rep += 1 + if rep >= 2 + reps += 1 + end + else + rep = 0 + old = c + end + t = 'n' + if c.ascii_uppercase? + t = 'u' + elsif c.ascii_lowercase? + t = 'l' + elsif c >= '0' && c <= '9' + t = 'd' + end + ctypes[t] += 1 + end + changes.push(reps) + ctypes.delete('n') + k = ctypes.keys.size + if k < 3 + spare = ctypes.values.sum - k + if spare < 0 + changes.push(-spare) + end + end + if a.size < 6 + changes.push(6 - a.size) + end + changes.max +end + +require "spec" +describe "strongpassword" do + it "test_ex1" do + strongpassword("a").should eq 5 + end + it "test_ex2" do + strongpassword("aB2").should eq 3 + end + it "test_ex3" do + strongpassword("PaaSW0rd").should eq 0 + end + it "test_ex4" do + strongpassword("Paaasw0rd").should eq 1 + end + it "test_ex5" do + strongpassword("aaaaa").should eq 3 + end +end diff --git a/challenge-287/roger-bell-west/crystal/ch-2.cr b/challenge-287/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..0ed51ba306 --- /dev/null +++ b/challenge-287/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,40 @@ +#! /usr/bin/crystal + +def validnumber(a) + integer = "[-+]?[0-9]+" + float = "[-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)" + exponential = + "(" + integer + "|" + float + ")[Ee]" + integer + number = + "^(" + integer + "|" + float + "|" + exponential + ")$" + rx = Regex.new(number) + if rx.match(a) + return true + end + false +end + +require "spec" +describe "validnumber" do + it "test_ex1" do + validnumber("1").should eq true + end + it "test_ex2" do + validnumber("a").should eq false + end + it "test_ex3" do + validnumber(".").should eq false + end + it "test_ex4" do + validnumber("1.2e4.2").should eq false + end + it "test_ex5" do + validnumber("-1.").should eq true + end + it "test_ex6" do + validnumber("+1E-8").should eq true + end + it "test_ex7" do + validnumber(".44").should eq true + end +end diff --git a/challenge-287/roger-bell-west/javascript/ch-1.js b/challenge-287/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..39f675c3de --- /dev/null +++ b/challenge-287/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,78 @@ +#! /usr/bin/node + +"use strict" + +function strongpassword(a) { + let ctypes = new Map(); + let reps = 0; + let rep = 0; + let old = '@'; + let changes = []; + a.split("").forEach((c, i) => { + if (i > 0 && c == old) { + rep += 1; + if (rep >= 2) { + reps += 1; + } + } else { + rep = 0; + old = c; + } + let t = 'n'; + if (c >= 'A' && c <= 'Z') { + t = 'u'; + } else if (c >= 'a' && c <= 'z') { + t = 'l'; + } else if (c >= '0' && c <= '9') { + t = 'd'; + } + if (!ctypes.has(t)) { + ctypes.set(t, 0); + } + ctypes.set(t, ctypes.get(t) + 1); + }); + changes.push(reps); + ctypes.delete('n'); + let k = ctypes.keys().size; + if (k < 3) { + let spare = ctypes.values().reduce((x, y) => x + y) - k; + if (spare < 0) { + changes.push(-spare); + } + } + if (a.length < 6) { + changes.push(6 - a.length); + } + return Math.max(...changes); +} + +if (strongpassword('a') == 5) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (strongpassword('aB2') == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (strongpassword('PaaSW0rd') == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (strongpassword('Paaasw0rd') == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (strongpassword('aaaaa') == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-287/roger-bell-west/javascript/ch-2.js b/challenge-287/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..b78443c149 --- /dev/null +++ b/challenge-287/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,57 @@ +#! /usr/bin/node + +"use strict" + +function validnumber(a) { + const integer = "[-+]?[0-9]+"; + const float = "[-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)"; + const exponential = + "(" + integer + "|" + float + ")[Ee]" + integer; + const number = + "^(" + integer + "|" + float + "|" + exponential + ")$"; + const rx = new RegExp(number); + return a.search(rx) != -1; +} + +if (validnumber('1')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!validnumber('a')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!validnumber('.')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!validnumber('1.2e4.2')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (validnumber('-1.')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (validnumber('+1E-8')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (validnumber('.44')) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-287/roger-bell-west/kotlin/ch-1.kt b/challenge-287/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..97f9736560 --- /dev/null +++ b/challenge-287/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,75 @@ +fun strongpassword(a: String): Int { + var ctypes = mutableMapOf<Char, Int>().withDefault({0}) + var reps = 0 + var rep = 0 + var old = '@' + var changes = ArrayList<Int>() + a.toList().forEachIndexed{i, c -> + if (i > 0 && c == old) { + rep += 1 + if (rep >= 2) { + reps += 1 + } + } else { + rep = 0 + old = c + } + var t = 'n' + if (c >= 'A' && c <= 'Z') { + t = 'u' + } else if (c >= 'a' && c <= 'z') { + t = 'l' + } else if (c >= '0' && c <= '9') { + t = 'd' + } + ctypes.set(t, ctypes.getValue(t) + 1) + } + changes.add(reps) + ctypes.remove('n') + var k = ctypes.keys.size + if (k < 3) { + var spare = ctypes.values.reduce{x, y -> x + y} - k + if (spare < 0) { + changes.add(-spare) + } + } + if (a.length < 6) { + changes.add(6 - a.length) + } + return changes.maxOrNull()!! +} + +fun main() { + + if (strongpassword("a") == 5) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (strongpassword("aB2") == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (strongpassword("PaaSW0rd") == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (strongpassword("Paaasw0rd") == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (strongpassword("aaaaa") == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-287/roger-bell-west/kotlin/ch-2.kt b/challenge-287/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..554ef84089 --- /dev/null +++ b/challenge-287/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,57 @@ +fun validnumber(a: String): Boolean { + val integer = "[-+]?[0-9]+" + val float = """[-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)""" + val exponential = + "(" + integer + "|" + float + ")[Ee]" + integer + val number = + "^(" + integer + "|" + float + "|" + exponential + ")$" + val rx = number.toRegex() + return rx.containsMatchIn(a) +} + +fun main() { + + if (validnumber("1")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!validnumber("a")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!validnumber(".")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!validnumber("1.2e4.2")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (validnumber("-1.")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (validnumber("+1E-8")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (validnumber(".44")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-287/roger-bell-west/lua/ch-1.lua b/challenge-287/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..4168b7024b --- /dev/null +++ b/challenge-287/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,116 @@ +#! /usr/bin/lua + +function split(t) + local cl = {} + string.gsub(t, + "(.)", + function(c) + table.insert(cl, c) + end + ) + return cl +end + +function keys(t) + local a = {} + for k, v in pairs(t) do + table.insert(a, k) + end + return a +end + +function values(t) + local a = {} + for k, v in pairs(t) do + table.insert(a, v) + end + return a +end + +function sum(t) + local ss = 0 + for i, k in ipairs(t) do + ss = ss + k + end + return ss +end + +function strongpassword(a) + local ctypes = {} + local reps = 0 + local rep = 0 + local old = '@' + local changes = {} + for i, c in ipairs(split(a)) do + if (i > 0 and c == old) then + rep = rep + 1 + if rep >= 2 then + reps = reps + 1 + end + else + rep = 0 + old = c + end + local t = 'n' + if c >= 'A' and c <= 'Z' then + t = 'u' + elseif c >= 'a' and c <= 'z' then + t = 'l' + elseif c >= '0' and c <= '9' then + t = 'd' + end + if ctypes[t] == nil then + ctypes[t] = 0 + end + ctypes[t] = ctypes[t] + 1 + end + table.insert(changes, reps) + ctypes['n'] = nil + local k = #keys(ctypes) + if (k < 3) then + local spare = sum(values(ctypes)) - k + if spare < 0 then + table.insert(changes, -spare) + end + end + if (string.len(a) < 6) then + table.insert(changes, 6 - string.len(a)) + end + return math.max(table.unpack(changes)) +end + +if strongpassword("a") == 5 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if strongpassword("aB2") == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if strongpassword("PaaSW0rd") == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if strongpassword("Paaasw0rd") == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if strongpassword("aaaaa") == 3 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-287/roger-bell-west/lua/ch-2.lua b/challenge-287/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..1102f0936a --- /dev/null +++ b/challenge-287/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,64 @@ +#! /usr/bin/lua + +function validnumber(a) + local integer = "[-+]?%d+" + local float1 = "[-+]?%d+%.%d*" + local float2 = "[-+]?%d*%.%d+" + return + string.match(a, "^" .. integer .. "$") ~= nil or + string.match(a, "^" .. float1 .. "$") ~= nil or + string.match(a, "^" .. float2 .. "$") ~= nil or + string.match(a, "^" .. integer .. "[Ee]" .. integer .. "$") ~= nil or + string.match(a, "^" .. float1 .. "[Ee]" .. integer .. "$") ~= nil or + string.match(a, "^" .. float2 .. "[Ee]" .. integer .. "$") ~= nil +end + +if validnumber("1") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not validnumber("a") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not validnumber(".") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not validnumber("1.2e4.2") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if validnumber("-1.") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if validnumber("+1E-8") then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if validnumber(".44") then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-287/roger-bell-west/perl/ch-1.pl b/challenge-287/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..45996031ce --- /dev/null +++ b/challenge-287/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,57 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(strongpassword('a'), 5, 'example 1'); +is(strongpassword('aB2'), 3, 'example 2'); +is(strongpassword('PaaSW0rd'), 0, 'example 3'); +is(strongpassword('Paaasw0rd'), 1, 'example 4'); +is(strongpassword('aaaaa'), 3, 'example 5'); + +use List::Util qw(sum max); + +sub strongpassword($a) { + my %ctypes; + my $reps = 0; + my $rep = 0; + my $old = '@'; + my @changes; + my @chars = split '',$a; + while (my ($i, $c) = each @chars) { + if ($i > 0 && $c eq $old) { + $rep += 1; + if ($rep >= 2) { + $reps += 1; + } + } else { + $rep = 0; + $old = $c; + } + my $t = 'n'; + if ($c =~ /[[:upper:]]/) { + $t = 'u'; + } elsif ($c =~ /[[:lower:]]/) { + $t = 'l'; + } elsif ($c =~ /[[:digit:]]/) { + $t = 'd'; + } + $ctypes{$t}++; + } + push @changes, $reps; + delete $ctypes{n}; + my $k = scalar keys %ctypes; + if ($k < 3) { + my $spare = sum(values %ctypes) - $k; + if ($spare < 0) { + push @changes, -$spare; + } + } + if (length($a) < 6) { + push @changes, 6 - length($a); + } + return max(@changes); +} diff --git a/challenge-287/roger-bell-west/perl/ch-2.pl b/challenge-287/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..97ba6f31db --- /dev/null +++ b/challenge-287/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,29 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 7; + +is(validnumber('1'), 1, 'example 1'); +is(validnumber('a'), 0, 'example 2'); +is(validnumber('.'), 0, 'example 3'); +is(validnumber('1.2e4.2'), 0, 'example 4'); +is(validnumber('-1.'), 1, 'example 5'); +is(validnumber('+1E-8'), 1, 'example 6'); +is(validnumber('.44'), 1, 'example 7'); + +sub validnumber($a) { + my $integer = '[-+]?[0-9]+'; + my $float = '[-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)'; + my $exponential = + '(' . $integer . '|' . $float . ')[Ee]' . $integer; + my $number = + '^(' . $integer . '|' . $float . '|' . $exponential . ')$'; + if ($a =~ /$number/) { + return 1; + } else { + return 0; + } +} diff --git a/challenge-287/roger-bell-west/postscript/ch-1.ps b/challenge-287/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..0226cc2f08 --- /dev/null +++ b/challenge-287/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,169 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + +/values { % dict -> array of dict values + [ exch + { + exch pop + } forall + ] +} bind def + +/listmax { + { max } reduce +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + + +/c.islower { + dup 97 ge exch 122 le and +} bind def + +/s2a { + [ exch { } forall ] +} bind def + +/c.isdigit { + dup 48 ge exch 57 le and +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/dget { + 3 1 roll + 2 copy + known { + get exch pop + } { + pop pop + } ifelse +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/c.isupper { + dup 65 ge exch 90 le and +} bind def + + +% end included library code + +/strongpassword { + 0 dict begin + /ctypes 0 dict def + /reps 0 def + /rep 0 def + /old 64 def + /a exch def + [ + 0 + a s2a enumerate.array { + aload pop + /c exch def + /i exch def + i 0 gt c old eq and { + /rep rep 1 add def + rep 2 ge { + /reps reps 1 add def + } if + } { + /rep 0 def + /old c def + } ifelse + /t 0 def + 1 { + c c.isupper { + /t 1 def + exit + } if + c c.islower { + /t 2 def + exit + } if + c c.isdigit { + /t 3 def + } if + } repeat + ctypes t 2 copy 0 dget 1 add put + } forall + reps + ctypes 0 undef + /k ctypes keys length def + k 3 lt { + /spare ctypes values { add } reduce k sub def + spare 0 lt { + spare neg + } if + } if + a length 6 lt { + 6 a length sub + } if + ] listmax + end +} bind def + +(strongpassword) test.start +(a) strongpassword 5 eq test +(aB2) strongpassword 3 eq test +(PaaSW0rd) strongpassword 0 eq test +(Paaasw0rd) strongpassword 1 eq test +(aaaaa) strongpassword 3 eq test +test.end diff --git a/challenge-287/roger-bell-west/python/ch-1.py b/challenge-287/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..c387ae56fc --- /dev/null +++ b/challenge-287/roger-bell-west/python/ch-1.py @@ -0,0 +1,59 @@ +#! /usr/bin/python3 + +from collections import defaultdict + +def strongpassword(a): + ctypes = defaultdict(lambda: 0) + reps = 0 + rep = 0 + old = '@' + changes = [] + for i, c in enumerate(a): + if i > 0 and c == old: + rep += 1 + if rep >= 2: + reps += 1 + else: + rep = 0 + old = c + t = 'n' + if c.isupper(): + t = 'u' + elif c.islower(): + t = 'l' + elif c.isdigit(): + t = 'd' + ctypes[t] += 1 + changes.append(reps) + if 'n' in ctypes: + del ctypes['n'] + k = len(ctypes.keys()) + if k < 3: + spare = sum(ctypes.values()) - k + if spare < 0: + changes.append(-spare) + if len(a) < 6: + changes.append(6 - len(a)) + return max(changes) + + +import unittest + +class TestStrongpassword(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(strongpassword("a"), 5, 'example 1') + + def test_ex2(self): + self.assertEqual(strongpassword("aB2"), 3, 'example 2') + + def test_ex3(self): + self.assertEqual(strongpassword("PaaSW0rd"), 0, 'example 3') + + def test_ex4(self): + self.assertEqual(strongpassword("Paaasw0rd"), 1, 'example 4') + + def test_ex5(self): + self.assertEqual(strongpassword("aaaaa"), 3, 'example 5') + +unittest.main() diff --git a/challenge-287/roger-bell-west/python/ch-2.py b/challenge-287/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..7197f9b4f3 --- /dev/null +++ b/challenge-287/roger-bell-west/python/ch-2.py @@ -0,0 +1,40 @@ +#! /usr/bin/python3 + +import re + +def validnumber(a): + integer = r"[-+]?[0-9]+"; + float = r"[-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)"; + exponential = "(" + integer + "|" + float + ")[Ee]" + integer; + number = "^(" + integer + "|" + float + "|" +exponential + ")$"; + rx = re.compile(number) + if re.match(rx, a): + return True + return False + +import unittest + +class TestValidnumber(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(validnumber("1"), True, 'example 1') + + def test_ex2(self): + self.assertEqual(validnumber("a"), False, 'example 2') + + def test_ex3(self): + self.assertEqual(validnumber("."), False, 'example 3') + + def test_ex4(self): + self.assertEqual(validnumber("1.2e4.2"), False, 'example 4') + + def test_ex5(self): + self.assertEqual(validnumber("-1."), True, 'example 5') + + def test_ex6(self): + self.assertEqual(validnumber("+1E-8"), True, 'example 6') + + def test_ex7(self): + self.assertEqual(validnumber(".44"), True, 'example 7') + +unittest.main() diff --git a/challenge-287/roger-bell-west/raku/ch-1.p6 b/challenge-287/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..ec96d62c02 --- /dev/null +++ b/challenge-287/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,53 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(strongpassword('a'), 5, 'example 1'); +is(strongpassword('aB2'), 3, 'example 2'); +is(strongpassword('PaaSW0rd'), 0, 'example 3'); +is(strongpassword('Paaasw0rd'), 1, 'example 4'); +is(strongpassword('aaaaa'), 3, 'example 5'); + +sub strongpassword($a) { + my %ctypes; + my $reps = 0; + my $rep = 0; + my $old = '@'; + my @changes; + my @chars = $a.comb; + for @chars.kv -> $i, $c { + if ($i > 0 && $c eq $old) { + $rep += 1; + if ($rep >= 2) { + $reps += 1; + } + } else { + $rep = 0; + $old = $c; + } + my $t = 'n'; + if ($c ~~ /<upper>/) { + $t = 'u'; + } elsif ($c ~~ /<lower>/) { + $t = 'l'; + } elsif ($c ~~ /<digit>/) { + $t = 'd'; + } + %ctypes{$t}++; + } + @changes.push($reps); + %ctypes{'n'}:delete; + my $k = %ctypes.keys.elems; + if ($k < 3) { + my $spare = %ctypes.values.sum - $k; + if ($spare < 0) { + @changes.push(-$spare); + } + } + if ($a.chars < 6) { + @changes.push(6 - $a.chars); + } + return @changes.max; +} diff --git a/challenge-287/roger-bell-west/raku/ch-2.p6 b/challenge-287/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..e7560cd43d --- /dev/null +++ b/challenge-287/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,27 @@ +#! /usr/bin/raku + +use Test; |
