+ 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)
3 Antworten
+ 5
Here is the code:
https://code.sololearn.com/cYQ6B5rP85Dv/?ref=app
+ 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)
0
How can this code be written in JavaScript?