def num_buses(n): """ (int) -> int Precondition: n >= 0 Return the minimum number of buses required to transport n people. Each bus can hold 50 people. >>> num_buses(75) 2 """ if not (type(n) is int): raise ValueError('n must be int') if n < 0: raise ValueError('n must be >= 0') if (n % 50 == 0): return n/50 else: return n/50 + 1 def stock_price_summary(price_changes): """ (list of number) -> (number, number) tuple price_changes contains a list of stock price changes. Return a 2-item tuple where the first item is the sum of the gains in price_changes and the second is the sum of the losses in price_changes. >>> stock_price_summary([0.01, 0.03, -0.02, -0.14, 0, 0, 0.10, -0.01]) (0.14, -0.17) """ gains = 0 losses = 0 for price_change in price_changes: if price_change > 0: gains = gains + price_change else: losses = losses + price_change return (gains, losses) def swap_k(L, k): """ (list, int) -> NoneType Precondtion: 0 <= k <= len(L) // 2 Swap the first k items of L with the last k items of L. >>> nums = [1, 2, 3, 4, 5, 6] >>> swap_k(nums, 2) >>> nums [5, 6, 3, 4, 1, 2] """ if k < 0 or k> len(L) // 2: raise ValueError('k must be 0 <= k <= len(L) // 2') for i in range(k): temp = L[i] L[i] = L[len(L) - k + i] L[len(L) - k + i] = temp if __name__ == '__main__': import doctest doctest.testmod()
import a1 import unittest class TestNumBuses(unittest.TestCase): """ Test class for function a1.num_buses. """ # Add your test methods for a1.num_buses here. def test_num_buses_with_regular_integer(self): """Test num_buses with regular integer.""" actual = a1.num_buses(75) expected = 2 self.assertEqual(expected, actual) def test_num_buses_with_integer_50(self): """Test num_buses with 50. The function should return 1.""" actual = a1.num_buses(50) expected = 1 self.assertEqual(expected, actual) def test_num_buses_with_zero(self): """Test num_buses with 0. The function should return 0.""" actual = a1.num_buses(0) expected = 0 self.assertEqual(expected, actual) def test_num_buses_with_negative_integer(self): """Test num_buses with negative integer. A error should be raised.""" self.assertRaises(ValueError, a1.num_buses, -1) def test_num_buses_with_non_integer(self): """Test num_buses with non integer. A error should be raised.""" self.assertRaises(ValueError, a1.num_buses, 1.1) if __name__ == '__main__': unittest.main(exit=False)
import a1 import unittest class TestStockPriceSummary(unittest.TestCase): """ Test class for function a1.stock_price_summary. """ # Add your test methods for a1.stock_price_summary here. def test_stock_price_summary_1(self): """Test stock_price_summary with regular integer list.""" actual = a1.stock_price_summary([0.01, 0.03, -0.02, -0.14, 0, 0, 0.10, -0.01]) expected = (0.14, -0.17) self.assertAlmostEqual(expected[0], actual[0]) self.assertAlmostEqual(expected[1], actual[1]) def test_stock_price_summary_2(self): """Test stock_price_summary with empty list.""" actual = a1.stock_price_summary([]) expected = (0, 0) self.assertAlmostEqual(expected[0], actual[0]) self.assertAlmostEqual(expected[1], actual[1]) def test_stock_price_summary_3(self): """Test stock_price_summary with a list full of 0.""" actual = a1.stock_price_summary([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) expected = (0, 0) self.assertAlmostEqual(expected[0], actual[0]) self.assertAlmostEqual(expected[1], actual[1]) def test_stock_price_summary_4(self): """Test stock_price_summary with a list with non-positive values.""" actual = a1.stock_price_summary([-0.01, -0.03, -0.02, -0.14, 0, 0, -0.10, -0.01]) expected = (0, -0.31) self.assertAlmostEqual(expected[0], actual[0]) self.assertAlmostEqual(expected[1], actual[1]) def test_stock_price_summary_5(self): """Test stock_price_summary with a list with non-negative values.""" actual = a1.stock_price_summary([0.01, 0.03, 0.02, 0.14, 0.1, 0.02, 0.10, 0.01]) expected = (0.43, 0) self.assertAlmostEqual(expected[0], actual[0]) self.assertAlmostEqual(expected[1], actual[1]) if __name__ == '__main__': unittest.main(exit=False)
import a1 import unittest class TestSwapK(unittest.TestCase): """ Test class for function a1.swap_k. """ # Add your test methods for a1.swap_k here. def test_swap_k_regular_list_integer(self): """Test swap_k with regular list and integer.""" actual = [1, 2, 3, 4, 5, 6] a1.swap_k(actual, 2) expected = [5, 6, 3, 4, 1, 2] self.assertEqual(expected, actual) def test_swap_k_empty_list_zero(self): """Test swap_k with empty list and 0.""" actual = [] a1.swap_k(actual, 0) expected = [] self.assertEqual(expected, actual) def test_swap_k_regular_list_zero(self): """Test swap_k with regular list and 0. The list should not change.""" actual = [1, 2, 3, 4, 5, 6] a1.swap_k(actual, 0) expected = [1, 2, 3, 4, 5, 6] self.assertEqual(expected, actual) def test_swap_k_4(self): """Test swap_k with a regular list and a negative integer. The function should raise an error """ self.assertRaises(ValueError, a1.swap_k, [1, 2, 3, 4, 5, 6], -1) def test_swap_k_5(self): """Test swap_k with a regular list and an integer which is larger than half of the size of the list.""" """The function should raise an error.""" self.assertRaises(ValueError, a1.swap_k, [1, 2, 3, 4, 5, 6], 4) def test_swap_k_7(self): """Test swap_k with a regular string list and a regular integer.""" actual = ['a', 'b', 'c', 'd', 'e', 'f'] a1.swap_k(actual, 3) expected = ['d', 'e', 'f', 'a', 'b', 'c'] self.assertEqual(expected, actual) if __name__ == '__main__': unittest.main(exit=False)