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
def rotate_left(image):
    row = len(image)
    col = len(image[0])
    return [[image[i][j] for i in range(row)] 
            for j in range(col - 1, -1, -1)]

def rotate_right(image):
    row = len(image)
    col = len(image[0])
    return [[image[i][j] for i in range(row - 1, -1, -1)] 
            for j in range(col)]

def invert(image):
    row = len(image)
    col = len(image[0])
    return [[tuple([255 - x for x in image[i][j]]) 
            for j in range(col)] for i in range(row)]

def lighten(image, index):
    row = len(image)
    col = len(image[0])
    return [[tuple([x + int(index*(255 - x)) for x in image[i][j]]) 
            for j in range(col)] for i in range(row)]

def darken(image, index):
    row = len(image)
    col = len(image[0])
    return [[tuple([x - int(index*(x - 0)) for x in image[i][j]]) 
            for j in range(col)] for i in range(row)]

def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    rows = len(image)
    columns = len(image[0])
    for i in range(rows):
        for j in range(columns):
            r, g, b = image[i][j]
            if r in histogram['red'].keys():
                histogram['red'][r] += 1
            else:
                histogram['red'][r] = 1
            if g in histogram['green'].keys():
                histogram['green'][g] += 1
            else:
                histogram['green'][g] = 1 
            if b in histogram['blue'].keys():
                histogram['blue'][b] += 1
            else:
                histogram['blue'][b] = 1 
    return histogram

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

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

OK

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

Ивета обнови решението на 12.03.2016 22:30 (преди почти 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 rotate_left(image):
    row = len(image)
    col = len(image[0])
    return [[image[i][j] for i in range(row)] 
            for j in range(col - 1, -1, -1)]

def rotate_right(image):
    row = len(image)
    col = len(image[0])
    return [[image[i][j] for i in range(row - 1, -1, -1)] 
            for j in range(col)]

def invert(image):
    row = len(image)
    col = len(image[0])
    return [[tuple([255 - x for x in image[i][j]]) 
            for j in range(col)] for i in range(row)]

def lighten(image, index):
    row = len(image)
    col = len(image[0])
    return [[tuple([x + int(index*(255 - x)) for x in image[i][j]]) 
            for j in range(col)] for i in range(row)]

def darken(image, index):
    row = len(image)
    col = len(image[0])
    return [[tuple([x - int(index*(x - 0)) for x in image[i][j]]) 
            for j in range(col)] for i in range(row)]

def create_histogram(image):
    histogram = {'red': {}, 'green': {}, 'blue': {}}
    rows = len(image)
    columns = len(image[0])
    for i in range(rows):
        for j in range(columns):
            r, g, b = image[i][j]
            if r in histogram['red'].keys():
                histogram['red'][r] += 1
            else:
                histogram['red'][r] = 1
            if g in histogram['green'].keys():
                histogram['green'][g] += 1
            else:
                histogram['green'][g] = 1 
            if b in histogram['blue'].keys():
                histogram['blue'][b] += 1
            else:
                histogram['blue'][b] = 1 
    return histogram

Само да попитам по-добър вариант ли щеше да е:

histogram = {}
list_of_separated_colours = list(zip(*(list_of_lists_to_list(image))))
histogram['red'] = Counter(list_of_separated_colours[0])
histogram['green'] = Counter(list_of_separated_colours[1])
histogram['blue'] = Counter(list_of_separated_colours[2])
return histogram

Като list_of_lists_to_list ми прави картинката на списък от tuple-и