diff --git a/.github/workflows/run_test.yml b/.github/workflows/run_test.yml new file mode 100644 index 00000000..6c1b8047 --- /dev/null +++ b/.github/workflows/run_test.yml @@ -0,0 +1,35 @@ +name: simple_calculator unit test + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with Ruff + run: | + pip install ruff + ruff --format=github --target-version=py310 . + continue-on-error: true + - name: Test with pytest + run: | + coverage run -m pytest tests/tests_1b.py -v -s + - name: Test with pytest + run: | + coverage run -m pytest tests/tests_1c.py -v -s + - name: Generate Coverage Report + run: | + coverage report -m \ No newline at end of file diff --git a/labs/lab_1/lab_1a.py b/labs/lab_1/lab_1a.py index 9d15ec83..37147a45 100644 --- a/labs/lab_1/lab_1a.py +++ b/labs/lab_1/lab_1a.py @@ -1,16 +1,24 @@ """ lab_1a.py +This is to simulate a change made on a robot: robot_speed = 8 # m/s + + + The first lab in the BWSI CSS course. To complete this lab, fill out the variable on line 10 with your name. Then, save the code, add it to the staging area, and commit it to the Git tree. + + +This is to simulate a change made on a robot: robot_speed = 3 # m/s """ def main(): print("Hello World!") - name = "" # TODO: Insert your name between the double quotes + name = "Juha Lee" # TODO: Insert your name between the double quotes print(f"{name}, Welcome to the CSS course!") + print("Hi, I'm Juha and I'm a junior from New Jersey.") if __name__ == "__main__": main() diff --git a/labs/lab_1/lab_1b.py b/labs/lab_1/lab_1b.py index e58dd957..e710178b 100644 --- a/labs/lab_1/lab_1b.py +++ b/labs/lab_1/lab_1b.py @@ -37,19 +37,33 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float: else: raise ValueError("Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.") -def main(): +def request_sanitized_number(prompt: str) -> float: + """ + Function to request a sanitized number for the operation - print(f"===== Simple Calculator =====") + Returns: + float: The sanitized numeric input by the user + """ + while True: + try: + number = float(input(prompt)) + return number + except ValueError: + print("Invalid input. Please enter a valid number.") + +def main(): + print("===== Simple Calculator =====") - # Ask the user for sample input - num1 = float(input("Enter the first number: ")) - num2 = float(input("Enter the second number: ")) + # Ask the user for sample input + num1 = request_sanitized_number("Enter the first number: ") + num2 = request_sanitized_number("Enter the second number: ") operation = input("Enter the operation (add, subtract, multiply, divide): ").strip().lower() - # Perform the calculation and display the result - result = simple_calculator(operation, num1, num2) - print(f"The result of {operation}ing {num1} and {num2} is: {result}") - + try: + result = simple_calculator(operation, num1, num2) + print(f"The result of {operation}ing {num1} and {num2} is: {result}") + except ValueError as e: + print(f"Error: {e}") if __name__ == "__main__": main() diff --git a/labs/lab_1/lab_1c.py b/labs/lab_1/lab_1c.py index 40cf98db..b1495473 100644 --- a/labs/lab_1/lab_1c.py +++ b/labs/lab_1/lab_1c.py @@ -22,9 +22,9 @@ def max_subarray_sum(nums: list[int]) -> int: max_current = max_global = nums[0] - for num in nums: + for num in nums[1:]: max_current = max(num, max_current + num) - if max_current < max_global: + if max_current > max_global: max_global = max_current return max_global diff --git a/tests/tests_1c.py b/tests/tests_1c.py new file mode 100644 index 00000000..f289608b --- /dev/null +++ b/tests/tests_1c.py @@ -0,0 +1,32 @@ +""" +test_1c.py + +This module contains unit tests for the maximum subarray sum function defined in lab_1c.py. +""" + +import pytest +from labs.lab_1.lab_1c import max_subarray_sum + +def test_example(): + assert max_subarray_sum([-2,1,-3,4,-1,2,1,-5,4]) == 6 # test given example + +def test_single_positive(): + assert max_subarray_sum([3]) == 3 # test a single positive number + +def test_single_negative(): + assert max_subarray_sum([-5]) == -5 # test a single negative number + +def test_all_positive(): + assert max_subarray_sum([1, 5, 7, 2]) == 15 # test a list of all positives + +def test_all_negative(): + assert max_subarray_sum([-3, -2, -7]) == -2 # test a list of all negatives + +def test_with_zero(): + assert max_subarray_sum([-2, -5, 0]) == 0 # test a zero in the list + +def test_all_zeroes(): + assert max_subarray_sum([0, 0, 0]) == 0 # test a list of all zeroes + +if __name__ == "__main__": + pytest.main() \ No newline at end of file