timeit

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

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

Решение на Работа с картинки от Кузман Белев

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

Към профила на Кузман Белев

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 10 успешни тест(а)
  • 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
COLOR = {0: 'red', 1: 'green', 2: 'blue'}


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


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


def invert(image):
    return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]


def add(x, coef):
    return int(x + coef * (255 - x))


def lighten(image, c):
    return modify(image, c)


def darken(image, c):
    return modify(image, -c)


def modify(image, c):
    return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]


def create_histogram(image):
    size = range(len(COLOR))
    flt = list(zip(*sum(image, [])))
    return {COLOR[i]:
            {clr: flt[i].count(clr) for clr in flt[i]} for i in size}

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

..FF........
======================================================================
FAIL: test_darken (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 127 not less than or equal to 1

======================================================================
FAIL: test_darken_rectangle (test.TestImages)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 28 not less than or equal to 1

----------------------------------------------------------------------
Ran 12 tests in 0.093s

FAILED (failures=2)

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

Кузман обнови решението на 12.03.2016 10:37 (преди почти 2 години)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def rotate_right(image):
    return list(zip(*iamge[::-1]))

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

def invert(image):
    return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]

def add(x, coef):
    return int(x + coef * (255 - x))

def sub(x, coef):
    return int(x - coef * (255 - x))

def lighten(image, c):
    return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]

def darken(image, c):
    return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]

# def create_histogram(image):
#         return {}

Кузман обнови решението на 12.03.2016 16:40 (преди почти 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
def rotate_right(image):
    return list(zip(*iamge[::-1]))

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

def invert(image):
    return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]

def add(x, coef):
    return int(x + coef * (255 - x))

def sub(x, coef):
    return int(x - coef * (255 - x))

def lighten(image, c):
    return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]

def darken(image, c):
    return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]

def create_histogram(image):
    result = {"red": {}, "green": {}, "blue": {}}
    for row in image:
        for rgb in row:
            for i in range(3):
                if i == 0:
                    if rgb[i] in result["red"]:
                        result["red"][rgb[i]] += 1
                    else:
                        result["red"][rgb[i]] = 1
                if i == 1:
                    if rgb[i] in result["green"]:
                        result["green"][rgb[i]] += 1
                    else:
                        result["green"][rgb[i]] = 1
                if i == 2:
                    if rgb[i] in result["blue"]:
                        result["blue"][rgb[i]] += 1
                    else:
                        result["blue"][rgb[i]] = 1
    return result

Кузман обнови решението на 12.03.2016 16:44 (преди почти 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
def rotate_right(image):
    return list(zip(*iamge[::-1]))

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

def invert(image):
    return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]

def add(x, coef):
    return int(x + coef * (255 - x))

def sub(x, coef):
    return int(x - coef * (255 - x))

def lighten(image, c):
    return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]

def darken(image, c):
    return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]

def create_histogram(image):
    result = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for rgb in row:
            for i in range(3):
                if i == 0:
                    if rgb[i] in result['red']:
                        result['red'][rgb[i]] += 1
                    else:
                        result['red'][rgb[i]] = 1
                if i == 1:
                    if rgb[i] in result['green']:
                        result['green'][rgb[i]] += 1
                    else:
                        result['green'][rgb[i]] = 1
                if i == 2:
                    if rgb[i] in result['blue']:
                        result['blue'][rgb[i]] += 1
                    else:
                        result['blue'][rgb[i]] = 1
    return result

Кузман обнови решението на 12.03.2016 17:16 (преди почти 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
def rotate_right(image):
    return list(zip(*image[::-1]))

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

def invert(image):
    return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]

def add(x, coef):
    return int(x + coef * (255 - x))

def sub(x, coef):
    return int(x - coef * (255 - x))

def lighten(image, c):
    return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]

def darken(image, c):
    return [[tuple([sub(x, c) for x in rgb]) for rgb in row] for row in image]

def create_histogram(image):
    result = {'red': {}, 'green': {}, 'blue': {}}
    for row in image:
        for rgb in row:
            for i in range(3):
                if i == 0:
                    if rgb[i] in result['red']:
                        result['red'][rgb[i]] += 1
                    else:
                        result['red'][rgb[i]] = 1
                if i == 1:
                    if rgb[i] in result['green']:
                        result['green'][rgb[i]] += 1
                    else:
                        result['green'][rgb[i]] = 1
                if i == 2:
                    if rgb[i] in result['blue']:
                        result['blue'][rgb[i]] += 1
                    else:
                        result['blue'][rgb[i]] = 1
    return result

Не ме радва, че има доста повтарящ се код, направо си е разхищение :)

Особено този в create_histogram е доста неприятен. Представи си, че в един пиксел има 100, вместо три. Тогава ще си по-принуден да не повтаряш if-else-блокове. Току виж си отървал и единия for.

Кузман обнови решението на 13.03.2016 23:22 (преди почти 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
COLOR = {0: 'red', 1: 'green', 2: 'blue'}

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

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

def invert(image):
    return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]

def add(x, coef):
    return int(x + coef * (255 - x))

def lighten(image, c):
    return modify(image, c)

def darken(image, c):
    return modify(image, -c)

def modify(image, c):
    return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]

def create_histogram(image):
    size = range(len(COLOR))
    flt = list(zip(*sum(image, [])))
    return {COLOR[i]:
        { clr: flt[i].count(clr) for clr in flt[i] } for i in size}

Кузман обнови решението на 13.03.2016 23:31 (преди почти 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
COLOR = {0: 'red', 1: 'green', 2: 'blue'}


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


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


def invert(image):
    return [[tuple([255 - x for x in rgb]) for rgb in row] for row in image]


def add(x, coef):
    return int(x + coef * (255 - x))


def lighten(image, c):
    return modify(image, c)


def darken(image, c):
    return modify(image, -c)


def modify(image, c):
    return [[tuple([add(x, c) for x in rgb]) for rgb in row] for row in image]


def create_histogram(image):
    size = range(len(COLOR))
    flt = list(zip(*sum(image, [])))
    return {COLOR[i]:
            {clr: flt[i].count(clr) for clr in flt[i]} for i in size}