remove-duplicates.py 863 B

12345678910111213141516171819202122232425262728293031
  1. # Design an algorithm that removes the duplicate characters in a string
  2. # without using additional buffer.
  3. import pytest
  4. def test_remove_duplicates():
  5. case1 = ''
  6. case2 = 'a'
  7. case3 = 'asdfghjkl'
  8. case4 = 'aasdee'
  9. assert remove_duplicates(case1) == ''
  10. assert remove_duplicates(case2) == 'a'
  11. assert remove_duplicates(case3) == 'asdfghjkl'
  12. assert remove_duplicates(case4) == 'asde'
  13. pytest.main()
  14. def remove_duplicates(input):
  15. len_input = len(input)
  16. if len_input <= 1:
  17. return input
  18. else:
  19. list_input = list(input)
  20. len_non_dup = 1
  21. for j in range(1, len_input):
  22. if list_input[j] in list_input[:len_non_dup]:
  23. pass
  24. else:
  25. list_input[len_non_dup] = input[j]
  26. len_non_dup += 1
  27. return ''.join(list_input[:len_non_dup])