diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-09-22 12:45:35 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-09-22 12:45:35 +0200 |
| commit | fbf2a1b7a246de38ca0a2e74d3bb6982d2984120 (patch) | |
| tree | eb1f234e045432e87feacbbfebf006d59f7107b1 | |
| parent | c415df80f8ee986bce4bbcb1533306841ad3cd7f (diff) | |
| download | perlweeklychallenge-club-fbf2a1b7a246de38ca0a2e74d3bb6982d2984120.tar.gz perlweeklychallenge-club-fbf2a1b7a246de38ca0a2e74d3bb6982d2984120.tar.bz2 perlweeklychallenge-club-fbf2a1b7a246de38ca0a2e74d3bb6982d2984120.zip | |
Add ch-1 in Python
| -rw-r--r-- | challenge-340/spadacciniweb/python/ch-1.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/challenge-340/spadacciniweb/python/ch-1.py b/challenge-340/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..a6bcad20be --- /dev/null +++ b/challenge-340/spadacciniweb/python/ch-1.py @@ -0,0 +1,74 @@ +# Task 1: Duplicate Removals +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, consisting of lowercase English letters. +# +# Write a script to return the final string after all duplicate removals have been made. Repeat duplicate removals on the given string until we no longer can. +# +# A duplicate removal consists of choosing two adjacent and equal letters and removing them. +# +# Example 1 +# Input: $str = 'abbaca' +# Output: 'ca' +# +# Step 1: Remove 'bb' => 'aaca' +# Step 2: Remove 'aa' => 'ca' +# +# Example 2 +# Input: $str = 'azxxzy' +# Output: 'ay' +# +# Step 1: Remove 'xx' => 'azzy' +# Step 2: Remove 'zz' => 'ay' +# +# Example 3 +# Input: $str = 'aaaaaaaa' +# Output: '' +# +# Step 1: Remove 'aa' => 'aaaaaa' +# Step 2: Remove 'aa' => 'aaaa' +# Step 3: Remove 'aa' => 'aa' +# Step 4: Remove 'aa' => '' +# +# Example 4 +# Input: $str = 'aabccba' +# Output: 'a' +# +# Step 1: Remove 'aa' => 'bccba' +# Step 2: Remove 'cc' => 'bba' +# Step 3: Remove 'bb' => 'a' +# +# Example 5 +# Input: $str = 'abcddcba' +# Output: '' +# +# Step 1: Remove 'dd' => 'abccba' +# Step 2: Remove 'cc' => 'abba' +# Step 3: Remove 'bb' => 'aa' +# Step 4: Remove 'aa' => '' + +import re + +def get_noduplicate(str): + str_orig = str + + while re.search(r'([a-z])\1', str): + str = re.sub(r'([a-z])\1', r'', str) + + print("'%s' -> '%s'" % ( str_orig, str ) ) + +if __name__ == "__main__": + str = 'abbaca' + get_noduplicate(str) + + str = 'azxxzy' + get_noduplicate(str) + + str = 'aaaaaaaa' + get_noduplicate(str) + + str = 'aabccba' + get_noduplicate(str) + + str = 'abcddcba' + get_noduplicate(str) |
