⌘K
Flycodx

Reverse Words in a String

Reverse Words in a String

EasyStrings68% accepted

Given a sentence as a single line of text, reverse the order of its words. Words are separated by one or more spaces. The result must have words joined by exactly one space with no leading or trailing spaces. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: "hello world from flycodx" → "flycodx from world hello"

Input format — only if your code reads from stdin

Input format (stdin): - A single line containing the sentence Output: the words in reversed order, separated by single spaces.

Approach

Split the string on whitespace (this handles multiple spaces naturally), reverse the resulting list, then join with a single space.

Input

hello world

Output

world hello
  • •sentence length ≤ 10^4 characters
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3