Reverse Words in a Sentence Practice Problem
This data science coding problem helps you practice String Manipulation, reverse words in a sentence, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of String Manipulation.
- Problem ID: 100
- Problem key: 100-reverse-words-in-a-sentence
- URL: https://datacrack.app/solve/100-reverse-words-in-a-sentence
- Difficulty: easy
- Topic: String Manipulation
- Module: Python Fundamentals
Problem Statement
# 🧩 Reverse Words in a Sentence
---
### 🎯 Goal
String manipulation with `split()` and `join()` is one of the most frequent interview patterns and a daily tool in NLP preprocessing. This problem teaches you to decompose and reconstruct strings — the foundation of text tokenization.
---
### 🔍 The Task
Reverse the **order of words** (not characters) in a sentence:
```
"the quick brown fox" → "fox brown quick the"
```
---
### 💻 Task
Implement `reverse_words(sentence)` using `split()`, slicing, and `join()`.
---
### 📥 Input
- `sentence`: a string of space-separated words
### 📤 Output
- A string with the words in reverse order, joined by single spaces
---
### 🧩 Starter Code
```python
def reverse_words(sentence):
"""
Reverse the order of words in a sentence.
Args:
sentence (str): Space-separated words
Returns:
str: Words in reversed order
"""
# 🧠 TODO: Split the sentence into a list of words
# 🧠 TODO: Reverse the list
# 🧠 TODO: Join back into a single string with spaces
pass
```
---
### 💡 Example
```python
reverse_words("the quick brown fox")
# Expected: "fox brown quick the"
```
---
### 🔑 Key Concepts
- `str.split()` — splits on whitespace, returns a list
- `list[::-1]` — reverse a list using slice notation
- `" ".join(list)` — join list elements with a space between themStarter Code
def reverse_words(sentence):
"""
Reverse the order of words in a sentence.
Args:
sentence (str): Space-separated words
Returns:
str: Words in reversed order
"""
# 🧠 TODO: Split the sentence into a list of words
# 🧠 TODO: Reverse the list
# 🧠 TODO: Join back into a single string with spaces
pass