timeit

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

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

Решение на Работа с картинки от Александрина Ламбова

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

Към профила на Александрина Ламбова

Резултати

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

Код

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
def rotate_left(image):
    new_image = [[] for item in image[0]]
    for row in image:
        count = len(image) - 1
        for pixel in row:
            new_image[count].append(pixel)
            count = count - 1
    return new_image


def rotate_right(image):
    new_image = [[] for item in image[0]]
    for i in range(0, len(image)):
        count = 0
        for pixel in image[len(image)-i-1]:
            new_image[count].append(pixel)
            count += 1
    return new_image


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


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


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


def create_histogram(image):
    histogram = {"red": {}, "green": {}, "blue": {}}
    for row in image:
        for pixel in row:
            histogram["red"].setdefault(pixel[0], 0)
            histogram["red"][pixel[0]] += 1
            histogram["green"].setdefault(pixel[1], 0)
            histogram["green"][pixel[1]] += 1
            histogram["blue"].setdefault(pixel[2], 0)
            histogram["blue"][pixel[2]] += 1
    return histogram

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

..FF.....F..
======================================================================
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: 255 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: 255 not less than or equal to 1

======================================================================
FAIL: 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
AssertionError: Tuples differ: (90, 0, 255) != (0, 12, 255)

First differing element 0:
90
0

- (90, 0, 255)
?  -   ^

+ (0, 12, 255)
?     ^^


----------------------------------------------------------------------
Ran 12 tests in 0.087s

FAILED (failures=3)

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

Александрина обнови решението на 10.03.2016 22:58 (преди почти 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
def rotate_left(image):
    rotated = [[] for item in image[0]]
    for row in image:
        count = len(image)-1
        for pixel in row:
            rotated[count].append(pixel)
            count = count-1
    return rotated


def rotate_right(image):
    rotated = [[] for item in image[0]]
    for i in range(0, len(image)):
        count = 0
        for pixel in image[len(image)-i-1]:
            rotated[count].append(pixel)
            count += 1
    return rotated


def invert(image):
    inverted = [[] for row in image]
    for i in range(len(image)):
        for pixel in image[i]:
            inverted[i].append((255-pixel[0],
                                255-pixel[1],
                                255-pixel[2]))
    return inverted


def lighten(image, k):
    new = [[] for row in image]
    for i in range(len(image)):
        for pixel in image[i]:
            new[i].append(((int)(pixel[0]+k*(255-pixel[0])),
                           (int)(pixel[1]+k*(255-pixel[1])),
                           (int)(pixel[2]+k*(255-pixel[2]))))
    return new


def darken(image, k):
    new = [[] for row in image]
    for i in range(len(image)):
        for pixel in image[i]:
            for color in pixel:
                new[i].append(((int)(pixel[0]-k*pixel[0]),
                               (int)(pixel[1]-k*pixel[1]),
                               (int)(pixel[2]-k*pixel[2])))
    return new


def create_histogram(image):
    histogram = {"red": {}, "green": {}, "blue": {}}
    for row in image:
        for pixel in row:
            histogram["red"].setdefault(pixel[0], 0)
            histogram["red"][pixel[0]] += 1
            histogram["green"].setdefault(pixel[1], 0)
            histogram["green"][pixel[1]] += 1
            histogram["blue"].setdefault(pixel[2], 0)
            histogram["blue"][pixel[2]] += 1
    return histogram
  • Не ти ли прилича малко на C++ с тези вложени цикли? Мини пак през лекцията за колекции и виж дали не можеш да измислиш нещо по-хитро
  • Дали пък няма колекция, която ти дава стойности по подразбиране за речник и ще ти спести малко код в create_histogram :)
  • new не е хубаво име, по-добре му дай някое по-описателно, например new_image, същото важи и за rotated
  • Оставяй интервали около операторите
  • Правилното кастването към int е int("53"), а не `(int)"53

Александрина обнови решението на 11.03.2016 22: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
def rotate_left(image):
    new_image = [[] for item in image[0]]
    for row in image:
        count = len(image) - 1
        for pixel in row:
            new_image[count].append(pixel)
            count = count - 1
    return new_image


def rotate_right(image):
    new_image = [[] for item in image[0]]
    for i in range(0, len(image)):
        count = 0
        for pixel in image[len(image)-i-1]:
            new_image[count].append(pixel)
            count += 1
    return new_image


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


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


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


def create_histogram(image):
    histogram = {"red": {}, "green": {}, "blue": {}}
    for row in image:
        for pixel in row:
            histogram["red"].setdefault(pixel[0], 0)
            histogram["red"][pixel[0]] += 1
            histogram["green"].setdefault(pixel[1], 0)
            histogram["green"][pixel[1]] += 1
            histogram["blue"].setdefault(pixel[2], 0)
            histogram["blue"][pixel[2]] += 1
    return histogram