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
51
52
53
54
55
56
57
58
59
60
61
62
import itertools


def help_invert(pixelLine):
    return [(
                int(255 - pixel[0]),
                int(255 - pixel[1]),
                int(255 - pixel[2])
            ) for pixel in pixelLine]


def invert(picture):
    return tuple(map(help_invert, picture))


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


def rotate_left(picture):
    return reversed(list(zip(*picture[::1])))


def help_darken(pixelLine, n):
    return [(
                int(pixel[0] - n * (pixel[0] - 0)),
                int(pixel[1] - n * (pixel[1] - 0)),
                int(pixel[2] - n * (pixel[2] - 0))
            ) for pixel in pixelLine]


def darken(picture, n):
    return tuple(map(help_darken, picture, itertools.repeat(n, len(picture))))


def help_lighten(pixelLine, n):
    return [(
                int(pixel[0] + n * (255 - pixel[0])),
                int(pixel[1] + n * (255 - pixel[1])),
                int(pixel[2] + n * (255 - pixel[2]))
            ) for pixel in pixelLine]


def lighten(picture, n):
    return tuple(map(help_lighten, picture, itertools.repeat(n, len(picture))))


def count_color(picture, n, rgbIndex):
    count = 0
    for pixelLine in picture:
        for pixel in pixelLine:
            if n == pixel[rgbIndex]:
                count += 1
    return count


def create_histogram(picture):
    return {
        'red': {i: count_color(picture, i, 0) for i in range(1, 255)},
        'green': {i: count_color(picture, i, 1) for i in range(1, 255)},
        'blue': {i: count_color(picture, i, 2) for i in range(1, 255)}
    }

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

FF......EE..
======================================================================
ERROR: test_rotate_left (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 'list_reverseiterator' has no len()

======================================================================
ERROR: test_rotate_left_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 'list_reverseiterator' has no len()

======================================================================
FAIL: test_create_histogram (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: {'green': {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0,[5745 chars]: 0}} != {'green': {0: 5, 255: 4}, 'blue': {0: 6, 255: 3}, 'r[15 chars]: 2}}
Diff is 14994 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: test_create_histogram_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: {'green': {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0,[5745 chars]: 0}} != {'green': {0: 4, 54: 1, 12: 1, 42: 1, 60: 1, 255: 4}[83 chars]: 3}}
Diff is 15068 characters long. Set self.maxDiff to None to see it.

----------------------------------------------------------------------
Ran 12 tests in 0.148s

FAILED (failures=2, errors=2)

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

Емануил обнови решението на 13.03.2016 01:45 (преди почти 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
import itertools


def help_invert(pixelLine):
    return [(
                int(255 - pixel[0]),
                int(255 - pixel[1]),
                int(255 - pixel[2])
            ) for pixel in pixelLine]


def invert(picture):
    return tuple(map(help_invert, picture))


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


def rotate_left(picture):
    return reversed(list(zip(*picture[::1])))


def help_darken(pixelLine, n):
    return [(
                int(pixel[0] - n * (pixel[0] - 0)),
                int(pixel[1] - n * (pixel[1] - 0)),
                int(pixel[2] - n * (pixel[2] - 0))
            ) for pixel in pixelLine]


def darken(picture, n):
    return tuple(map(help_darken, picture, itertools.repeat(n, len(picture))))


def help_lighten(pixelLine, n):
    return [(
                int(pixel[0] + n * (255 - pixel[0])),
                int(pixel[1] + n * (255 - pixel[1])),
                int(pixel[2] + n * (255 - pixel[2]))
            ) for pixel in pixelLine]


def lighten(picture, n):
    return tuple(map(help_lighten, picture, itertools.repeat(n, len(picture))))


def count_color(picture, n, rgbIndex):
    count = 0
    for pixelLine in picture:
        for pixel in pixelLine:
            if n == pixel[rgbIndex]:
                count += 1
    return count


def create_histogram(picture):
    return {
        'red': {i: count_color(picture, i, 0) for i in range(1, 255)},
        'green': {i: count_color(picture, i, 1) for i in range(1, 255)},
        'blue': {i: count_color(picture, i, 2) for i in range(1, 255)}
    }