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
from itertools import chain


def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def reducer(image, function):
    unpacked = list(chain(*image))
    unpacked = [tuple(map(function, x)) for x in unpacked]
    result = [[] for x in range(len(image))]
    for x in range(len(image)):
        for y in range(len(image[0])):
            result[x].append(unpacked[x * len(image[0]) + y])
    return result


def invert(image):
    return reducer(image, lambda x: 255 - x)


def lighten(image, coefficient):
    return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))


def darken(image, coefficient):
    return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))


def create_histogram(image):
    rgb = {'red': 0, 'green': 1, 'blue': 2}
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    unpacked = list(zip(*list(chain(*image))))
    for key, value in rgb.items():
        for element in unpacked[value]:
            histogram[key][element] = histogram[key].get(element, 0) + 1
    return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.102s

OK

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

Мартин обнови решението на 10.03.2016 17:11 (преди почти 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
def rotate_left(image):
    temp_image = [[] for x in range(len(image[0]))]
    rows = len(image)
    columns = len(image[0])
    [[temp_image[columns - 1 - y].append(image[x][y])
      for y in range(columns)]
     for x in range(rows)]
    return temp_image


def rotate_right(image):
    temp_image = [[] for x in range(len(image[0]))]
    rows = len(image)
    columns = len(image[0])
    [[temp_image[y].append(image[rows - 1 - x][y])
      for y in range(columns)]
     for x in range(rows)]
    return temp_image


def invert(image):
    rows = len(image)
    columns = len(image[0])
    temp_image = [[] for x in range(rows)]
    [[temp_image[x].append(tuple(map(lambda x: 255 - x, image[x][y])))
      for y in range(columns)]
     for x in range(rows)]
    return temp_image


def lighten(image, coefficient):
    rows = len(image)
    columns = len(image[0])
    temp_image = [[] for x in range(rows)]
    [[temp_image[x].append(tuple(
        map(lambda x: (int)(x + coefficient * (255 - x)), image[x][y])))
        for y in range(columns)]
     for x in range(rows)]
    return temp_image


def darken(image, coefficient):
    rows = len(image)
    columns = len(image[0])
    temp_image = [[] for x in range(rows)]
    [[temp_image[x].append(tuple(
        map(lambda x: (int)(x - coefficient * (0 + x)), image[x][y])))
        for y in range(columns)]
     for x in range(rows)]
    return temp_image


def create_histogram(image):
    histogram = dict()
    histogram['red'] = dict()
    histogram['blue'] = dict()
    histogram['green'] = dict()
    for x in range(len(image)):
        for y in range(len(image[0])):
            if image[x][y][0] not in histogram['red']:
                histogram['red'][image[x][y][0]] = 1
            else:
                histogram['red'][image[x][y][0]] += 1
            if image[x][y][1] not in histogram['green']:
                histogram['green'][image[x][y][1]] = 1
            else:
                histogram['green'][image[x][y][1]] += 1
            if image[x][y][2] not in histogram['blue']:
                histogram['blue'][image[x][y][2]] = 1
            else:
                histogram['blue'][image[x][y][2]] += 1
    return histogram
  • rotate_left и rotate_right са почти еднакви. Същото важи и за invert, lighten, darken. Помисли как да избегнеш това.
  • Ако стигнеш до нещо такова, значи list comprehension не е подходящото решение:
    [[temp_image[y].append(image[rows - 1 - x][y])
      for y in range(columns)]
     for x in range(rows)]
  • В този ред на мисли, идентацията на тези list comprehension-и са неконсистентни.
  • temp_* не е добро име за променлива. Всяка променлива в твоя код е "temp"
  • Нямаш добра причина не да създаваш празен речник с литерала {}. Винаги го предпочитай пред dict()
  • Трите if-else блока за red, green и blue са следствие от copy -> paste. Представи си, че цветовете в един пиксел не са 3, а 300. Как ги избегнал 300 if-else-a?

Мартин обнови решението на 12.03.2016 17: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
from itertools import chain


def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def reducer(image, function):
    unpacked = list(chain(*image))
    unpacked = [tuple(map(function, x)) for x in unpacked]
    result = [[] for x in range(len(image))]
    for x in range(len(image)):
        for y in range(len(image[0])):
            result[x].append(unpacked[x*len(image) + y])
    return result


def invert(image):
    return reducer(image, lambda x: 255 - x)


def lighten(image, coefficient):
    return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))


def darken(image, coefficient):
    return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))


def create_histogram(image):
    rows = len(image)
    columns = len(image[0])
    hui = {'red': 0, 'green': 1, 'blue': 2}
    histogram = {'red': {}, 'blue': {}, 'green': {}}
    for x in range(rows):
        for y in range(columns):
            for hist, count in histogram.items():
                if image[x][y][hui[hist]] not in count:
                    count[image[x][y][hui[hist]]] = 1
                else:
                    count[image[x][y][hui[hist]]] += 1
    return histogram

Мартин обнови решението на 12.03.2016 19:03 (преди почти 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
from itertools import chain


def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def reducer(image, function):
    unpacked = list(chain(*image))
    unpacked = [tuple(map(function, x)) for x in unpacked]
    result = [[] for x in range(len(image))]
    for x in range(len(image)):
        for y in range(len(image[0])):
            result[x].append(unpacked[x*len(image) + y])
    return result


def invert(image):
    return reducer(image, lambda x: 255 - x)


def lighten(image, coefficient):
    return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))


def darken(image, coefficient):
    return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))


def create_histogram(image):
    rows = len(image)
    columns = len(image[0])
    rgb = {'red': 0, 'green': 1, 'blue': 2}
    histogram = {'red': {}, 'blue': {}, 'green': {}}
    for x in range(rows):
        for y in range(columns):
            for hist, count in histogram.items():
                if image[x][y][rgb[hist]] not in count:
                    count[image[x][y][rgb[hist]]] = 1
                else:
                    count[image[x][y][rgb[hist]]] += 1
    return histogram

Мартин обнови решението на 12.03.2016 20:09 (преди почти 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
from itertools import chain


def rotate_left(image):
    return list(zip(*image))[::-1]


def rotate_right(image):
    return list(zip(*image[::-1]))


def reducer(image, function):
    unpacked = list(chain(*image))
    unpacked = [tuple(map(function, x)) for x in unpacked]
    result = [[] for x in range(len(image))]
    for x in range(len(image)):
        for y in range(len(image[0])):
            result[x].append(unpacked[x * len(image[0]) + y])
    return result


def invert(image):
    return reducer(image, lambda x: 255 - x)


def lighten(image, coefficient):
    return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))


def darken(image, coefficient):
    return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))


def create_histogram(image):
    rgb = {'red': 0, 'green': 1, 'blue': 2}
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    unpacked = list(zip(*list(chain(*image))))
    for key, value in rgb.items():
        for element in unpacked[value]:
            histogram[key][element] = histogram[key].get(element, 0) + 1
    return histogram