Exercise 17: Bag of words - Advanced
When running the tests for Exercise 17: Bag of words - Advanced, only one of the test cases pass.
File "/usr/local/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
yield
File "/usr/local/lib/python3.8/unittest/case.py", line 676, in run
self._callTestMethod(testMethod)
File "/usr/local/lib/python3.8/unittest/case.py", line 633, in _callTestMethod
method()
File "/app/test/test_main.py", line 41, in test_answer_two
self.assertIn(output, expected)
File "/usr/local/lib/python3.8/unittest/case.py", line 1179, in assertIn
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/local/lib/python3.8/unittest/case.py", line 753, in fail
raise self.failureException(msg)
Error: '(2, 3)' not found in ['(0, 4)', '(4, 0)']I can’t for the life of me work out what’s wrong with my solution:
import numpy as np
data = [[1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 3, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]]
def distance(matrix, rowIndex1, rowIndex2):
d = 0
row1 = matrix[rowIndex1]
row2 = matrix[rowIndex2]
for i in range(len(row1)):
d += abs(row1[i] - row2[i])
return d
def find_nearest_pair(matrix):
N = len(matrix)
temp = np.empty((N, N), dtype=float)
for i in range(N):
for j in range(N):
if i == j:
temp[i][j] = np.inf
else:
temp[i][j] = distance(matrix, i, j)
print(np.unravel_index(np.argmin(temp), temp.shape))
find_nearest_pair(data)
Any help would be much appreciated.