+ 1

i need help with this andela lab test, please its urgent

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') => ('aehlstx', 2)

15th Apr 2017, 7:55 AM
Maazi McRoyal Njoku
Maazi McRoyal Njoku - avatar
3 Réponses
15th Apr 2017, 8:04 AM
Shraddha
Shraddha - avatar
+ 1
#defines the function def remove_duplicates(some_str): #creates an array where we will store our characters chars = [] #create a variable where we will store our number of duplicates numOfDuplicates = 0 #goes through all the letters in the string for letter in some_str: #how many letter do we have? startLength = len(some_str) #we add the letter to our types of letters(chars) chars.append(letter) #then we delete all the letters of this type so we don't add them to chars again some_str.replace(letter, "") #how many letter do we have now? newLength = len(some_str) #Now we calculate how many duplicates were deleted and add that number #to the total number of duplicates numOfDuplicates += startLength - newLength + 1 #now we sort the letters chars.sort() #and now we join them final_str = "".join(chars) #And finally!!! print(final_str, numberOfDuplicates)
15th Apr 2017, 9:10 AM
Yur3k
0
How can this code be written in JavaScript?
15th Jun 2018, 1:26 AM
Onyedikachi Ukawuba
Onyedikachi Ukawuba - avatar