Programming Assignment
Part 1: Hypotenuse Calculation Using Incremental Development
Introduction
Incremental development is a programming approach where a function is
built in small, manageable stages, with each stage tested before moving to
the next (Downey, 2015). This method helps in debugging by isolating errors
early.
Stage 1: Function Definition and Basic Structure
The first step is defining the function and ensuring it accepts two arguments.
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = num1 / num2
print(f"The result of division is: {result}")
except ZeroDivisionError:
print("Error: Division by zero is not allowed!")
def hypotenuse(a, b):
return 0.0 # Placeholder return value
Test Input: hypotenuse(3, 4)
Output: 0.0
Explanation:
At this stage, the function only returns a placeholder value to verify that the
function structure is correct.
Stage 2: Squaring the Inputs
Next, we compute the squares of the inputs.
def hypotenuse(a, b):
a_sq = a ** 2
b_sq = b ** 2
print(f"Squares: a² = {a_sq}, b² = {b_sq}") # Debugging print
return 0.0
Test Input: hypotenuse(3, 4)
Output:
Squares: a² = 9, b² = 16
0.0
Explanation:
The function now calculates the squares of the inputs and prints them for
verification.
Stage 3: Summing the Squares
Now, we add the squared values.
def hypotenuse(a, b):
a_sq = a ** 2
b_sq = b ** 2
sum_sq = a_sq + b_sq
print(f"Sum of squares: {sum_sq}")
return 0.0
Test Input: hypotenuse(3, 4)
Output:
Sum of squares: 25
0.0
Explanation:
The sum of squares is computed and printed to ensure correctness before
proceeding.
Stage 4: Calculating the Square Root (Final Implementation)
The final step is computing the hypotenuse using the Pythagorean theorem.
import math
def hypotenuse(a, b):
a_sq = a ** 2
b_sq = b ** 2
sum_sq = a_sq + b_sq
result = math.sqrt(sum_sq)
return result
Test Inputs & Outputs:
1. hypotenuse(3, 4) → Output: 5.0
2. hypotenuse(5, 12) → Output: 13.0
3. hypotenuse(8, 15) → Output: 17.0
Explanation:
The function now correctly calculates the hypotenuse using math.sqrt().
Part 2: Custom Function for Portfolio
Function: Calculate Compound Interest
Stage 1: Basic Function Definition
First, define the function with principal, rate, time, and compounding
frequency.
def compound_interest(p, r, t, n):
return 0.0 # Placeholder
Test Input: compound_interest(1000, 0.05, 10, 12)
Output: 0.0
Stage 2: Calculate Rate per Period
Next, compute the rate per compounding period.
def compound_interest(p, r, t, n):
rate_per_period = r / n
print(f"Rate per period: {rate_per_period}")
return 0.0
Test Input: compound_interest(1000, 0.05, 10, 12)
Output:
Rate per period: 0.004166666666666667
0.0
Stage 3: Compute Total Compounding Periods
Now, calculate the total number of compounding periods.
def compound_interest(p, r, t, n):
rate_per_period = r / n
total_periods = n * t
print(f"Total periods: {total_periods}")
return 0.0
Test Input: compound_interest(1000, 0.05, 10, 12)
Output:
Total periods: 120
0.0
Stage 4: Final Calculation (Exponentiation)
The final implementation uses the compound interest formula:
def compound_interest(p, r, t, n):
rate_per_period = r / n
total_periods = n * t
amount = p * (1 + rate_per_period) ** total_periods
return amount
Test Inputs & Outputs:
1. compound_interest(1000, 0.05, 10,
12) → Output: 1647.0094976902813
2. compound_interest(5000, 0.07, 20, 4) → Output: 20080.31077487139
3. compound_interest(200, 0.03, 5, 1) → Output: 231.8543000000001
Explanation:
The function now correctly computes compound interest using the formula:
A=P(1+rn)ntA=P(1+nr)nt
References
Downey, A. (2015). Think Python: How to think like a computer scientist.
Green Tea Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf