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 Steven2222
def backward_string_by_word(text: str) -> str:
"""Every word in order read backward
In: str
Out: str """
# Split text into list taking into account empty space and empty string
if text == '':
return ''
text_n = []
text_n = text.split(' ')
text_rev = []
for element in text_n:
if element == ' ':
tex_rev.append(' ')
else:
text_rev.append(element[::-1])
# Join list of reversed words and spaces
text_rev = ' '.join(text_rev)
print(text_n)
return text_rev
#text_n = ''.join([word[::-1] for word in text_n])
#print(text_n)
if __name__ == '__main__':
print("Example:")
print(backward_string_by_word(''))
# 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!")
April 16, 2021
Comments: