-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q10_paint_fill.py
52 lines (45 loc) · 1.36 KB
/
Q10_paint_fill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest
def paint_fill_with_color(image, x, y, color):
if x < 0 or x >= len(image[0]) or y < 0 or y >= len(image):
return image
old_color = image[y][x]
paint_fill(image, x, y, color, old_color)
def paint_fill(image, x, y, color, old_color):
neighbours = [(1, 0), (-1, 0), (0, 1), (0, -1)]
if (
x < 0
or x >= len(image[0])
or y < 0
or y >= len(image)
or image[y][x] != old_color
):
return
image[y][x] = color
for neighbour in neighbours:
paint_fill(image, x + neighbour[0], y + neighbour[1], color, old_color)
class Test(unittest.TestCase):
def test_paint_fill(self):
image1 = [
[10, 10, 10, 10],
[30, 20, 20, 10],
[10, 10, 20, 20],
[10, 10, 30, 20],
]
image2 = [
[10, 10, 10, 10],
[30, 20, 20, 10],
[30, 30, 20, 20],
[30, 30, 30, 20],
]
image3 = [
[10, 10, 10, 10],
[10, 20, 20, 10],
[10, 10, 20, 20],
[10, 10, 10, 20],
]
paint_fill_with_color(image1, 0, 2, 30)
self.assertEqual(image1, image2)
paint_fill_with_color(image1, 0, 3, 10)
self.assertEqual(image1, image3)
paint_fill_with_color(image1, 5, 0, 50)
self.assertEqual(image1, image3)