Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.83 KB | None | 0 0
  1. """
  2. Bucket Fill Exercise
  3.  
  4. Imagine you are working on an image editing application. You need to implement a bucket fill tool similar to the one
  5. in paint. The user will use the tool by selecting a color and clicking on the canvas. The tool fills the selected
  6. region of color with the new color.
  7.  
  8. When a pixel is filled, all of its neighbors (above, below, left, or right) of the same color must also be filled,
  9. as well as their neighbors, and so on, until the entire region has been filled with the new color.
  10.  
  11. In this exercise, you must write *TWO* implementations of the tool. Each implementation must be different. It is not
  12. required that you invent the solutions yourself. You are encouraged to research the problem. Please write documentation
  13. explaining the difference of each implementation, such as when one solution might be more appropriate than the other.
  14. Don't forget to validate input. There is one existing test, however, you might consider adding some more. Keep in mind
  15. that although the given canvas is small, the solution should be applicable for a real canvas that could have huge
  16. resolutions.
  17.  
  18. Please use python3 to complete this assignment.
  19. """
  20. import queue
  21. from scipy.ndimage.measurements import label
  22. import numpy as np
  23.  
  24.  
  25.  
  26. class Canvas(object):
  27.     def __init__(self, pixels):
  28.         self.pixels = pixels
  29.  
  30.     def __str__(self):
  31.         return '\n'.join(map(lambda row: ''.join(row), self.pixels))
  32.  
  33.     def fill(self, x, y, color):
  34.         """
  35.        Fills a region of color at a given location with a given color.
  36.  
  37.        :param x:  the x coordinate where the user clicked
  38.        :param y: the y coordinate where the user clicked
  39.        :param color: the specified color to change the region to
  40.        """
  41.         raise NotImplementedError  # Override this function in the Solution classes
  42.  
  43.  
  44. class Solution1(Canvas):
  45.     # TODO write documentation
  46.  
  47.     def fill(self, x, y, color):
  48.         bucket = self.pixels
  49.  
  50.         b_height = len(bucket)
  51.         b_width = len(bucket[0])
  52.  
  53.         start = bucket[x][y]
  54.         bucket[x][y] = color
  55.  
  56.         q = queue.Queue()
  57.         q.put([x, y])
  58.  
  59.         visited = set()
  60.         visited.add((x, y))
  61.  
  62.         while not q.empty():
  63.             i, j = q.get(0)
  64.  
  65.             # color bucket position (i, j-1) with color
  66.             if (0 <= i < b_height) and (0 <= (j-1) < b_width) and ((i, j-1) not in visited):
  67.                     visited.add((i, j-1))
  68.                     if (bucket[i][j-1] == start):
  69.                         bucket[i][j-1] = color
  70.                         q.put([i, j-1])
  71.  
  72.             # color bucket position (i, j+1) with color
  73.             if (0 <= i < b_height) and (0 <= (j+1) < b_width) and ((i, j+1) not in visited):
  74.                     visited.add((i, j+1))
  75.                     if (bucket[i][j+1] == start):
  76.                         bucket[i][j+1] = color
  77.                         q.put([i, j+1])
  78.  
  79.             # color_bucket (i+1, j) position
  80.             if (0 <= (i+1) < b_height) and (0 <= j < b_width) and ((i+1, j) not in visited):
  81.                     visited.add((i+1, j))
  82.                     if (bucket[i+1][j] == start):
  83.                         bucket[i+1][j] = color
  84.                         q.put([i+1, j])
  85.  
  86.             # color_bucket (i-1, j) position
  87.             if (0 <= (i-1) < b_height) and (0 <= j < b_width) and ((i-1, j) not in visited):
  88.                     visited.add((i-1, j))
  89.                     if (bucket[i-1][j] == start):
  90.                         bucket[i-1][j] = color
  91.                         q.put([i-1, j])
  92.  
  93.         self.pixels = bucket
  94.  
  95.  
  96.  
  97. class Solution2(Canvas):
  98.     # TODO write documentation
  99.  
  100.     def color_bucket(self, x, y, start, color):
  101.         # print(x,y, start, color)
  102.         if (start == color):
  103.             return
  104.  
  105.         if (0 <= x < len(self.pixels)) and (0 <= y < len(self.pixels[0])) and self.pixels[x][y] == start:
  106.             self.pixels[x][y] = color
  107.         else:
  108.             return
  109.  
  110.         self.color_bucket(x, y - 1, start, color)
  111.         self.color_bucket(x, y + 1, start, color)
  112.         self.color_bucket(x + 1, y, start, color)
  113.         self.color_bucket(x - 1, y, start, color)
  114.  
  115.     def fill(self, x, y, color):
  116.         start = self.pixels[x][y]
  117.         self.color_bucket(x, y, start, color)
  118.  
  119.  
  120.  
  121. def test_solution(impl):
  122.     s = impl([
  123.         ['O', 'X', 'X', 'X', 'X'],
  124.         ['X', 'O', 'O', 'O', 'X'],
  125.         ['X', 'O', '#', 'O', 'X'],
  126.         ['X', 'O', 'O', 'O', 'X'],
  127.         ['X', 'X', 'X', 'X', 'X'],
  128.         ['X', 'X', 'X', '#', '#'],
  129.         ['X', 'X', 'X', 'X', 'X']
  130.     ])
  131.     s.fill(0, 1, '*')
  132.     # print(s)
  133.     s.fill(5, 4, 'O')
  134.     # print(s)
  135.     s.fill(2, 2, '@')
  136.     # print(s)
  137.     #
  138.     assert str(s) == 'O****\n*OOO*\n*O@O*\n*OOO*\n*****\n***OO\n*****'
  139.  
  140.  
  141.     a = impl([
  142.         ['O', 'X', 'O', 'X', 'O']
  143.     ])
  144.  
  145.     a.fill(0, 0, 'X')
  146.     # print(a)
  147.     # Output(BFS): XXOXO
  148.     # Output(DFS): XXOXO
  149.  
  150.     a.fill(0, 4, '#')
  151.     # print(a)
  152.     # Output(BFS): XXOX#
  153.     # Output(DFS): XXOX#
  154.  
  155.     assert str(a) == 'XXOX#'
  156.  
  157.  
  158. #-------------------------------------------------------------
  159.  
  160.  
  161.     b = impl([
  162.         ['O', 'O', 'O', 'O', 'O']
  163.     ])
  164.  
  165.     b.fill(0, 0, 'X')
  166.     # print(b)
  167.     # # Output(BFS): XXXXX
  168.     # # Output(DFS): XXXXX
  169.     #
  170.     b.fill(0, 2, 'X')
  171.     # print(b)
  172.     # # Output(BFS): XXXXX
  173.     # # Output(DFS): XXXXX
  174.     #
  175.     b.fill(0, 4, 'X')
  176.     # print(b)
  177.     # Output(BFS): XXXXX
  178.     # Output(DFS: XXXXX
  179.  
  180.     assert str(b) == 'XXXXX'
  181.  
  182.  
  183. #---------------------------------------------------------------
  184.  
  185.     c = impl([
  186.         ['O']
  187.     ])
  188.  
  189.     c.fill(0, 0, 'X')
  190.     # print(c)
  191.     # Output(BFS): X
  192.     # Output(DFS): X
  193.  
  194.     assert str(c) == 'X'
  195.  
  196.  
  197.  
  198. # ---------------------------------------------------------------
  199.  
  200.  
  201.     d = impl([
  202.         ['X'],
  203.         ['X'],
  204.         ['X'],
  205.         ['X'],
  206.         ['X']
  207.     ])
  208.  
  209.     d.fill(0, 0, '#')
  210.     # print(d)
  211.     # # Output(BFS):
  212.     # # #
  213.     # # #
  214.     # # #
  215.     # # #
  216.     # # #
  217.     #
  218.     # # Output(DFS):
  219.     # # #
  220.     # # #
  221.     # # #
  222.     # # #
  223.     # # #
  224.     #
  225.     d.fill(2, 0, '#')
  226.     # print(d)
  227.     # # Output(BFS):
  228.     # # #
  229.     # # #
  230.     # # #
  231.     # # #
  232.     # # #
  233.     #
  234.     # # Output(DFS):
  235.     # # #
  236.     # # #
  237.     # # #
  238.     # # #
  239.     # # #
  240.  
  241.     assert str(d) == '#\n#\n#\n#\n#'
  242.  
  243.  
  244.  
  245. # ---------------------------------------------------------------
  246.  
  247.     e = impl([
  248.         ['X', '#'],
  249.         ['X', '#']
  250.     ])
  251.  
  252.     e.fill(1, 1, 'X')
  253.     # print(e)
  254.     # # Output(BFS):
  255.     # # XX
  256.     # # XX
  257.     #
  258.     # # Output(DFS):
  259.     # # XX
  260.     # # XX
  261.     #
  262.     e.fill(1, 0, '#')
  263.     # print(e)
  264.     # # Output(BFS):
  265.     # # ##
  266.     # # ##
  267.     # #
  268.     # # Output(DFS):
  269.     # # ##
  270.     # # ##
  271.  
  272.     assert str(e) == '##\n##'
  273.  
  274.  
  275.  
  276. if __name__ == '__main__':
  277.     test_solution(Solution1)
  278.     test_solution(Solution2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement