Stack and Concatenate Arrays Practice Problem
This data science coding problem helps you practice Array Manipulation, stack and concatenate arrays, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Array Manipulation.
- Problem ID: 62
- Problem key: 62-stack-and-concatenate-arrays
- URL: https://datacrack.app/solve/62-stack-and-concatenate-arrays
- Difficulty: easy
- Topic: Array Manipulation
- Module: NumPy Foundations
Problem Statement
# 🧩 Stack and Concatenate Arrays
---
### 🎯 Goal
Combining arrays is a daily task in data science: adding new features to a dataset, stacking batches from different sources, combining train and validation splits. This problem teaches the four most important NumPy combining functions.
---
### 🔍 The Four Functions
Here:
- `n` = number of rows or values in each array
- `m` = number of columns
- `k` = number of arrays being combined
Example: if you combine `k = 3` arrays, each with shape `(2, 4)`, then `n = 2` and `m = 4`.
| Function | Description | Example input shapes | Example output shape |
|:---------|:------------|:---------------------|:---------------------|
| `np.vstack(arrays)` | Stack arrays vertically as rows | `(n, m)` each | `(k*n, m)` |
| `np.hstack(arrays)` on 1D | Stack arrays horizontally into one longer flat array | `(n,)` each | `(k*n,)` |
| `np.hstack(arrays)` on 2D | Stack arrays horizontally into more columns | `(n, m)` each | `(n, k*m)` |
| `np.column_stack(arrays)` | Stack 1D arrays as columns | `(n,)` each | `(n, k)` |
| `np.concatenate(arrays, axis=0)` | Concatenate vertically, adding more rows | `(n, m)` each | `(k*n, m)` |
| `np.concatenate(arrays, axis=1)` | Concatenate horizontally, adding more columns | `(n, m)` each | `(n, k*m)` |
---
### 💻 Task
Implement `stack_arrays(arrays, operation)`.
---
### 📥 Input
- `arrays`: list of lists
- `operation`: string — `"vstack"`, `"hstack"`, `"column_stack"`, `"concatenate_axis0"`, `"concatenate_axis1"`
### 📤 Output
- Combined array as a nested Python list
---
### 🧩 Starter Code
```python
import numpy as np
def stack_arrays(arrays, operation):
"""
Combine multiple arrays using the specified stacking operation.
Args:
arrays (list[list]): List of arrays to combine
operation (str): "vstack" | "hstack" | "column_stack" | "concatenate_axis0" | "concatenate_axis1"
Returns:
list: Combined array as a Python list
"""
np_arrays = [np.array(a) for a in arrays]
# 🧠 TODO:
pass
```
---
### 🔑 Key Concepts
- `vstack` → adds rows (like appending more data points to a dataset)
- `column_stack` → adds columns (like adding a new feature to a dataset)
- `hstack` behaves differently depending on shape: for 1D arrays it creates one longer flat array, but for 2D arrays it adds columns.Starter Code
import numpy as np
def stack_arrays(arrays, operation):
"""
Combine multiple arrays using the specified stacking operation.
Args:
arrays (list[list]): List of arrays to combine
operation (str): "vstack" | "hstack" | "column_stack" | "concatenate_axis0" | "concatenate_axis1"
Returns:
list: Combined array as a Python list
"""
np_arrays = [np.array(a) for a in arrays]
# 🧠 TODO:
pass