Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Backward Each Word by Alex_4444D
import re
def backward_string_by_word(text: str) -> str:
backward = ""
while text != '':
if re.search('\s*\w+', text).group(0).count(' ') != 0:
for i in range(re.search('\s*\w+', text).group(0).count(' ')):
backward += " "
text = text.replace(' ', '', re.search('\s*\w+', text).group(0).count(' '))
backward += re.search('\s*\w+', text).group(0)[::-1]
text = text.replace(re.search('\s*\w+', text).group(0), '', 1)
return backward
if __name__ == '__main__':
print("Example:")
print(backward_string_by_word(''))
print(backward_string_by_word('hello world'))
print(backward_string_by_word('hello world'))
print(backward_string_by_word('welcome to a game'))
# These "asserts" are used for self-checking and not for an auto-testing
assert backward_string_by_word('') == ''
assert backward_string_by_word('world') == 'dlrow'
assert backward_string_by_word('hello world') == 'olleh dlrow'
assert backward_string_by_word('hello world') == 'olleh dlrow'
assert backward_string_by_word('welcome to a game') == 'emoclew ot a emag'
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 16, 2021