diff options
| author | Simon Green <mail@simon.green> | 2025-06-01 22:54:52 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-06-01 22:54:52 +1000 |
| commit | 08dd8bb4e98654546426dfa427943a5670912745 (patch) | |
| tree | 51b7bba76a8d4afc12863ac8600f7b3389ea16ef /challenge-323/sgreen/python/ch-1.py | |
| parent | 0729d1308bfe2e7d4fc1ea6f41b40356645d4f72 (diff) | |
| download | perlweeklychallenge-club-08dd8bb4e98654546426dfa427943a5670912745.tar.gz perlweeklychallenge-club-08dd8bb4e98654546426dfa427943a5670912745.tar.bz2 perlweeklychallenge-club-08dd8bb4e98654546426dfa427943a5670912745.zip | |
sgreen solutions to challenge 323
Diffstat (limited to 'challenge-323/sgreen/python/ch-1.py')
| -rwxr-xr-x | challenge-323/sgreen/python/ch-1.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-323/sgreen/python/ch-1.py b/challenge-323/sgreen/python/ch-1.py new file mode 100755 index 0000000000..1932147849 --- /dev/null +++ b/challenge-323/sgreen/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import sys + + +def increment_decrement(operations: list) -> int: + """ + This function takes a list of strings representing increment and decrement operations + on a variable `x` initialized to 0. It returns the final value of `x` after applying + all operations in the list. + :param operations: List of strings, each representing an operation on `x` + :return: Final value of `x` after all operations + """ + + counter = 0 + for operation in operations: + if operation == "x++" or operation == "++x": + counter += 1 + elif operation == "x--" or operation == "--x": + counter -= 1 + else: + raise ValueError(f"Unknown operation: {operation}") + + return counter + + +def main(): + result = increment_decrement(sys.argv[1:]) + print(result) + + +if __name__ == '__main__': + main() |
