Created
July 22, 2020 18:42
-
-
Save JosepER/55f7a7bcb72c3fc99cacad3e5628a433 to your computer and use it in GitHub Desktop.
Cranky solution
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
import re | |
def backward_string_by_word(text: str) -> str: | |
spaces = re.findall(" +", text) | |
words = text.split() | |
if len(words) == 0: | |
return '' | |
if len(words) == 1: | |
return text[::-1] | |
list_words_reversed = [list(w[::-1]) for w in words] | |
intermediate = [(''.join(list_words_reversed[i]) + (spaces[i])) for i in range(len(spaces))] | |
return ''.join(intermediate) + ''.join(list_words_reversed[-1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment