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
from collections import defaultdict


def rotate_left(picture):
    rotated_picture = list(zip(*picture))
    rotated_picture.reverse()
    return rotated_picture


def rotate_right(picture):
    rotated_picture = list(picture)
    rotated_picture.reverse()
    rotated_picture = list(zip(*rotated_picture))
    return rotated_picture


def invert(picture):
    def invert_color(pixel):
        return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])

    return [list(map(invert_color, row)) for row in picture]


def lighten(picture, factor):
    def lighten_color(pixel):
        red = pixel[0] + factor * (255 - pixel[0])
        green = pixel[1] + factor * (255 - pixel[1])
        blue = pixel[2] + factor * (255 - pixel[2])
        return (int(red), int(green), int(blue))

    return [list(map(lighten_color, row)) for row in picture]


def darken(picture, factor):
    def darken_color(pixel):
        red = pixel[0] - factor * pixel[0]
        green = pixel[1] - factor * pixel[1]
        blue = pixel[2] - factor * pixel[2]
        return (int(red), int(green), int(blue))

    return [list(map(darken_color, row)) for row in picture]


def create_histogram(picture):
    def color_value(index):
        color_statistics = defaultdict(int)
        color_values = [pixel[index] for row in picture for pixel in row]

        for value in color_values:
            color_statistics[value] += 1

        return color_statistics

    return {
        'red': color_value(0),
        'green': color_value(1),
        'blue': color_value(2)
    }

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.086s

OK

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

Марио обнови решението на 14.03.2016 12:04 (преди почти 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
from collections import defaultdict


def rotate_left(picture):
    rotated_picture = list(zip(*picture))
    rotated_picture.reverse()
    return rotated_picture


def rotate_right(picture):
    rotated_picture = list(picture)
    rotated_picture.reverse()
    rotated_picture = list(zip(*rotated_picture))
    return rotated_picture


def invert(picture):
    def invert_color(pixel):
        return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])

    return [list(map(invert_color, row)) for row in picture]


def lighten(picture, factor):
    def lighten_color(pixel):
        red = pixel[0] + factor * (255 - pixel[0])
        green = pixel[1] + factor * (255 - pixel[1])
        blue = pixel[2] + factor * (255 - pixel[2])
        return (int(red), int(green), int(blue))

    return [list(map(lighten_color, row)) for row in picture]


def darken(picture, factor):
    def darken_color(pixel):
        red = pixel[0] - factor * pixel[0]
        green = pixel[1] - factor * pixel[1]
        blue = pixel[2] - factor * pixel[2]
        return (int(red), int(green), int(blue))

    return [list(map(darken_color, row)) for row in picture]


def create_histogram(picture):
    def color_value(index):
        color_statistics = defaultdict(int)
        color_values = [pixel[index] for row in picture for pixel in row]

        for value in color_values:
            color_statistics[value] += 1

        return color_statistics

    return {
        'red': color_value(0),
        'green': color_value(1),
        'blue': color_value(2)
    }