timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Работа с картинки от Николай Лазаров

Обратно към всички решения

Към профила на Николай Лазаров

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 12 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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
52
53
54
55
56
57
58
59
from collections import Counter


def rotate_left(image):
    height = len(image)
    width = len(image[0])
    rotated = [
        [image[x][y] for x in range(height)] for y in range(width-1, -1, -1)
    ]

    return rotated


def rotate_right(image):
    height = len(image)
    width = len(image[0])
    rotated = [
        [image[x][y] for x in range(height-1, -1, -1)] for y in range(width)
    ]

    return rotated


def invert(image):
    return [[(255-r, 255-g, 255-b) for (r, g, b) in row] for row in image]


def lighten(image, coef):
    def transform(p):
        return (p[0] + int(coef*(255-p[0])),
                p[1] + int(coef*(255-p[1])),
                p[2] + int(coef*(255-p[2])))

    return [[transform(p) for p in row] for row in image]


def darken(image, coef):
    def transform(p):
        return (p[0] - int(coef*p[0]),
                p[1] - int(coef*p[1]),
                p[2] - int(coef*p[2]))

    return [[transform(p) for p in row] for row in image]


def create_histogram(image):
    histogram = {
        'red': Counter(),
        'green': Counter(),
        'blue': Counter(),
    }

    for row in image:
        for pixel in row:
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1

    return histogram

Лог от изпълнението

............
----------------------------------------------------------------------
Ran 12 tests in 0.085s

OK

История (2 версии и 2 коментара)

Николай обнови решението на 08.03.2016 12:56 (преди почти 2 години)

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
import unittest

import solution


class TestImages(unittest.TestCase):
    image = [
        [(0, 0, 255), (0, 255, 0), (0, 0, 255)],
        [(255, 0, 0), (0, 0, 255), (0, 255, 0)],
        [(0, 255, 0), (0, 255, 0), (255, 0, 0)]]

    def test_rotate_left(self):
        self.assertEqual(
            solution.rotate_left(self.image),
            [[(0, 0, 255), (0, 255, 0), (255, 0, 0)],
             [(0, 255, 0), (0, 0, 255), (0, 255, 0)],
             [(0, 0, 255), (255, 0, 0), (0, 255, 0)]])

    def test_lighten(self):
        self.assertEqual(
            solution.lighten(self.image, 0.5),
            [[(127, 127, 255), (127, 255, 127), (127, 127, 255)],
             [(255, 127, 127), (127, 127, 255), (127, 255, 127)],
             [(127, 255, 127), (127, 255, 127), (255, 127, 127)]])

    def test_invert(self):
        self.assertEqual(
            solution.invert(self.image),
            [[(255, 255, 0), (255, 0, 255), (255, 255, 0)],
             [(0, 255, 255), (255, 255, 0), (255, 0, 255)],
             [(255, 0, 255), (255, 0, 255), (0, 255, 255)]])

    def test_create_histogram(self):
        self.assertEqual(
            solution.create_histogram(self.image),
            {'blue': {0: 6, 255: 3},
             'green': {0: 5, 255: 4},
             'red': {0: 7, 255: 2}})


if __name__ == '__main__':
    unittest.main()

Николай обнови решението на 08.03.2016 14:50 (преди почти 2 години)

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
52
53
54
55
56
57
58
59
from collections import Counter


def rotate_left(image):
    height = len(image)
    width = len(image[0])
    rotated = [
        [image[x][y] for x in range(height)] for y in range(width-1, -1, -1)
    ]

    return rotated


def rotate_right(image):
    height = len(image)
    width = len(image[0])
    rotated = [
        [image[x][y] for x in range(height-1, -1, -1)] for y in range(width)
    ]

    return rotated


def invert(image):
    return [[(255-r, 255-g, 255-b) for (r, g, b) in row] for row in image]


def lighten(image, coef):
    def transform(p):
        return (p[0] + int(coef*(255-p[0])),
                p[1] + int(coef*(255-p[1])),
                p[2] + int(coef*(255-p[2])))

    return [[transform(p) for p in row] for row in image]


def darken(image, coef):
    def transform(p):
        return (p[0] - int(coef*p[0]),
                p[1] - int(coef*p[1]),
                p[2] - int(coef*p[2]))

    return [[transform(p) for p in row] for row in image]


def create_histogram(image):
    histogram = {
        'red': Counter(),
        'green': Counter(),
        'blue': Counter(),
    }

    for row in image:
        for pixel in row:
            histogram['red'][pixel[0]] += 1
            histogram['green'][pixel[1]] += 1
            histogram['blue'][pixel[2]] += 1

    return histogram
  • Не изпозлваш предимствата на Counter в create_histogram, каква ще бъде разликата, ако използваш defaultdict в твоя случай?
  • Еднобуквените имена рядко са добра идея (когато говорим за координати x и y са хубави имена, но p може да бъде по-описателно)