Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
lambda solution in Clear category for Backward Each Word by Max0526
backward_string_by_word = lambda text: ' '.join(word[::-1] for word in text.split(' '))
'''
A rewrite of
def backward_string_by_word(text: str) -> str:
result = word = blank = ""
for i in text:
if i == " ":
result += word[::-1]
word = ""
blank += i
else:
result += blank
blank = ""
word += i
return result + word[::-1] + blank
'''
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!")
May 3, 2023
Comments: