diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-01 22:54:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-01 22:54:44 +0100 |
| commit | 72a1c60daab582b75fc1439c852a7c868adc0172 (patch) | |
| tree | 11330f178a2108df26455ca98592d415c28a54c9 /challenge-323/sgreen/python/ch-1.py | |
| parent | 3517294cda226112a6cddb4ab8c76f285694ad83 (diff) | |
| parent | 08dd8bb4e98654546426dfa427943a5670912745 (diff) | |
| download | perlweeklychallenge-club-72a1c60daab582b75fc1439c852a7c868adc0172.tar.gz perlweeklychallenge-club-72a1c60daab582b75fc1439c852a7c868adc0172.tar.bz2 perlweeklychallenge-club-72a1c60daab582b75fc1439c852a7c868adc0172.zip | |
Merge pull request #12111 from simongreen-net/master
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() |
