Multiplication Table Practice Problem
This data science coding problem helps you practice List & Dict Comprehensions, multiplication table, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of List & Dict Comprehensions.
- Problem ID: 93
- Problem key: 93-multiplication-table
- URL: https://datacrack.app/solve/93-multiplication-table
- Difficulty: easy
- Topic: List & Dict Comprehensions
- Module: Python Fundamentals
Problem Statement
# 🧩 Multiplication Table
---
### 🎯 Goal
A multiplication table is a **2D structure** — a list of lists (matrix). Building it with a **nested list comprehension** is one of the most important comprehension patterns, directly applicable to matrix operations in NumPy and feature cross-products in ML.
---
### 🔍 The Structure
For `n = 3`, the multiplication table is:
```
[[1, 2, 3],
[2, 4, 6],
[3, 6, 9]]
```
Row `i`, Column `j` contains `i * j` (1-indexed).
---
### 💻 Task
Implement `multiplication_table(n)` using a **nested list comprehension**.
---
### 📥 Input
- `n`: int — size of the table (n × n)
### 📤 Output
- A list of `n` lists, each of length `n`, where `result[i][j] = (i+1) * (j+1)`
---
### 🧩 Starter Code
```python
def multiplication_table(n):
"""
Build an n x n multiplication table using a nested list comprehension.
Args:
n (int): Table dimension
Returns:
list[list[int]]: n x n multiplication table
"""
# 🧠 TODO: Use a nested comprehension:
# Outer loop: rows (i from 1 to n)
# Inner loop: columns (j from 1 to n)
# Expression: i * j
pass
```
---
### 💡 Example
```python
multiplication_table(3)
# Expected: [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
```
---
### 🔑 Key Concepts
- Nested comprehension: `[[inner for j in ...] for i in ...]`
- The outer `for i` produces rows, the inner `for j` produces columns
- This is the Python equivalent of creating a 2D NumPy arrayStarter Code
def multiplication_table(n):
"""
Build an n x n multiplication table using a nested list comprehension.
Args:
n (int): Table dimension
Returns:
list[list[int]]: n x n multiplication table
"""
# 🧠 TODO: Use a nested comprehension:
# Outer loop: rows (i from 1 to n)
# Inner loop: columns (j from 1 to n)
# Expression: i * j
pass