diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-18 23:23:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-18 23:23:21 +0100 |
| commit | 36ff7b6646e15c3a76c287ce4d4462b1d251b101 (patch) | |
| tree | 455dae4b742ce019c160038efb79c92d35abed1f /challenge-321/sgreen/python/ch-2.py | |
| parent | 8d1db242e6d28eb45e7fc8963488eee71f687d7b (diff) | |
| parent | 59548f33170863d1b540daa9fed90707a6117a01 (diff) | |
| download | perlweeklychallenge-club-36ff7b6646e15c3a76c287ce4d4462b1d251b101.tar.gz perlweeklychallenge-club-36ff7b6646e15c3a76c287ce4d4462b1d251b101.tar.bz2 perlweeklychallenge-club-36ff7b6646e15c3a76c287ce4d4462b1d251b101.zip | |
Merge pull request #12038 from simongreen-net/master
sgreen solutions to challenge 321
Diffstat (limited to 'challenge-321/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-321/sgreen/python/ch-2.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-321/sgreen/python/ch-2.py b/challenge-321/sgreen/python/ch-2.py new file mode 100755 index 0000000000..81e0d9ed98 --- /dev/null +++ b/challenge-321/sgreen/python/ch-2.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import sys + +def covert_string(s: str) -> str: + """Convert a string by treating '#' as a backspace character + + Args: + s (str): The original string + + Returns: + str: The converted string + """ + + new_string = '' + for c in s: + if c == '#': + # This is safe even if new_string is empty + new_string = new_string[:-1] + else: + new_string += c + + return new_string + +def backspace_compare(str1: str, str2: str) -> bool: + """Compare two strings are the same if '#' is a back space character + + Args: + str1 (str): The first string + str2 (str): The second string + + Returns: + bool: Whether the resulting strings are the same + """ + return covert_string(str1) == covert_string(str2) + + +def main(): + result = backspace_compare(sys.argv[1], sys.argv[2]) + print(result) + + +if __name__ == '__main__': + main() |
