diff options
| author | Simon Green <mail@simon.green> | 2024-06-16 20:43:00 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-06-16 20:43:00 +1000 |
| commit | 84f502b31e856448e6e58a182c99180e0e9783ae (patch) | |
| tree | d48a351b20fe5add5fec2fcdf7f1e159da67ca71 /challenge-273/sgreen/python | |
| parent | 514530d6be7cc5067a95a11ebedff9e0d4d46cfe (diff) | |
| download | perlweeklychallenge-club-84f502b31e856448e6e58a182c99180e0e9783ae.tar.gz perlweeklychallenge-club-84f502b31e856448e6e58a182c99180e0e9783ae.tar.bz2 perlweeklychallenge-club-84f502b31e856448e6e58a182c99180e0e9783ae.zip | |
sgreen solutions to challenge 273
Diffstat (limited to 'challenge-273/sgreen/python')
| -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 |
3 files changed, 81 insertions, 0 deletions
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() |
