diff --git a/labs/lab_1/lab_1a.py b/labs/lab_1/lab_1a.py index 9d15ec83..128931f7 100644 --- a/labs/lab_1/lab_1a.py +++ b/labs/lab_1/lab_1a.py @@ -1,4 +1,4 @@ -""" +##"This is to simulate a change made on a robot: robot_speed = 8# m/s"" lab_1a.py The first lab in the BWSI CSS course. To complete this lab, fill out the variable on line 10 @@ -8,9 +8,10 @@ def main(): print("Hello World!") - name = "" # TODO: Insert your name between the double quotes + name = "Oluj" # TODO: Insert your name between the double quotes print(f"{name}, Welcome to the CSS course!") + print("This is my personal introduction from the beginning of the course!") if __name__ == "__main__": main() diff --git a/labs/lab_1/lab_1b.py b/labs/lab_1/lab_1b.py index e58dd957..bcaefad9 100644 --- a/labs/lab_1/lab_1b.py +++ b/labs/lab_1/lab_1b.py @@ -37,6 +37,12 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float: else: raise ValueError("Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.") +def request_sanitized_number(prompt: str) -> float: + while True: + try: + return float(input(prompt)) + except ValueError: + print("Invalid input. Please enter a valid number.") def main(): print(f"===== Simple Calculator =====") diff --git a/labs/lab_1/lab_1c.py b/labs/lab_1/lab_1c.py index 40cf98db..f2853bf7 100644 --- a/labs/lab_1/lab_1c.py +++ b/labs/lab_1/lab_1c.py @@ -1,14 +1,3 @@ -""" -lab_1c.py - -Given a list of numbers, return the maximum sum of any contiguous subarray of the list. - -Do not assume anything. Account for all edge cases. - -Derived from LeetCode problem: https://leetcode.com/problems/maximum-subarray/ (leetcode medium) -""" - -# TODO: Find and resolve the bug in the following implementation. Create unit tests to verify your fix. def max_subarray_sum(nums: list[int]) -> int: """ Function that takes in a list of integers and returns the maximum sum of any contiguous subarray. @@ -18,22 +7,18 @@ def max_subarray_sum(nums: list[int]) -> int: Returns: int: The maximum sum of any contiguous subarray. + + Raises: + ValueError: If nums is empty. """ + if not nums: + raise ValueError("nums must not be empty") 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 - -# Example usage: -def main(): - nums = [-2,1,-3,4,-1,2,1,-5,4] - result = max_subarray_sum(nums) - print(f"Maximum subarray sum: {result}") -if __name__ == "__main__": - main() \ No newline at end of file + return max_global \ No newline at end of file diff --git a/tests/test_1d.py b/tests/test_1d.py new file mode 100644 index 00000000..3cd2ba1c --- /dev/null +++ b/tests/test_1d.py @@ -0,0 +1,31 @@ +import unittest + +def two_sum(nums: list[int], target: int) -> list[int]: + num_to_index = {} + for index, num in enumerate(nums): + complement = target - num + if complement in num_to_index: + return [num_to_index[complement], index] + num_to_index[num] = index + return [] + + +class TestTwoSum(unittest.TestCase): + def test_basic_case(self): + self.assertEqual(two_sum([2, 7, 11, 15], 9), [0, 1]) + + def test_other_case(self): + self.assertEqual(two_sum([3, 2, 4], 6), [1, 2]) + + def test_duplicate_values(self): + self.assertEqual(two_sum([3, 3], 6), [0, 1]) + + def test_negative_numbers(self): + self.assertEqual(two_sum([-1, -2, -3, -4, -5], -8), [2, 4]) + + def test_no_solution_fallback(self): + self.assertEqual(two_sum([1, 2, 3], 100), []) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/tests/tests_1c.py b/tests/tests_1c.py new file mode 100644 index 00000000..94e9f59b --- /dev/null +++ b/tests/tests_1c.py @@ -0,0 +1,45 @@ +import unittest + +def max_subarray_sum(nums: list[int]) -> int: + if not nums: + raise ValueError("nums must not be empty") + + max_current = max_global = nums[0] + + for num in nums[1:]: + max_current = max(num, max_current + num) + if max_current > max_global: + max_global = max_current + + return max_global + + +class TestMaxSubarraySum(unittest.TestCase): + def test_example_case(self): + self.assertEqual(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]), 6) + + def test_single_positive(self): + self.assertEqual(max_subarray_sum([5]), 5) + + def test_single_negative(self): + self.assertEqual(max_subarray_sum([-5]), -5) + + def test_all_negative(self): + self.assertEqual(max_subarray_sum([-8, -3, -6, -2, -5, -4]), -2) + + def test_all_positive(self): + self.assertEqual(max_subarray_sum([1, 2, 3, 4]), 10) + + def test_mixed_values(self): + self.assertEqual(max_subarray_sum([5, -2, 3, 4]), 10) + + def test_contains_zeroes(self): + self.assertEqual(max_subarray_sum([0, 0, 0]), 0) + + def test_empty_list_raises(self): + with self.assertRaises(ValueError): + max_subarray_sum([]) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file