aboutsummaryrefslogtreecommitdiff
path: root/challenge-322/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-322/sgreen/python')
-rwxr-xr-xchallenge-322/sgreen/python/ch-1.py32
-rwxr-xr-xchallenge-322/sgreen/python/ch-2.py30
-rwxr-xr-xchallenge-322/sgreen/python/test.py22
3 files changed, 84 insertions, 0 deletions
diff --git a/challenge-322/sgreen/python/ch-1.py b/challenge-322/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..7d58247b47
--- /dev/null
+++ b/challenge-322/sgreen/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def string_format(input_string: str, size: int) -> str:
+ # Remove existing dashes
+ input_string = input_string.replace("-", "")
+
+ # The start value is the position of the first characters, so the remaining
+ # characters are a multiple of 'size'
+ start = len(input_string) % size
+ parts = []
+
+
+ if start:
+ # The first group will be smaller than the rest
+ parts.append(input_string[0:start])
+
+ for pos in range(start, len(input_string), size):
+ # Group the remain characters by size.
+ parts.append(input_string[pos:pos+size])
+
+ return '-'.join(parts)
+
+def main():
+ result = string_format(sys.argv[1], int(sys.argv[2]))
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-322/sgreen/python/ch-2.py b/challenge-322/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..152fcf9e6f
--- /dev/null
+++ b/challenge-322/sgreen/python/ch-2.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def rank_array(ints: list) -> list:
+ """Return an array of the ranks of each element in a list
+
+ Args:
+ ints (list): The input list
+
+ Returns:
+ list: The position (1-based) of each integer
+ """
+ # Generate a list of the sorted unique values.
+ sorted_list = sorted(set(ints))
+
+ # Return the position (1-based) of the each value.
+ return [sorted_list.index(i)+1 for i in ints]
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = rank_array(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-322/sgreen/python/test.py b/challenge-322/sgreen/python/test.py
new file mode 100755
index 0000000000..a8277d6345
--- /dev/null
+++ b/challenge-322/sgreen/python/test.py
@@ -0,0 +1,22 @@
+#!/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.string_format("ABC-D-E-F", 3), "ABC-DEF")
+ self.assertEqual(ch_1.string_format("A-BC-D-E", 2), "A-BC-DE")
+ self.assertEqual(ch_1.string_format("A-B-CD-E", 4), "A-BCDE")
+ self.assertEqual(ch_1.string_format("AB-C-D-EF", 2), "AB-CD-EF")
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.rank_array([55, 22, 44, 33]), [4, 1, 3, 2])
+ self.assertEqual(ch_2.rank_array([10, 10, 10]), [1, 1, 1])
+ self.assertEqual(ch_2.rank_array([5, 1, 1, 4, 3]), [4, 1, 1, 3, 2])
+
+
+if __name__ == "__main__":
+ unittest.main()