aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-06-16 20:43:00 +1000
committerSimon Green <mail@simon.green>2024-06-16 20:43:00 +1000
commit84f502b31e856448e6e58a182c99180e0e9783ae (patch)
treed48a351b20fe5add5fec2fcdf7f1e159da67ca71
parent514530d6be7cc5067a95a11ebedff9e0d4d46cfe (diff)
downloadperlweeklychallenge-club-84f502b31e856448e6e58a182c99180e0e9783ae.tar.gz
perlweeklychallenge-club-84f502b31e856448e6e58a182c99180e0e9783ae.tar.bz2
perlweeklychallenge-club-84f502b31e856448e6e58a182c99180e0e9783ae.zip
sgreen solutions to challenge 273
-rw-r--r--challenge-273/sgreen/README.md2
-rw-r--r--challenge-273/sgreen/blog.txt1
-rwxr-xr-xchallenge-273/sgreen/perl/ch-1.pl21
-rwxr-xr-xchallenge-273/sgreen/perl/ch-2.pl13
-rwxr-xr-xchallenge-273/sgreen/python/ch-1.py27
-rwxr-xr-xchallenge-273/sgreen/python/ch-2.py29
-rwxr-xr-xchallenge-273/sgreen/python/test.py25
7 files changed, 117 insertions, 1 deletions
diff --git a/challenge-273/sgreen/README.md b/challenge-273/sgreen/README.md
index 553de4d128..e0dd42e4d4 100644
--- a/challenge-273/sgreen/README.md
+++ b/challenge-273/sgreen/README.md
@@ -1,3 +1,3 @@
# The Weekly Challenge 272
-Sorry, no blog this week.
+Blog: [Finding things](https://dev.to/simongreennet/finding-things-1f34)
diff --git a/challenge-273/sgreen/blog.txt b/challenge-273/sgreen/blog.txt
new file mode 100644
index 0000000000..50e547b719
--- /dev/null
+++ b/challenge-273/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/finding-things-1f34 \ No newline at end of file
diff --git a/challenge-273/sgreen/perl/ch-1.pl b/challenge-273/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..5105c5a587
--- /dev/null
+++ b/challenge-273/sgreen/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ( $str, $char ) {
+ # Count the occurrences of the character in the string
+ my $occurrences = 0;
+ for my $pos ( 0 .. length($str) - 1 ) {
+ if ( substr( $str, $pos, 1 ) eq $char ) {
+ $occurrences++;
+ }
+ }
+
+ # Display the percentage of occurrences
+ say int( $occurrences / length($str) * 100 + 0.5 );
+}
+
+main( $ARGV[0], $ARGV[1] ); \ No newline at end of file
diff --git a/challenge-273/sgreen/perl/ch-2.pl b/challenge-273/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..59a0624a8f
--- /dev/null
+++ b/challenge-273/sgreen/perl/ch-2.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main ($str) {
+ # Check that no 'a' is after the first 'b'
+ say $str =~ /^[^b]*b[^a]*$/ ? 'true' : 'false';
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-273/sgreen/python/ch-1.py b/challenge-273/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..98188f53ab
--- /dev/null
+++ b/challenge-273/sgreen/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+from math import floor
+import sys
+
+
+def char_percentage(s: str, char: str) -> int:
+ """
+ Calculates the percentage of a given character in a string.
+
+ Args:
+ s (str): The input string.
+ char (str): The character to be counted.
+
+ Returns:
+ int: The percentage of the character in the string.
+ """
+ return floor(s.count(char) / len(s) * 100 + 0.5)
+
+
+def main():
+ result = char_percentage(sys.argv[1], sys.argv[2])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-273/sgreen/python/ch-2.py b/challenge-273/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..8bf220971b
--- /dev/null
+++ b/challenge-273/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+import re
+
+
+def b_after_a(s: str) -> bool:
+ """
+ Checks if the string 's' contains the letter 'a' does not appear after a 'b'.
+
+ Args:
+ s (str): The input string to be checked.
+
+ Returns:
+ bool: True if the criteria is met, False otherwise.
+ """
+ return re.search(r'^[^b]*b[^a]*$', s) is not None
+
+
+def main():
+ result = b_after_a(sys.argv[1])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-273/sgreen/python/test.py b/challenge-273/sgreen/python/test.py
new file mode 100755
index 0000000000..8a25515973
--- /dev/null
+++ b/challenge-273/sgreen/python/test.py
@@ -0,0 +1,25 @@
+#!/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.char_percentage('perl', 'e'), 25)
+ self.assertEqual(ch_1.char_percentage('java', 'a'), 50)
+ self.assertEqual(ch_1.char_percentage('python', 'm'), 0)
+ self.assertEqual(ch_1.char_percentage('ada', 'a'), 67)
+ self.assertEqual(ch_1.char_percentage('ballerina', 'l'), 22)
+ self.assertEqual(ch_1.char_percentage('analitik', 'k'), 13)
+
+ def test_ch_2(self):
+ self.assertTrue(ch_2.b_after_a('aabb'))
+ self.assertFalse(ch_2.b_after_a('abab'))
+ self.assertFalse(ch_2.b_after_a('aaa'))
+ self.assertTrue(ch_2.b_after_a('bbb'))
+
+
+if __name__ == '__main__':
+ unittest.main()