def test_full_length(self): self.assertEqual(max_product_fast([1, 3, 7, 9]), 3 * 7 * 9) self.assertEqual(max_product_fast([-100, -1, 9, 5]), -100 * -1 * 9) self.assertEqual(max_product_fast([100, -1, -1, 0, 5]), 100 * -1 * -1) self.assertEqual(max_product_fast([-100, 1, 5, 0]), 0) self.assertEqual(max_product_fast([1, 2, 3, 4, 5, -1, -2, -3, -4, -5]), -5 * -4 * 5) self.assertEqual(max_product_fast([100, -1, -1, -1, -1, -1, 0, 0, 0]), 100 * -1 * -1) self.assertEqual(max_product_fast([-100, -1, -1, -1, -1, -1, 0, 0, 0]), 0) self.assertEqual(max_product_fast((-100, -1, -1, -1, -1, -1, 0, 0, 0)), 0) self.assertEqual(max_product_fast([-1, -2, -3, -4, -5]), -1 * -2 * -3) self.assertEqual(max_product_fast([0, -1, -2, -3, -4, -5]), 0)
def test_short_length(self): self.assertEqual(max_product_fast([0]), 0) self.assertEqual(max_product_fast([1]), 1) self.assertEqual(max_product_fast((0, )), 0) self.assertEqual(max_product_fast((1, )), 1) self.assertEqual(max_product_fast([-1]), -1) self.assertEqual(max_product_fast([1, 0]), 1 * 0) self.assertEqual(max_product_fast([5, 5]), 5 * 5) self.assertEqual(max_product_fast([-5, 5]), -5 * 5) self.assertEqual(max_product_fast((1, 0)), 1 * 0) self.assertEqual(max_product_fast((5, 5)), 5 * 5) self.assertEqual(max_product_fast((-5, 5)), -5 * 5) self.assertEqual(max_product_fast([-5, 5, 1]), -5 * 5 * 1) self.assertEqual(max_product_fast([5, 5, 1]), 5 * 5 * 1) self.assertEqual(max_product_fast((-5, 5, 1)), -5 * 5 * 1) self.assertEqual(max_product_fast((5, 5, 1)), 5 * 5 * 1)
def test_empty(self): self.assertEqual(max_product_fast(None), None) self.assertEqual(max_product_fast([]), None) self.assertEqual(max_product_fast(()), None)
def test_invalid(self): with self.assertRaises(TypeError): max_product_fast(0) with self.assertRaises(TypeError): max_product_fast({'key': 0})
import time from random import randint from max_product import max_product, max_product_fast arrays = [] for exp in range(1, 8): arrays.append([randint(-100, 100) for _ in range(10**exp)]) # Profile max_product for array in arrays: start = time.time() mp = max_product(array) elapsed = time.time() - start print(f'max_product: {elapsed} for array of length {len(array)}') start = time.time() mp = max_product_fast(array) elapsed = time.time() - start print(f'max_product_fast: {elapsed} for array of length {len(array)}')