Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. def elim_duplicates(input_string):
  2.     # Simplify what you already had in the first two if statements
  3.     if len(input_string) <= 1:
  4.         return input_string
  5.    
  6.     # Use elif instead of nested else/if
  7.     elif input_string[0] == input_string[1]:
  8.        
  9.         # This logic here is to slice up until the letter stops repating
  10.         # We use current_index to keep track of that variable
  11.         current_index = 1
  12.         target_letter = input_string[current_index]
  13.         while target_letter == input_string[0]:
  14.             current_index += 1
  15.             target_letter = input_string[current_index]
  16.        
  17.         # Return the recursive case where we slice the string until the target_letter stops repeating
  18.         return input_string[0] + elim_duplicates(input_string[current_index:])
  19.    
  20.     # Else return a recursive case without removing dups
  21.     else:
  22.         return input_string[0] + elim_duplicates(input_string[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement