def test_longest_increasing_subsequence(self): self.assertListEqual( lis([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]), [0, 2, 6, 9, 11, 15] ) self.assertListEqual( lis([1, 10, 100, 1000, 2, 3, 4, 5, 10, 20, 6, 7, 8]), [1, 2, 3, 4, 5, 6, 7, 8] )
def test_multiple_lis(self): """When there is more than one LIS, all LISs are returned""" answer = [ [0, 4, 6, 9, 13, 15], [0, 2, 6, 9, 13, 15], [0, 4, 6, 9, 11, 15], [0, 2, 6, 9, 11, 15] ] self.assertEquals(answer, lis.lis([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]))
def lcs(s1, s2): '''The longest common subsequence algorithm by reduction to the longest increasing subsequence problem.''' pi = [] for x in s1: pos = [] for i, y in enumerate(s2): if x == y: pos = [i,] + pos pi += pos return [s2[i] for i in lis(pi, greedy_cover)]
def myprint(lst): try: s1 = lis_s(lst) s2 = lis(lst) print "passed" if len(s1) == len(s2) else "failed" print lst print s1 print s2 print "\n" except: print "Runtime error!"
def find_lis(img1, img2): # Initiate SIFT detector orb = cv2.ORB_create() # find the keypoints and descriptors with SIFT kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # create BFMatcher object bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # Match descriptors. matches = bf.match(des1, des2) # Sort them in the order of their distance. matches = sorted(matches, key=lambda x: x.distance) # print('len:',len(matches)) img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=2) # cv2.imshow('1', img3) cv2.waitKey(0) cv2.destroyAllWindows() dict_train_to_query = {} dict_query_to_train = {} query_idx = [] for x in matches: query_idx.append(x.queryIdx) dict_train_to_query[x.trainIdx] = x.queryIdx dict_query_to_train[x.queryIdx] = x.trainIdx train_idx = dict_train_to_query.keys() sorted_query_kps = [] for x in query_idx: sorted_query_kps.append((kp1[x].pt[0], x, dict_query_to_train[x])) sorted_query_kps = sorted(sorted_query_kps, key=lambda x: x[0]) wlis_query = {} for i, x in enumerate(sorted_query_kps): wlis_query[x[1]] = i sorted_train_kps = [] for x in train_idx: sorted_train_kps.append((kp2[x].pt[0], x)) sorted_train_kps = sorted(sorted_train_kps, key=lambda x: x[0]) wlis_train = [] for x in sorted_train_kps: q_idx = dict_train_to_query[x[1]] idx = wlis_query[q_idx] wlis_train.append(idx) return lis(wlis_train)
def test_lis(self): for l, longest_s in zip(self.lists, self.list_lis): self.assertEqual(longest_s, lis(l))
def caculateLis(self): input_string = self.rawEdit.text() input_string = map(int, input_string.split(' ')) result = lis.lis(input_string) self.resultEdit.setText(result)
def test_empty_sequence_is_lis(self): """The empty sequence is the only LIS""" answer = [[]] self.assertEquals(answer, lis.lis([]))
def test_larger_element_before_single_lis(self): """When there is a large element before a single LIS, LIS is returned""" answer = [[1, 8, 9]] self.assertEquals(answer, lis.lis([11, 10, 1, 8, 9]))
def test_single_lis(self): """When there is one LIS, it is returned""" answer = [[0, 1, 8, 9]] self.assertEquals(answer, lis.lis([0, 10, 1, 8, 9]))
def test_itself_is_lis(self): """When there is an array that is itself the only LIS, it's returned""" answer = [[1, 2, 3, 4]] self.assertEquals(answer, lis.lis([1, 2, 3, 4]))
def test_single_element_is_lis(self): """A single element sequence is the only LIS""" answer = [[10]] self.assertEquals(answer, lis.lis([10]))