aboutsummaryrefslogtreecommitdiff
path: root/challenge-322/packy-anderson/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-322/packy-anderson/python')
-rwxr-xr-xchallenge-322/packy-anderson/python/ch-1.py27
-rwxr-xr-xchallenge-322/packy-anderson/python/ch-2.py27
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-322/packy-anderson/python/ch-1.py b/challenge-322/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..a42c02c26a
--- /dev/null
+++ b/challenge-322/packy-anderson/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+def strFormat(strVar, i):
+ strVar = strVar.replace("-", "")
+ output = ''
+ while (len(strVar) > i):
+ output = "-" + strVar[-i:] + output
+ strVar = strVar[0:-i] # Python strings are IMMUTABLE
+ return strVar + output
+
+def solution(strVar, i):
+ print(f'Input: $str = "{strVar}", $i = {i}')
+ output = strFormat(strVar, i)
+ print(f'Output: "{output}"')
+
+
+print('Example 1:')
+solution("ABC-D-E-F", 3)
+
+print('\nExample 2:')
+solution("A-BC-D-E", 2)
+
+print('\nExample 3:')
+solution("-A-B-CD-E", 4)
+
+print('\nExample 4:')
+solution("-A-B-CD-E", 5) \ No newline at end of file
diff --git a/challenge-322/packy-anderson/python/ch-2.py b/challenge-322/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..e0e0157f94
--- /dev/null
+++ b/challenge-322/packy-anderson/python/ch-2.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+def rankArray(ints):
+ rankInt = 0
+ rankDict = {}
+ for i in sorted(ints):
+ if i in rankDict: continue
+ rankInt += 1
+ rankDict[i] = rankInt
+ return [ rankDict[i] for i in ints ]
+
+def int_join(joiner, arr):
+ return joiner.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @ints = ({int_join(", ", ints)})')
+ print(f'Output: ({int_join(", ", rankArray(ints))})')
+
+
+print('Example 1:')
+solution([55, 22, 44, 33])
+
+print('\nExample 2:')
+solution([10, 10, 10])
+
+print('\nExample 3:')
+solution([5, 1, 1, 4, 3])