diff options
| -rw-r--r-- | challenge-273/sgreen/README.md | 2 | ||||
| -rw-r--r-- | challenge-273/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-273/sgreen/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-273/sgreen/perl/ch-2.pl | 13 | ||||
| -rwxr-xr-x | challenge-273/sgreen/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-273/sgreen/python/ch-2.py | 29 | ||||
| -rwxr-xr-x | challenge-273/sgreen/python/test.py | 25 |
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() |
