CS 1101 Unit 4
CS 1101 Unit 4
Sarah Hauya
Part 1
In this stage, one needs to set up the project and define the function's signature (name, parameters, and
return type). Here's how it might look in Python:def calculate_hypotenuse(a, b): pass # Placeholder for
the actual implementation.
Before calculating the hypotenuse, it is recommended that one need to validate the input values to
ensure they are valid with accurate lengths for the legs of a right triangle. Thus, this stage needs us to
add input validation code:def calculate_hypotenuse(a, b): if a <= 0 or b <= 0: raise ValueError("Lengths
of legs must be greater than zero") pass # Placeholder for the actual implementation
The next step is to calculate the hypotenuse using the Pythagorean theorem in this stage:import
mathdef calculate_hypotenuse(a, b): if a <= 0 or b <= 0: raise ValueError("Lengths of legs must be
greater than zero") return math.sqrt(a**2 + b**2)
Next, add input validation code to ensure that the input is a non-negative integer:def
calculate_factorial(n): if not isinstance(n, int) or n < 0: raise ValueError("Input must be a non-negative
integer") pass # Placeholder for the actual implementationStage 3: Base Case Handling
Factorial calculation typically involves recursive logic with a base case. Implement the base casewhere
`n` is 0 or 1:def calculate_factorial(n): if not isinstance(n, int) or n < 0: raise ValueError("Input must be a
non-negative integer") if n == 0 or n == 1: return 1 pass # Placeholder for the recursive caseStage 4:
Recursive Case Now, add the recursive logic for calculating the factorial:def calculate_factorial(n): if not
isinstance(n, int) or n < 0: raise ValueError("Input must be a non-negative integer") if n == 0 or n == 1:
return 1 return n * calculate_factorial(n - 1)
Test your function with various inputs and document the results in your ProgrammingAssignment:# Test
cases print(calculate_factorial(0)) # Expected output: 1 print(calculate_factorial(1)) # Expected output: 1
print(calculate_factorial(5)) # Expected output: 120 print(calculate_factorial(10)) # Expected output:
3628800After running these tests, record the actual outputs in your Programming Assignment
andcompare them with the expected results. Make sure to note if any issues or errors occur
duringtesting.
In this final stage, review your code and documentation to ensure everything is in order. Check that all
tests have passed and that your code is well-documented. You can also consider addingcomments and
explanations to clarify the code for potential clients who review your portfolio.By following this
incremental development approach, you'll demonstrate your programmingskills and problem-solving
abilities to potential clients. Your portfolio will serve as evidence of your capability to develop custom
software solutions, which will help you establish yourself as askilled and versatile.