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
37
38
39
40
41
42
43
from collections import Counter
import copy


def proces_image(image, func):
    result = copy.deepcopy(image)
    for i, row in enumerate(result):
        for j, pixel in enumerate(row):
            result[i][j] = ()
            for color in pixel:
                result[i][j] += (func(color),)
    return result


def lighten(image, n):
    return proces_image(image, lambda color:
                        int(color + n * (255 - color)))


def darken(image, n):
    return proces_image(image, lambda color:
                        int(color - n * (255 - color)))


def invert(image):
    return proces_image(image, lambda color:
                        255 - color)


def create_histogram(image):
    histogram = {}
    for i, color in enumerate(['red', 'green', 'blue']):
        histogram[color] = dict(Counter([pixel[i]
                                         for row in image for pixel in row]))
    return histogram


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


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

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

..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.088s

FAILED (failures=2)

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

Христо обнови решението на 12.03.2016 21:48 (преди почти 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
def proces_image(image, func):
        result = image[:]
        for i, row in enumerate(result):
                for j, pixel in enumerate(row):
                        result[i][j] = ()
                        for color in pixel:
                           result[i][j] += (func(color),)
        return result

def lighten(image, n):
        return proces_image(image, lambda color:
          int(color + n*(255-color)))

def darken(image, n):
        return proces_image(image, lambda color:
         int(color - n*(255-color)))

def invert(image):
        return proces_image(image, lambda color: 
                255-color)

def create_histogram(image):
        colors = ['red', 'green', 'blue'];
        histogram = {}

        for row in image:
                for pixel in row:
                        for i, value in enumerate(pixel):
                                color = colors[i]
                                if color in histogram:
                                        if value in histogram[color]:
                                                histogram[color][value] += 1
                                        else:
                                                histogram[color][value] = 1
                                else:
                                        histogram[color] = {value:1}
        return histogram
        
def rotate_left(image):
        return reversed(list(zip(*image[:])))

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

Христо обнови решението на 13.03.2016 02: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
def proces_image(image, func):
        result = image[:]
        for i, row in enumerate(result):
                for j, pixel in enumerate(row):
                        result[i][j] = ()
                        for color in pixel:
                           result[i][j] += (func(color),)
        return result

def lighten(image, n):
        return proces_image(image, lambda color:
          int(color + n*(255-color)))

def darken(image, n):
        return proces_image(image, lambda color:
         int(color - n*(255-color)))

def invert(image):
        return proces_image(image, lambda color: 
                255-color)

def create_histogram(image):
        red = [r for row in image for (r,g,b) in row]
        green = [g for row in image for (r,g,b) in row]
        blue = [b for row in image for (r,g,b) in row]
         
        histogram = {'red': dict(Counter(red)),
                     'green': dict(Counter(green)),
                     'blue' : dict(Counter(blue))}    
        return histogram
        
def rotate_left(image):
        return reversed(list(zip(*image[:])))

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

Христо обнови решението на 13.03.2016 14:26 (преди почти 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
def proces_image(image, func):
        result = image[:]
        for i, row in enumerate(result):
                for j, pixel in enumerate(row):
                        result[i][j] = ()
                        for color in pixel:
                           result[i][j] += (func(color),)
        return result

def lighten(image, n):
        return proces_image(image, lambda color:
          int(color + n*(255-color)))

def darken(image, n):
        return proces_image(image, lambda color:
         int(color - n*(255-color)))

def invert(image):
        return proces_image(image, lambda color: 
                255-color)

def create_histogram(image):
    raw = [[r for row in image for (r,g,b) in row],
            [g for row in image for (r,g,b) in row],
             [b for row in image for (r,g,b) in row]]

    counted = [dict(Counter(color)) for color in raw]
    colors = ['red', 'green', 'blue']
    
    return dict(zip(colors, counted))
        
def rotate_left(image):
        return reversed(list(zip(*image[:])))

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

Христо обнови решението на 13.03.2016 14:27 (преди почти 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
def proces_image(image, func):
        result = image[:]
        for i, row in enumerate(result):
                for j, pixel in enumerate(row):
                        result[i][j] = ()
                        for color in pixel:
                           result[i][j] += (func(color),)
        return result

def lighten(image, n):
        return proces_image(image, lambda color:
          int(color + n*(255-color)))

def darken(image, n):
        return proces_image(image, lambda color:
         int(color - n*(255-color)))

def invert(image):
        return proces_image(image, lambda color: 
                255-color)

def create_histogram(image):
        raw = [[r for row in image for (r,g,b) in row],
                [g for row in image for (r,g,b) in row],
                 [b for row in image for (r,g,b) in row]]

        counted = [dict(Counter(color)) for color in raw]
        colors = ['red', 'green', 'blue']

        return dict(zip(colors, counted))
        
def rotate_left(image):
        return reversed(list(zip(*image[:])))

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

Христо обнови решението на 13.03.2016 14: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
43
44
from collections import Counter

def proces_image(image, func):
    result = image[:]
    for i, row in enumerate(result):
        for j, pixel in enumerate(row):
            result[i][j] = ()
            for color in pixel:
                result[i][j] += (func(color),)
    return result


def lighten(image, n):
    return proces_image(image, lambda color:
                        int(color + n * (255 - color)))


def darken(image, n):
    return proces_image(image, lambda color:
                        int(color - n * (255 - color)))


def invert(image):
    return proces_image(image, lambda color:
                        255 - color)


def create_histogram(image):
    raw = [[r for row in image for (r, g, b) in row],
           [g for row in image for (r, g, b) in row],
           [b for row in image for (r, g, b) in row]]

    counted = [dict(Counter(color)) for color in raw]
    colors = ['red', 'green', 'blue']

    return dict(zip(colors, counted))


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


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

Христо обнови решението на 14.03.2016 00:35 (преди почти 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 collections import Counter
import copy


def proces_image(image, func):
    result = copy.deepcopy(image)
    for i, row in enumerate(result):
        for j, pixel in enumerate(row):
            result[i][j] = ()
            for color in pixel:
                result[i][j] += (func(color),)
    return result


def lighten(image, n):
    return proces_image(image, lambda color:
                        int(color + n * (255 - color)))


def darken(image, n):
    return proces_image(image, lambda color:
                        int(color - n * (255 - color)))


def invert(image):
    return proces_image(image, lambda color:
                        255 - color)


def create_histogram(image):
    raw = [[r for row in image for (r, g, b) in row],
           [g for row in image for (r, g, b) in row],
           [b for row in image for (r, g, b) in row]]

    counted = [dict(Counter(color)) for color in raw]
    colors = ['red', 'green', 'blue']

    return dict(zip(colors, counted))


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


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

Христо обнови решението на 14.03.2016 01:05 (преди почти 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
from collections import Counter
import copy


def proces_image(image, func):
    result = copy.deepcopy(image)
    for i, row in enumerate(result):
        for j, pixel in enumerate(row):
            result[i][j] = ()
            for color in pixel:
                result[i][j] += (func(color),)
    return result

def lighten(image, n):
    return proces_image(image, lambda color:
                        int(color + n * (255 - color)))

def darken(image, n):
    return proces_image(image, lambda color:
                        int(color - n * (255 - color)))

def invert(image):
    return proces_image(image, lambda color:
                        255 - color)

def create_histogram(image):
    histogram = {}
    for i, color in enumerate(['red', 'green', 'blue']):
        histogram[color] = dict(Counter([pixel[i]
                                         for row in image for pixel in row]))
    return histogram

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


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

Христо обнови решението на 14.03.2016 16:07 (преди почти 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
from collections import Counter
import copy


def proces_image(image, func):
    result = copy.deepcopy(image)
    for i, row in enumerate(result):
        for j, pixel in enumerate(row):
            result[i][j] = ()
            for color in pixel:
                result[i][j] += (func(color),)
    return result


def lighten(image, n):
    return proces_image(image, lambda color:
                        int(color + n * (255 - color)))


def darken(image, n):
    return proces_image(image, lambda color:
                        int(color - n * (255 - color)))


def invert(image):
    return proces_image(image, lambda color:
                        255 - color)


def create_histogram(image):
    histogram = {}
    for i, color in enumerate(['red', 'green', 'blue']):
        histogram[color] = dict(Counter([pixel[i]
                                         for row in image for pixel in row]))
    return histogram


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


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