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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def rotate_left(source):
    new_image = [
        [row[-ind] for row in source]
        for ind in sorted(range(1, len(source) + 1))
        ]
    return new_image


def rotate_right(source):
    new_image = [
        [row[ind] for row in reversed(source)]
        for ind in range(len(source))
        ]
    return new_image


def invert(source):
    new_image = [
        [
            (255 - red, 255 - green, 255 - blue)
            for red, green, blue in row
            ]
        for row in source
        ]
    return new_image


def lighten(source, factor):
    new_image = [
        [
            (
                red + int(factor * (255 - red)),
                green + int(factor * (255 - green)),
                blue + int(factor * (255 - blue))
                )
            for red, green, blue in row
            ]
        for row in source
        ]
    return new_image


def darken(source, factor):
    new_image = [
        [
            (
                red - int(factor * red),
                green - int(factor * green),
                blue - int(factor * blue)
                )
            for red, green, blue in row
            ]
        for row in source
        ]
    return new_image


def create_histogram(source):
    reds = [red for row in source for (red, green, blue) in row]
    greens = [green for row in source for (red, green, blue) in row]
    blues = [blue for row in source for (red, green, blue) in row]

    histogram = {
        'red': colour_values(reds),
        'green': colour_values(greens),
        'blue': colour_values(blues)
        }

    return histogram


def colour_values(values):
    colour = {}
    for value in values:
        if value in colour:
            colour[value] += 1
        else:
            colour[value] = 1
    return colour

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

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

OK

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

Тодор обнови решението на 09.03.2016 22:53 (преди почти 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
60
61
62
def rotate_left(source):
    new_image = [
                 [row[-ind] for row in source]
                 for ind in sorted(range(1, len(source) + 1))]
    return new_image


def rotate_right(source):
    new_image = [
                 [row[ind] for row in reversed(source)]
                 for ind in range(len(source))]
    return new_image


def invert(source):
    new_image = [
                 [(255 - red, 255 - green, 255 - blue)
                  for red, green, blue in row]
                 for row in source]
    return new_image


def lighten(source, factor):
    new_image = [
                 [(red + int(factor * (255 - red)),
                   green + int(factor * (255 - green)),
                   blue + int(factor * (255 - blue)))
                  for red, green, blue in row]
                 for row in source]
    return new_image


def darken(source, factor):
    new_image = [
                 [(red - int(factor * red),
                   green - int(factor * green),
                   blue - int(factor * blue)) for red, green, blue in row]
                 for row in source]
    return new_image


def create_histogram(source):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    for row in source:
        for red, green, blue in row:

            if red in histogram['red']:
                histogram['red'][red] += 1
            else:
                histogram['red'][red] = 1

            if green in histogram['green']:
                histogram['green'][green] += 1
            else:
                histogram['green'][green] = 1

            if blue in histogram['blue']:
                histogram['blue'][blue] += 1
            else:
                histogram['blue'][blue] = 1

    return histogram
  • Индентацията на многоредовите ти comprehension-и не е много ок. Виждам, че pep8 tool-а не се кара за това, но погледни секцията за индентация в PEP8 и ще намериш частта за списъци.
  • Вложените for-ове и дългия if/else чаршаф могат да се преобразят в доста по-стегнат и приятен за гледане код.

Тодор обнови решението на 10.03.2016 21:25 (преди почти 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def rotate_left(source):
    new_image = [
        [row[-ind] for row in source]
        for ind in sorted(range(1, len(source) + 1))
        ]
    return new_image


def rotate_right(source):
    new_image = [
        [row[ind] for row in reversed(source)]
        for ind in range(len(source))
        ]
    return new_image


def invert(source):
    new_image = [
        [
            (255 - red, 255 - green, 255 - blue)
            for red, green, blue in row
            ]
        for row in source
        ]
    return new_image


def lighten(source, factor):
    new_image = [
        [
            (
                red + int(factor * (255 - red)),
                green + int(factor * (255 - green)),
                blue + int(factor * (255 - blue))
                )
            for red, green, blue in row
            ]
        for row in source
        ]
    return new_image


def darken(source, factor):
    new_image = [
        [
            (
                red - int(factor * red),
                green - int(factor * green),
                blue - int(factor * blue)
                )
            for red, green, blue in row
            ]
        for row in source
        ]
    return new_image


def create_histogram(source):
    reds = [red for row in source for (red, green, blue) in row]
    greens = [green for row in source for (red, green, blue) in row]
    blues = [blue for row in source for (red, green, blue) in row]

    histogram = {
        'red': colour_values(reds),
        'green': colour_values(greens),
        'blue': colour_values(blues)
        }

    return histogram


def colour_values(values):
    colour = {}
    for value in values:
        if value in colour:
            colour[value] += 1
        else:
            colour[value] = 1
    return colour