From e232bf532001318e60655813603cda53ab729686 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Mon, 25 Jun 2018 19:06:36 -0700 Subject: [PATCH] Add image overlap problem --- dotfiles/lib/python/image_overlap.py | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 dotfiles/lib/python/image_overlap.py diff --git a/dotfiles/lib/python/image_overlap.py b/dotfiles/lib/python/image_overlap.py new file mode 100644 index 00000000..de05c4e6 --- /dev/null +++ b/dotfiles/lib/python/image_overlap.py @@ -0,0 +1,37 @@ +class Solution(object): + def largestOverlap(self, A, B): + self.init(A, B) + return max( + self.compare(x_trans, y_trans) + for x_trans in range(-(self.row_length-1), self.row_length) + for y_trans in range(-(self.column_count-1), self.column_count) + ) + + def init(self, A, B): + self.A = A + self.B = B + self.row_length = len(A[0]) + self.column_count = len(A) + + def compare(self, x_trans, y_trans): + overlap_count = 0 + for row_selection in range( + max(y_trans, 0), + min(self.column_count, self.column_count + y_trans) + ): + for column_selection in range( + max(x_trans, 0), + min(self.row_length, self.row_length + x_trans) + ): + if ( + self.A[row_selection][column_selection] == + self.B[row_selection - y_trans][column_selection - x_trans] == 1 + ): + overlap_count += 1 + return overlap_count + +if __name__ == '__main__': + sol = Solution() + sol.init([[1,1,0],[0,1,0],[0,1,0]], + [[1,0,0],[0,1,1],[0,0,1]]) + print(sol.compare(-1, -1))