timeit

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

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

Решение на Работа с картинки от Даниел Шушков

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

Към профила на Даниел Шушков

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 8 успешни тест(а)
  • 4 неуспешни тест(а)

Код

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


def count_pixel(histogram, pixel):
    histogram['red'][pixel[0]] += 1
    histogram['green'][pixel[1]] += 1
    histogram['blue'][pixel[2]] += 1


def create_histogram(image):
    histogram = {'red': defaultdict(int),
                 'green': defaultdict(int),
                 'blue': defaultdict(int)
                 }
    for row in image:
        for pixel in row:
            count_pixel(histogram, pixel)
    return histogram


def invert_calc(pixel):
    return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])


def invert(image):
    invert = [list(map(invert_calc, row)) for row in image]
    return invert


def darken(image, index):
    return [map(lambda pixel:(int(pixel[0] - index*pixel[0]),
                              int(pixel[1] - index*pixel[1]),
                              int(pixel[2] - index*pixel[2])), row)
            for row in image]


def lighten(image, index):
    return [list(map(lambda pixel:(int(pixel[0] + index*(255 - pixel[0])),
                                   int(pixel[1] + index*(255 - pixel[1])),
                                   int(pixel[2] + index*(255 - pixel[2]))),
                     row))
            for row in image]


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


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

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

..EE......EE
======================================================================
ERROR: 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
TypeError: object of type 'map' has no len()

======================================================================
ERROR: 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
TypeError: object of type 'map' has no len()

======================================================================
ERROR: test_rotate_right (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
TypeError: object of type 'zip' has no len()

======================================================================
ERROR: test_rotate_right_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
TypeError: object of type 'zip' has no len()

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

FAILED (errors=4)

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

Даниел обнови решението на 12.03.2016 11: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
def make_change(histo, pixel):
    histo['red'][pixel[0]] += 1
    histo['green'][pixel[1]] += 1
    histo['blue'][pixel[2]] += 1


def create_histogram(pic):
    histogram = {'red': {},
                 'green': {},
                 'blue': {}
                 }
    for i in range(0, 256):
        histogram['red'][i] = 0
        histogram['green'][i] = 0
        histogram['blue'][i] = 0
    for row in pic:
        for pixel in row:
            make_change(histogram, pixel)
    return histogram


def invert_calc(pixel):
    return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])


def invert(pic):
    invert = [list(map(invert_calc, row)) for row in pic]
    return invert


def darken(pic, index):
    return [map(lambda pixel:(int(pixel[0] - index * pixel[0]),
                              int(pixel[1] - index * pixel[1]),
                              int(pixel[2] - index * pixel[2])), row)
            for row in pic]


def lighten(pic, index):
    return [list(map(lambda pixel:(int(pixel[0] + index * (255 - pixel[0])),
                                   int(pixel[1] + index * (255 - pixel[1])),
                                   int(pixel[2] + index * (255 - pixel[2]))),
                     row))
            for row in pic]


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


def rotate_right(pic):
    return zip(*pic[::-1])
  • Няма нужда да указваш липсващи цветове в create_histogram
  • Дефиницията на histogram спокойно можеш да я сбиеш на един ред
  • Идята за функция като make_change е добра. Измисли ѝ по-добро име, обаче
  • Няма нужда да съкращаваш image до pic и histogram до histo
  • Приложи тази препоръка (за която няма автоматична проверка) от PEP 8:

If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator.

Даниел обнови решението на 14.03.2016 14: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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from collections import defaultdict


def count_pixel(histogram, pixel):
    histogram['red'][pixel[0]] += 1
    histogram['green'][pixel[1]] += 1
    histogram['blue'][pixel[2]] += 1


def create_histogram(image):
    histogram = {'red': defaultdict(int),
                 'green': defaultdict(int),
                 'blue': defaultdict(int)
                 }
    for row in image:
        for pixel in row:
            count_pixel(histogram, pixel)
    return histogram


def invert_calc(pixel):
    return (255 - pixel[0], 255 - pixel[1], 255 - pixel[2])


def invert(image):
    invert = [list(map(invert_calc, row)) for row in image]
    return invert


def darken(image, index):
    return [map(lambda pixel:(int(pixel[0] - index*pixel[0]),
                              int(pixel[1] - index*pixel[1]),
                              int(pixel[2] - index*pixel[2])), row)
            for row in image]


def lighten(image, index):
    return [list(map(lambda pixel:(int(pixel[0] + index*(255 - pixel[0])),
                                   int(pixel[1] + index*(255 - pixel[1])),
                                   int(pixel[2] + index*(255 - pixel[2]))),
                     row))
            for row in image]


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


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