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

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

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

Резултати

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

Код

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 (преди над 5 години)

+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 (преди над 5 години)

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):
+ def lighten_map(sub_pixel):
return lighten_sub_pixel(sub_pixel, factor)
- return tuple(map(mapping_lambda, pixel))
+ return tuple(map(lighten_map, pixel))
def darken_pixel(pixel, factor):
- def mapping_lambda(sub_pixel):
+ def darken_map(sub_pixel):
return darken_sub_pixel(sub_pixel, factor)
- return tuple(map(mapping_lambda, pixel))
+ 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 (преди над 5 години)

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):
- 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
+ map_pixel(lambda pixel: add_pixel_frequency(pixel, result), pic)
return result

Илия обнови решението на 13.03.2016 10:41 (преди над 5 години)

def map_pixel(func, pic):
result = []
for row in pic:
- new_row = map(lambda pixel: func(pixel), row)
+ 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