School: University of the People
Moodle ID: C110223808
Class: CS 1101 - Programming Fundamentals
Unit 4: Functions and Return Values
Group: 0036.
Course instructor: Muhammad Anwar Shahid.
Part 1
You work as a software developer in a company that creates custom software solutions for
various clients. Your company has been approached by an educational client who wants to
develop a function that calculates the length of the hypotenuse of a right triangle given the
lengths of the other two legs as arguments. Your manager has instructed you to use incremental
development to create the necessary function and document each stage of the development
process. After completing the final stage of development, you have to test the function with
different arguments and record the outputs in your Learning Journal.
Tasks
Include all of the following in your submission:
o An explanation of each stage of development, including code and any test input
and output.
o The output of hypotenuse(3,4).
o The output of two additional calls to hypotenuse with different arguments.
Understanding the Requirements
I reviewed the problem and identified the use of the Pythagorean Theorem to calculate the
hypotenuse. The function needs to calculate the hypotenuse (ccc) of a right triangle using the
Pythagorean theorem:
c=a2+b2c = \sqrt{a^2 + b^2}c=a2+b2
Where aaa and bbb are the lengths of the two legs.
Planning the Function
1. Define a function named hypotenuse that takes two arguments: a and b.
2. Compute the hypotenuse using the formula above.
3. Return the calculated value.
Implement a Basic Version
I Written the basic function using Python, ensuring it uses mathematical operations to compute
the hypotenuse. We implement a simple version of the function:
Output
Testing and Validating
I decided on function structure, parameters (a and b), and the use of Python's math square root
for the square root calculation. Validated the function with three test cases: (3, 4), (6, 8), and (5,
12). All produced correct outputs, confirming the implementation's correctness.
Test the function with additional inputs to ensure it works for various cases.
Test Case 2:
Output
Test Case 3:
Output
Part 2
You are a software developer who wants to establish yourself as a skilled and versatile
programmer. To achieve this, you have decided to create a work portfolio that showcases your
ability to develop custom software solutions. This portfolio will be your gateway to attract
potential clients and establish yourself as a freelancer.
As part of your portfolio, you plan to create your own function that does some useful
computation using an incremental development approach that will demonstrate your
programming skills and problem-solving abilities. You will document each stage of the
development process, including the code and any test input and output in your Programming
Assignment.
Developing a Custom Function for Portfolio
To showcase programming skills and problem-solving abilities, I have developed a custom
function called find_primes_in_range. This function computes all prime numbers within a
specified range. The project demonstrates an incremental approach to development, focusing on
clarity and optimization. Below, I explain each stage of the development process.
Stage 1: Define the Problem and Plan
The task is to find all prime numbers within a given range. A prime number is a number greater
than 1 that has no divisors other than 1 and itself. The function will:
1. Take two integers as input: the start and end of the range.
2. Validate the input.
3. Compute and return a list of prime numbers within the range.
Stage 2: Initial Implementation
The initial step is creating a function that identifies whether a single number is prime.
Explanation: The is_prime function checks divisors up to the square root of the number for
efficiency. It returns True for primes and False otherwise.
Stage 3: Extend Functionality
Build on is_prime to find all primes within a specified range.
Explanation: The function iterates over the range, checking each number with is_prime. Valid
inputs return a list of primes; invalid ranges return an error message.
Stage 4: Optimize and Finalize
Incorporate optimizations to handle edge cases and improve readability.
Outputs with Explanations
Test Case 1: The primes between 10 and 20 are [11, 13, 17, 19].
Test Case 2: The primes between 1 and 10 are [2, 3, 5, 7]. The function ensures the range
starts at 2 since no numbers below 2 are prime.
Test Case 3: An invalid range produces the error message "Invalid range: start must be
less than or equal to end.".
Conclusion
This project demonstrates problem-solving, input validation, optimization, and clarity in code. It
showcases the ability to handle edge cases and write reusable functions. By documenting each
stage of the development process, potential clients gain insight into the approach and attention to
detail.