aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-22 21:03:12 +0100
committerGitHub <noreply@github.com>2024-09-22 21:03:12 +0100
commit37249de613745e952811b997f60779ebcc39835d (patch)
treed3f79d883c9b8f3999199633458dcb56dc8358d5
parent8ff45dfef41651acb36f11eb8585ebc5c1957286 (diff)
parent9e99d7529d22b326148431954541f6ffdd76497f (diff)
downloadperlweeklychallenge-club-37249de613745e952811b997f60779ebcc39835d.tar.gz
perlweeklychallenge-club-37249de613745e952811b997f60779ebcc39835d.tar.bz2
perlweeklychallenge-club-37249de613745e952811b997f60779ebcc39835d.zip
Merge pull request #10887 from simongreen-net/master
sgreen solutions to challenge 287
-rw-r--r--challenge-286/sgreen/README.md3
-rw-r--r--challenge-287/sgreen/README.md4
-rw-r--r--challenge-287/sgreen/blog.txt1
-rwxr-xr-xchallenge-287/sgreen/perl/ch-1.pl36
-rwxr-xr-xchallenge-287/sgreen/perl/ch-2.pl19
-rwxr-xr-xchallenge-287/sgreen/python/ch-1.py46
-rwxr-xr-xchallenge-287/sgreen/python/ch-2.py25
-rwxr-xr-xchallenge-287/sgreen/python/test.py27
8 files changed, 156 insertions, 5 deletions
diff --git a/challenge-286/sgreen/README.md b/challenge-286/sgreen/README.md
deleted file mode 100644
index 9259df2b60..0000000000
--- a/challenge-286/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 285
-
-Blog: [Making connections](https://dev.to/simongreennet/making-connections-13p8)
diff --git a/challenge-287/sgreen/README.md b/challenge-287/sgreen/README.md
index 9259df2b60..edb0f8425e 100644
--- a/challenge-287/sgreen/README.md
+++ b/challenge-287/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 285
+# The Weekly Challenge 287
-Blog: [Making connections](https://dev.to/simongreennet/making-connections-13p8)
+Blog: [Good things](https://dev.to/simongreennet/good-things-1e3a)
diff --git a/challenge-287/sgreen/blog.txt b/challenge-287/sgreen/blog.txt
new file mode 100644
index 0000000000..01489ea41d
--- /dev/null
+++ b/challenge-287/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/good-things-1e3a \ No newline at end of file
diff --git a/challenge-287/sgreen/perl/ch-1.pl b/challenge-287/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..fb99026033
--- /dev/null
+++ b/challenge-287/sgreen/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'max';
+
+sub strong_password($password) {
+# The minimum number of steps required to make the given string very strong password.
+
+ # Count consecutive characters
+ my $cons_count = 0;
+ my @c = ( $password =~ /((.)\2{2,})/g );
+ for (my $i = 0; $i < $#c; $i++) {
+ # For every 3 consecutive characters, we need to replace one
+ $cons_count += int( length($c[$i]) / 3 );
+ }
+
+ # Additional characters require to make it at least 6 characters
+ my $char_count = max( 0, 6 - length($password) );
+
+ # Count the number of missing character types
+ my $type_count = 0;
+ $type_count++ if $password !~ /[a-z]/;
+ $type_count++ if $password !~ /[A-Z]/;
+ $type_count++ if $password !~ /[0-9]/;
+
+ # Since the type change can be covered by one of the other required
+ # changes, return the maximum of the two
+ my $count = max( $cons_count + $char_count, $type_count );
+ say $count;
+}
+
+strong_password( $ARGV[0] ); \ No newline at end of file
diff --git a/challenge-287/sgreen/perl/ch-2.pl b/challenge-287/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..28610e76e7
--- /dev/null
+++ b/challenge-287/sgreen/perl/ch-2.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub valid_number ($str) {
+ # Check if the given string is a valid number
+
+ if ($str =~ /^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][+-]?[0-9]+)?$/) {
+ say 'true';
+ }
+ else {
+ say 'false';
+ }
+}
+
+valid_number($ARGV[0]); \ No newline at end of file
diff --git a/challenge-287/sgreen/python/ch-1.py b/challenge-287/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..54a595cdc6
--- /dev/null
+++ b/challenge-287/sgreen/python/ch-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def strong_password(password: str) -> int:
+ """The minimum number of steps required to make the given string very strong password.
+
+ Args:
+ password (str): The supplied password.
+
+ Returns:
+ int: The minimum number of steps required.
+ """
+
+ # Count consecutive characters
+ cons_count = 0
+ for c in re.findall(r'((.)\2{2,})', password):
+ # For every 3 consecutive characters, we need to replace one
+ cons_count += len(c[0]) // 3
+
+ # Additional characters require to make it at least 6 characters
+ char_count = max(0, 6 - len(password))
+
+ # Count the number of missing character types
+ type_count = 0
+ if not re.search(r'[a-z]', password):
+ type_count += 1
+ if not re.search(r'[A-Z]', password):
+ type_count += 1
+ if not re.search(r'[0-9]', password):
+ type_count += 1
+
+ # Since the type change can be covered by one of the other required
+ # changes, return the maximum of the two
+ return max(cons_count + char_count, type_count)
+
+
+def main():
+ result = strong_password(sys.argv[1])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-287/sgreen/python/ch-2.py b/challenge-287/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..87059a0873
--- /dev/null
+++ b/challenge-287/sgreen/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def valid_number(s: str) -> bool:
+ """Check if the given string is a valid number.
+
+ Args:
+ s (str): The supplied string.
+
+ Returns:
+ bool: True if the string is a valid number, False otherwise.
+ """
+ return bool(re.search(r'^[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][+-]?[0-9]+)?$', s))
+
+
+def main():
+ result = valid_number(sys.argv[1])
+ print('true' if result else 'false')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-287/sgreen/python/test.py b/challenge-287/sgreen/python/test.py
new file mode 100755
index 0000000000..33582047ac
--- /dev/null
+++ b/challenge-287/sgreen/python/test.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.strong_password('a'), 5)
+ self.assertEqual(ch_1.strong_password('aB2'), 3)
+ self.assertEqual(ch_1.strong_password('PaaSW0rd'), 0)
+ self.assertEqual(ch_1.strong_password('Paaasw0rd'), 1)
+ self.assertEqual(ch_1.strong_password('aaaaa'), 2)
+
+ def test_ch_2(self):
+ self.assertTrue(ch_2.valid_number('1'))
+ self.assertFalse(ch_2.valid_number('a'))
+ self.assertFalse(ch_2.valid_number('.'))
+ self.assertFalse(ch_2.valid_number('1.2e4.2'))
+ self.assertTrue(ch_2.valid_number('-1'))
+ self.assertTrue(ch_2.valid_number('+1E-8'))
+ self.assertTrue(ch_2.valid_number('.44'))
+
+
+if __name__ == '__main__':
+ unittest.main()