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
def get_columns(matrix):
    return [list(x) for x in zip(*matrix)]


def rotate_left(picture):
    columns = get_columns(picture)
    return columns[::-1]


def rotate_right(picture):
    columns = get_columns(picture)
    return list(map(lambda column: column[::-1], columns))


def map_picture(func, picture):
    def map_pixel(pixel):
        return tuple(func(index, color) for index, color in enumerate(pixel))
    return [list(map(map_pixel, row)) for row in picture]


def invert(picture):
    def invert_color(_, color):
        return 255 - color
    return map_picture(invert_color, picture)


def lighten(picture, factor):
    def lighten_color(_, color):
        return int(color + factor * (255 - color))
    return map_picture(lighten_color, picture)


def darken(picture, factor):
    def darken_color(_, color):
        return int(color - factor*color)
    return map_picture(darken_color, picture)


def create_histogram(picture):
    histogram = {
        0: {},
        1: {},
        2: {},
    }

    def add_color(index, color):
        histogram[index][color] = histogram[index].get(color, 0) + 1
    map_picture(add_color, picture)
    return {
        "red": histogram[0],
        "green": histogram[1],
        "blue": histogram[2],
    }

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.084s

OK

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

Денис обнови решението на 13.03.2016 01:39 (преди почти 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
def get_columns(matrix):
    return [list(x) for x in zip(*matrix)]


def rotate_left(picture):
    columns = get_columns(picture)
    return columns[::-1]


def rotate_right(picture):
    columns = get_columns(picture)
    return list(map(lambda column: column[::-1], columns))


def map_picture(func, picture):
    def map_pixel(pixel):
        return tuple(func(index, color) for index, color in enumerate(pixel))
    return [list(map(map_pixel, row)) for row in picture]


def invert(picture):
    def invert_color(_, color):
        return 255 - color
    return map_picture(invert_color, picture)


def lighten(picture, factor):
    def lighten_color(_, color):
        return int(color + factor * (255 - color))
    return map_picture(lighten_color, picture)


def darken(picture, factor):
    def darken_color(_, color):
        return int(color - factor*color)
    return map_picture(darken_color, picture)


def create_histogram(picture):
    histogram = {
        0: dict.fromkeys(range(0, 256), 0),
        1: dict.fromkeys(range(0, 256), 0),
        2: dict.fromkeys(range(0, 256), 0),
    }

    def add_color(index, color):
        histogram[index][color] += 1
    map_picture(add_color, picture)
    return {
        "red": histogram[0],
        "green": histogram[1],
        "blue": histogram[2],
    }

Денис обнови решението на 13.03.2016 19:17 (преди почти 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
def get_columns(matrix):
    return [list(x) for x in zip(*matrix)]


def rotate_left(picture):
    columns = get_columns(picture)
    return columns[::-1]


def rotate_right(picture):
    columns = get_columns(picture)
    return list(map(lambda column: column[::-1], columns))


def map_picture(func, picture):
    def map_pixel(pixel):
        return tuple(func(index, color) for index, color in enumerate(pixel))
    return [list(map(map_pixel, row)) for row in picture]


def invert(picture):
    def invert_color(_, color):
        return 255 - color
    return map_picture(invert_color, picture)


def lighten(picture, factor):
    def lighten_color(_, color):
        return int(color + factor * (255 - color))
    return map_picture(lighten_color, picture)


def darken(picture, factor):
    def darken_color(_, color):
        return int(color - factor*color)
    return map_picture(darken_color, picture)


def create_histogram(picture):
    histogram = {
        0: {},
        1: {},
        2: {},
    }

    def add_color(index, color):
        histogram[index][color] = histogram[index].get(color, 0) + 1
    map_picture(add_color, picture)
    return {
        "red": histogram[0],
        "green": histogram[1],
        "blue": histogram[2],
    }