Created
May 17, 2018 10:50
-
-
Save lttzzlll/7bfcd9ba988c274ba10d4bbcc2a6b82e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
扎眼的 res[len(res] - 1] = res[-1]
return ''.join(reduce(lambda x, y: x + y if x[-1] != y[0] else x, [[i] for i in a]))
return reduce(lambda x, y: x + y if x[-1] != y else x, string[1:], string[0]) if string else ''
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
更简明