+ 2

Data structures

Write a function called remove_duplicates which will take one argument called string. This string input will only have characters between a-z. The function should remove all repeated characters in the string and return a tuple with two values: A new string with only unique, sorted characters. The total number of duplicates dropped. For example: remove_duplicates('aaabbbac') => ('abc', 5) remove_duplicates('a') => ('a', 0) remove_duplicates('thelexash') =>

11th Oct 2017, 9:06 PM
Franklin Yaw Frimpong
Franklin Yaw Frimpong - avatar
1 Answer
+ 1
def remove_duplicates(string): list = {} duplicates = 0 for char in string: if char in list: list[char] = list.get(char) + 1 duplicates = duplicates + 1 else: list[char] = 0 print('(\'' + ''.join(list.keys()) + '\', ' + str(duplicates) + ')')
11th Oct 2017, 10:27 PM
Muhammad Kamal
Muhammad Kamal - avatar