timeit

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

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

Решение на Работа с картинки от Илия Жечев

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

Към профила на Илия Жечев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 12 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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
64
65
66
67
68
69
70
71
72
def map_pixel(func, pic):
    result = []
    for row in pic:
        new_row = map(func, row)
        result.append(list(new_row))
    return result


def get_column(index, matrix):
    return [row[index] for row in matrix]


def inver_pixel(pixel):
    return tuple(map(lambda sub_pixel: 255 - sub_pixel, pixel))


def lighten_sub_pixel(sub_pixel, factor):
    return int(sub_pixel + factor * (255 - sub_pixel))


def darken_sub_pixel(sub_pixel, factor):
    return int(sub_pixel - factor * sub_pixel)


def lighten_pixel(pixel, factor):
    def lighten_map(sub_pixel):
        return lighten_sub_pixel(sub_pixel, factor)
    return tuple(map(lighten_map, pixel))


def darken_pixel(pixel, factor):
    def darken_map(sub_pixel):
        return darken_sub_pixel(sub_pixel, factor)
    return tuple(map(darken_map, pixel))


def invert(pic):
    return map_pixel(inver_pixel, pic)


def lighten(pic, factor):
    return map_pixel(lambda pixel: lighten_pixel(pixel, factor), pic)


def darken(pic, factor):
    return map_pixel(lambda pixel: darken_pixel(pixel, factor), pic)


def rotate_left(pic):
    cols = len(pic[0])
    return [list(get_column(cols - id - 1, pic)) for id in range(cols)]


def rotate_right(pic):
    cols = len(pic[0])
    return [list(reversed(get_column(id, pic))) for id in range(cols)]


color_key = {'red': 0, 'green': 1, 'blue': 2}


def add_pixel_frequency(pixel, current_hist):
    for color_name in color_key:
        sub_pixel = pixel[color_key[color_name]]
        default_value = current_hist[color_name].get(sub_pixel, 0)
        current_hist[color_name][sub_pixel] = default_value + 1


def create_histogram(pic):
    result = {key: {} for key in color_key}
    map_pixel(lambda pixel: add_pixel_frequency(pixel, result), pic)
    return result

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.084s

OK

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

Илия обнови решението на 09.03.2016 22: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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def map_pixel(func, pic):
    result = []
    for row in pic:
        new_row = map(lambda pixel: func(pixel), row)
        result.append(list(new_row))
    return result


def get_column(index, matrix):
    return [row[index] for row in matrix]


def inver_pixel(pixel):
    return tuple(map(lambda sub_pixel: 255 - sub_pixel, pixel))


def lighten_sub_pixel(sub_pixel, factor):
    return int(sub_pixel + factor * (255 - sub_pixel))


def darken_sub_pixel(sub_pixel, factor):
    return int(sub_pixel - factor * sub_pixel)


def lighten_pixel(pixel, factor):
    def mapping_lambda(sub_pixel):
        return lighten_sub_pixel(sub_pixel, factor)
    return tuple(map(mapping_lambda, pixel))


def darken_pixel(pixel, factor):
    def mapping_lambda(sub_pixel):
        return darken_sub_pixel(sub_pixel, factor)
    return tuple(map(mapping_lambda, pixel))


def invert(pic):
    return map_pixel(inver_pixel, pic)


def lighten(pic, factor):
    return map_pixel(lambda pixel: lighten_pixel(pixel, factor), pic)


def darken(pic, factor):
    return map_pixel(lambda pixel: darken_pixel(pixel, factor), pic)


def rotate_left(pic):
    cols = len(pic[0])
    return [list(get_column(cols - id - 1, pic)) for id in range(cols)]


def rotate_right(pic):
    cols = len(pic[0])
    return [list(reversed(get_column(id, pic))) for id in range(cols)]


def create_histogram(pic):
    color_key = {'red': 0, 'green': 1, 'blue': 2}
    result = {key: {} for key in color_key}
    for row in pic:
        for pixel in row:
            for color_name in result:
                sub_pixel = pixel[color_key[color_name]]
                default_value = result[color_name].get(sub_pixel, 0)
                result[color_name][sub_pixel] = default_value + 1
    return result

