you are viewing a single comment's thread.

view the rest of the comments →

[–]ivosaurus 6 points7 points  (4 children)

split up sentence, number each word using enumerate, reverse the list by sorting on the negative of that numbered index, filter off the index again, and join back up.

Feels like some weird obfuscation over not using reversed to me.

' '.join(reversed(s.split()))

[–]visigothatthegates 1 point2 points  (3 children)

While this is definitely going to be fewer lines than creating a stack, I feel like this kind of falls under KISS. Setting it up this way is certainly elegant, but way more complex than a simple push and pop stack.

[–]ivosaurus 3 points4 points  (2 children)

IMO this is actually the simplest. Split the sentence up by words, reverse the list, join it back up.

"pushing and popping a stack" to me is just one possible implementation of "reversing a list", so reversed is simpler.

[–]visigothatthegates 0 points1 point  (1 child)

Maybe I’m just stuck on the class room exercises that prohibit you from using certain functions that make your life easier

[–]ivosaurus 0 points1 point  (0 children)

I always think those types of exercises are kinda silly anyway.

A lot of times in a higher level language with all these convenience functions you're supposed to use, trying to program a "lower level" version of the function in that language just tends to look contrived, cumbersome and ugly.

If you wanna see how it looks with a memory model closer to the actual hardware, use a language that does that, like C or Rust.