def minimumLevel(numbers): # print("numbers=" + numbers) localOptimal = [] for idx in range(0, len(numbers) + 1): localOptimal.append(0) localOptimal[0] = 0 localOptimal[1] = INF localOptimal[2] = INF localOptimal[3] = integratedLevel(numbers[:3], 3) # vs integratedLevel(numbers[:4], 3) + localOptimal[4-3] localOptimal[4] = integratedLevel(numbers[:4], 4) + localOptimal[4 - 4] # vs integratedLevel(numbers[:4], 5) + localOptimal[4-5] # vs integratedLevel(numbers[:5], 3) + localOptimal[5-3] # vs integratedLevel(numbers[:5], 4) + localOptimal[5-4] localOptimal[5] = integratedLevel(numbers[:5], 5) for idx in range(6, len(numbers) + 1): minimum = INF for sublength in [3, 4, 5]: candidate = integratedLevel(numbers[:idx], sublength) \ + localOptimal[idx - sublength] if minimum > candidate: minimum = candidate localOptimal[idx] = minimum # print("localOptimal= " + str(minimum)) return localOptimal[-1]
def test_2(self): self.assertEqual(integratedLevel('123', 3), 2)
def test_length5_5(self): self.assertEqual(integratedLevel('13901', 5), 10)
def test_1(self): self.assertEqual(integratedLevel('000', 3), 1)
def test_length5_3(self): self.assertEqual(integratedLevel('23232', 5), 4)
def test_length5_4(self): self.assertEqual(integratedLevel('13579', 5), 5)
def test_length5_1(self): self.assertEqual(integratedLevel('00000', 5), 1)
def test_length5_2(self): self.assertEqual(integratedLevel('12345', 5), 2)
def test_length4_4(self): self.assertEqual(integratedLevel('1357', 4), 5)
def test_length4_5(self): self.assertEqual(integratedLevel('1390', 4), 10)
def test_length4_3(self): self.assertEqual(integratedLevel('2323', 4), 4)
def test_length4_2(self): self.assertEqual(integratedLevel('1234', 4), 2)
def test_5(self): self.assertEqual(integratedLevel('139', 3), 10)
def test_4(self): self.assertEqual(integratedLevel('147', 3), 5)
def test_3(self): self.assertEqual(integratedLevel('232', 3), 4)