Илия обнови решението на 09.03.2016 22:20 (преди почти 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
64
65
66
67
68
def map_pixel(func, pic):
    result = []
    for row in pic:
        new_row = map(lambda pixel: func(pixel), row)
        result.append(list(new_row))
    return result


def get_column(index, matrix):
    return [row[index] for row in matrix]


def inver_pixel(pixel):
    return tuple(map(lambda sub_pixel: 255 - sub_pixel, pixel))


def lighten_sub_pixel(sub_pixel, factor):
    return int(sub_pixel + factor * (255 - sub_pixel))


def darken_sub_pixel(sub_pixel, factor):
    return int(sub_pixel - factor * sub_pixel)


def lighten_pixel(pixel, factor):
    def lighten_map(sub_pixel):
        return lighten_sub_pixel(sub_pixel, factor)
    return tuple(map(lighten_map, pixel))


def darken_pixel(pixel, factor):
    def darken_map(sub_pixel):
        return darken_sub_pixel(sub_pixel, factor)
    return tuple(map(darken_map, pixel))


def invert(pic):
    return map_pixel(inver_pixel, pic)


def lighten(pic, factor):
    return map_pixel(lambda pixel: lighten_pixel(pixel, factor), pic)


def darken(pic, factor):
    return map_pixel(lambda pixel: darken_pixel(pixel, factor), pic)


def rotate_left(pic):
    cols = len(pic[0])
    return [list(get_column(cols - id - 1, pic)) for id in range(cols)]


def rotate_right(pic):
    cols = len(pic[0])
    return [list(reversed(get_column(id, pic))) for id in range(cols)]


def create_histogram(pic):
    color_key = {'red': 0, 'green': 1, 'blue': 2}
    result = {key: {} for key in color_key}
    for row in pic:
        for pixel in row:
            for color_name in result:
                sub_pixel = pixel[color_key[color_name]]
                default_value = result[color_name].get(sub_pixel, 0)
                result[color_name][sub_pixel] = default_value + 1
    return result

Илия обнови решението на 12.03.2016 18: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
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
64
65
66
67
68
69
70
71
72
def map_pixel(func, pic):
    result = []
    for row in pic:
        new_row = map(lambda pixel: func(pixel), row)
        result.append(list(new_row))
    return result


def get_column(index, matrix):
    return [row[index] for row in matrix]


def inver_pixel(pixel):
    return tuple(map(lambda sub_pixel: 255 - sub_pixel, pixel))


def lighten_sub_pixel(sub_pixel, factor):
    return int(sub_pixel + factor * (255 - sub_pixel))


def darken_sub_pixel(sub_pixel, factor):
    return int(sub_pixel - factor * sub_pixel)


def lighten_pixel(pixel, factor):
    def lighten_map(sub_pixel):
        return lighten_sub_pixel(sub_pixel, factor)
    return tuple(map(lighten_map, pixel))


def darken_pixel(pixel, factor):
    def darken_map(sub_pixel):
        return darken_sub_pixel(sub_pixel, factor)
    return tuple(map(darken_map, pixel))


def invert(pic):
    return map_pixel(inver_pixel, pic)


def lighten(pic, factor):
    return map_pixel(lambda pixel: lighten_pixel(pixel, factor), pic)


def darken(pic, factor):
    return map_pixel(lambda pixel: darken_pixel(pixel, factor), pic)


def rotate_left(pic):
    cols = len(pic[0])
    return [list(get_column(cols - id - 1, pic)) for id in range(cols)]


def rotate_right(pic):
    cols = len(pic[0])
    return [list(reversed(get_column(id, pic))) for id in range(cols)]


color_key = {'red': 0, 'green': 1, 'blue': 2}


def add_pixel_frequency(pixel, current_hist):
    for color_name in color_key:
        sub_pixel = pixel[color_key[color_name]]
        default_value = current_hist[color_name].get(sub_pixel, 0)
        current_hist[color_name][sub_pixel] = default_value + 1


def create_histogram(pic):
    result = {key: {} for key in color_key}
    map_pixel(lambda pixel: add_pixel_frequency(pixel, result), pic)
    return result

Илия обнови решението на 13.03.2016 10:41 (преди почти 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
64
65
66
67
68
69
70
71
72
def map_pixel(func, pic):
    result = []
    for row in pic:
        new_row = map(func, row)
        result.append(list(new_row))
    return result


def get_column(index, matrix):
    return [row[index] for row in matrix]


def inver_pixel(pixel):
    return tuple(map(lambda sub_pixel: 255 - sub_pixel, pixel))


def lighten_sub_pixel(sub_pixel, factor):
    return int(sub_pixel + factor * (255 - sub_pixel))


def darken_sub_pixel(sub_pixel, factor):
    return int(sub_pixel - factor * sub_pixel)


def lighten_pixel(pixel, factor):
    def lighten_map(sub_pixel):
        return lighten_sub_pixel(sub_pixel, factor)
    return tuple(map(lighten_map, pixel))


def darken_pixel(pixel, factor):
    def darken_map(sub_pixel):
        return darken_sub_pixel(sub_pixel, factor)
    return tuple(map(darken_map, pixel))


def invert(pic):
    return map_pixel(inver_pixel, pic)


def lighten(pic, factor):
    return map_pixel(lambda pixel: lighten_pixel(pixel, factor), pic)


def darken(pic, factor):
    return map_pixel(lambda pixel: darken_pixel(pixel, factor), pic)


def rotate_left(pic):
    cols = len(pic[0])
    return [list(get_column(cols - id - 1, pic)) for id in range(cols)]


def rotate_right(pic):
    cols = len(pic[0])
    return [list(reversed(get_column(id, pic))) for id in range(cols)]


color_key = {'red': 0, 'green': 1, 'blue': 2}


def add_pixel_frequency(pixel, current_hist):
    for color_name in color_key:
        sub_pixel = pixel[color_key[color_name]]
        default_value = current_hist[color_name].get(sub_pixel, 0)
        current_hist[color_name][sub_pixel] = default_value + 1


def create_histogram(pic):
    result = {key: {} for key in color_key}
    map_pixel(lambda pixel: add_pixel_frequency(pixel, result), pic)
    return result