aboutsummaryrefslogtreecommitdiff
path: root/challenge-323/packy-anderson/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-323/packy-anderson/python/ch-1.py')
-rwxr-xr-xchallenge-323/packy-anderson/python/ch-1.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-323/packy-anderson/python/ch-1.py b/challenge-323/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..5cbccba4aa
--- /dev/null
+++ b/challenge-323/packy-anderson/python/ch-1.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+def incDec(operations):
+ x = 0
+ old_x = 0
+ o = ''
+ explain = []
+ for op in operations:
+ x = x + 1 if "++" in op else x - 1
+ o = '+' if "++" in op else '-'
+ explain.append(
+ 'Operation "{}" => {:2d} {} 1 => {:2d}'.format(
+ op, old_x, o, x
+ )
+ )
+ old_x = x
+ return x, "\n".join(explain)
+
+def solution(operations):
+ display = '"' + '", "'.join(operations) + '"'
+ print(f'Input: @operations = ({display})')
+ output, explain = incDec(operations)
+ print(f'Output: {output}\n\n{explain}')
+
+
+print('Example 1:')
+solution(["--x", "x++", "x++"])
+
+print('\nExample 2:')
+solution(["x++", "++x", "x++"])
+
+print('\nExample 3:')
+solution(["x++", "++x", "--x", "x--"])