Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def elim_duplicates(input_string):
- # Simplify what you already had in the first two if statements
- if len(input_string) <= 1:
- return input_string
- # Use elif instead of nested else/if
- elif input_string[0] == input_string[1]:
- # This logic here is to slice up until the letter stops repating
- # We use current_index to keep track of that variable
- current_index = 1
- target_letter = input_string[current_index]
- while target_letter == input_string[0]:
- current_index += 1
- target_letter = input_string[current_index]
- # Return the recursive case where we slice the string until the target_letter stops repeating
- return input_string[0] + elim_duplicates(input_string[current_index:])
- # Else return a recursive case without removing dups
- else:
- return input_string[0] + elim_duplicates(input_string[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement