Power Function Practice Problem
This data science coding problem helps you practice Functions & Scope, power function, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Functions & Scope.
- Problem ID: 84
- Problem key: 84-power-function
- URL: https://datacrack.app/solve/84-power-function
- Difficulty: easy
- Topic: Functions & Scope
- Module: Python Fundamentals
Problem Statement
# 🧩 Power Function
---
### 🎯 Goal
Implement exponentiation (raising a base to a power) without using Python's `**` operator or `math.pow()`. This teaches **iterative accumulation** and prepares you for understanding how gradient computation and learning rates scale in machine learning.
$$
\text{base}^{\text{exp}} = \underbrace{\text{base} \times \text{base} \times \cdots \times \text{base}}_{\text{exp times}}
$$
---
### 💻 Task
Implement `power(base, exp)` **iteratively** using a loop (no `**` or `math.pow`).
---
### 📥 Input
- `base`: number — the base value
- `exp`: non-negative integer — the exponent
### 📤 Output
- number — base raised to the power exp
---
### 🧩 Starter Code
```python
def power(base, exp):
"""
Compute base^exp iteratively without using ** or math.pow.
Args:
base (number): The base
exp (int): Non-negative integer exponent
Returns:
number: base raised to the power exp
"""
# 🧠 TODO: Start with result = 1
# 🧠 TODO: Multiply result by base, exp times
pass
```
---
### 💡 Example
```python
power(2, 10) # Expected: 1024
power(3, 4) # Expected: 81
power(5, 0) # Expected: 1 (anything to the power 0 is 1)
power(7, 1) # Expected: 7
```
---
### 🔑 Key Concepts
- Any number to the power 0 is 1 — this falls out naturally if you initialize `result = 1` and loop 0 times
- `range(exp)` loops exactly `exp` timesStarter Code
def power(base, exp):
"""
Compute base^exp iteratively without using ** or math.pow.
Args:
base (number): The base
exp (int): Non-negative integer exponent
Returns:
number: base raised to the power exp
"""
# 🧠 TODO: Start with result = 1
# 🧠 TODO: Multiply result by base, exp times
pass