DEV Community

Discussion on: Daily Challenge #263 - Reverse Words

Collapse
 
rafaacioly profile image
Rafael Acioly

Well done! It could also be

return ' '.join(word[::-1] for word in sentence.split() if not word.isspace())
Collapse
 
agtoever profile image
agtoever

I think this would fail with doubles spaces in a sentence...

Thread Thread
 
astagi profile image
Andrea Stagi • Edited

That's true :( looks like the right implementation is

def reverse_words(sentence: str) -> str:
    return ' '.join(word[::-1] for word in sentence.split())
Thread Thread
 
rafaacioly profile image
Rafael Acioly

you don't need to use (' ') to split by spaces :)

Thread Thread
 
astagi profile image
Andrea Stagi

That's what happens when you write too much js code 😅 edited thanks!

Collapse
 
astagi profile image
Andrea Stagi

Looks great :)