diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-10 01:06:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-10 01:06:47 +0100 |
| commit | 6f95463abb6dc28410bd5b152886bfb227ebe26c (patch) | |
| tree | f19faeea24512e10225c4aeb87bde278a95cf622 /challenge-272/sgreen/python | |
| parent | 8d10b8c978be0a30822a009728d43812cbc1204e (diff) | |
| parent | f2a469825adcd320c4e2754094fd3f024cd98d8f (diff) | |
| download | perlweeklychallenge-club-6f95463abb6dc28410bd5b152886bfb227ebe26c.tar.gz perlweeklychallenge-club-6f95463abb6dc28410bd5b152886bfb227ebe26c.tar.bz2 perlweeklychallenge-club-6f95463abb6dc28410bd5b152886bfb227ebe26c.zip | |
Merge pull request #10233 from simongreen-net/master
sgreen solutions to challenge 272
Diffstat (limited to 'challenge-272/sgreen/python')
| -rwxr-xr-x | challenge-272/sgreen/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-272/sgreen/python/ch-2.py | 36 | ||||
| -rwxr-xr-x | challenge-272/sgreen/python/test.py | 20 |
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-272/sgreen/python/ch-1.py b/challenge-272/sgreen/python/ch-1.py new file mode 100755 index 0000000000..9e72a22790 --- /dev/null +++ b/challenge-272/sgreen/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys + + +def defang_ip(ip: str) -> str: + """ + Replaces all occurrences of '.' in the given IP address with '[.]'. + + Args: + ip (str): The IP address to defang. + + Returns: + str: The defanged IP address. + + """ + return ip.replace('.', '[.]') + + +def main(): + result = defang_ip(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-272/sgreen/python/ch-2.py b/challenge-272/sgreen/python/ch-2.py new file mode 100755 index 0000000000..2a05a1f811 --- /dev/null +++ b/challenge-272/sgreen/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import sys + + +def string_store(word: str) -> int: + """ + Calculates the score of a given word based on the absolute difference + between ASCII values of consecutive characters. + + Args: + word (str): The input word. + + Returns: + int: The score of the word. + """ + + score = 0 + + # Convert the string to a list of ASCII values + ascii_values = [ord(char) for char in word] + + # Calculate the score + for pos in range(len(ascii_values)-1): + score += abs(ascii_values[pos] - ascii_values[pos+1]) + + return score + + +def main(): + result = string_store(sys.argv[1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-272/sgreen/python/test.py b/challenge-272/sgreen/python/test.py new file mode 100755 index 0000000000..59a19f44c6 --- /dev/null +++ b/challenge-272/sgreen/python/test.py @@ -0,0 +1,20 @@ +#!/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.defang_ip('1.1.1.1'), '1[.]1[.]1[.]1') + self.assertEqual(ch_1.defang_ip('255.101.1.0'), '255[.]101[.]1[.]0') + + def test_ch_2(self): + self.assertEqual(ch_2.string_store('hello'), 13) + self.assertEqual(ch_2.string_store('perl'), 30) + self.assertEqual(ch_2.string_store('raku'), 37) + + +if __name__ == '__main__': + unittest.main() |
