Skip to content

Instantly share code, notes, and snippets.

@lttzzlll
Created May 17, 2018 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lttzzlll/7bfcd9ba988c274ba10d4bbcc2a6b82e to your computer and use it in GitHub Desktop.
Save lttzzlll/7bfcd9ba988c274ba10d4bbcc2a6b82e to your computer and use it in GitHub Desktop.
def delete_reoccurring_chars(string):
"""Return the string after deduplication.
>>> delete_reoccurring_chars('metasotaaaa')
'metasota'
>>> delete_reoccurring_chars('mmeettaassoottaa')
'metasota'
>>> delete_reoccurring_chars('metasota')
'metasota'
>>> delete_reoccurring_chars('')
''
"""
# your codes here
if not string:
return ''
output = list(string)
res = [output[0]]
for i in range(1, len(output)):
if res[len(res) - 1] != output[i]:
res.append(output[i])
return ''.join(res)
if __name__ == '__main__':
import doctest
doctest.testmod()
@lttzzlll
Copy link
Author

更简明

@lttzzlll
Copy link
Author

扎眼的 res[len(res] - 1] = res[-1]

@lttzzlll
Copy link
Author

return ''.join(reduce(lambda x, y: x + y if x[-1] != y[0] else x,  [[i] for i in a]))

@lttzzlll
Copy link
Author

return reduce(lambda x, y: x + y if x[-1] != y else x, string[1:], string[0]) if string else ''

@lttzzlll
Copy link
Author

return reduce(lambda x, y: x + y if x and x[-1] != y else x, string) if string else ''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment