Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions labs/lab_1/lab_1a.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
6 changes: 6 additions & 0 deletions labs/lab_1/lab_1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 =====")
Expand Down
33 changes: 9 additions & 24 deletions labs/lab_1/lab_1c.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()
return max_global
31 changes: 31 additions & 0 deletions tests/test_1d.py
Original file line number Diff line number Diff line change
@@ -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()
45 changes: 45 additions & 0 deletions tests/tests_1c.py
Original file line number Diff line number Diff line change
@@ -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()