Skip to content

Instantly share code, notes, and snippets.

@JosepER
Created July 22, 2020 18:42
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 JosepER/55f7a7bcb72c3fc99cacad3e5628a433 to your computer and use it in GitHub Desktop.
Save JosepER/55f7a7bcb72c3fc99cacad3e5628a433 to your computer and use it in GitHub Desktop.
Cranky solution
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