aboutsummaryrefslogtreecommitdiff
path: root/challenge-272/sgreen/python
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-06-10 00:29:04 +1000
committerSimon Green <mail@simon.green>2024-06-10 00:29:04 +1000
commitf2a469825adcd320c4e2754094fd3f024cd98d8f (patch)
treefda1f914d5e656be985d5485b1f0c83f5ce0cb2c /challenge-272/sgreen/python
parent6b1c67381886ee30e0dd9f9356340350101139b8 (diff)
downloadperlweeklychallenge-club-f2a469825adcd320c4e2754094fd3f024cd98d8f.tar.gz
perlweeklychallenge-club-f2a469825adcd320c4e2754094fd3f024cd98d8f.tar.bz2
perlweeklychallenge-club-f2a469825adcd320c4e2754094fd3f024cd98d8f.zip
sgreen solutions to challenge 272
Diffstat (limited to 'challenge-272/sgreen/python')
-rwxr-xr-xchallenge-272/sgreen/python/ch-1.py26
-rwxr-xr-xchallenge-272/sgreen/python/ch-2.py36
-rwxr-xr-xchallenge-272/sgreen/python/test.py20
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()