Решение на Работа с картинки от Мартин Стоев

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

Към профила на Мартин Стоев

Резултати

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

Код

from itertools import chain
def rotate_left(image):
return list(zip(*image))[::-1]
def rotate_right(image):
return list(zip(*image[::-1]))
def reducer(image, function):
unpacked = list(chain(*image))
unpacked = [tuple(map(function, x)) for x in unpacked]
result = [[] for x in range(len(image))]
for x in range(len(image)):
for y in range(len(image[0])):
result[x].append(unpacked[x * len(image[0]) + y])
return result
def invert(image):
return reducer(image, lambda x: 255 - x)
def lighten(image, coefficient):
return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))
def darken(image, coefficient):
return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))
def create_histogram(image):
rgb = {'red': 0, 'green': 1, 'blue': 2}
histogram = {'red': {}, 'green': {}, 'blue': {}}
unpacked = list(zip(*list(chain(*image))))
for key, value in rgb.items():
for element in unpacked[value]:
histogram[key][element] = histogram[key].get(element, 0) + 1
return histogram

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.102s

OK

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

Мартин обнови решението на 10.03.2016 17:11 (преди над 5 години)

+def rotate_left(image):
+ temp_image = [[] for x in range(len(image[0]))]
+ rows = len(image)
+ columns = len(image[0])
+ [[temp_image[columns - 1 - y].append(image[x][y])
+ for y in range(columns)]
+ for x in range(rows)]
+ return temp_image
+
+
+def rotate_right(image):
+ temp_image = [[] for x in range(len(image[0]))]
+ rows = len(image)
+ columns = len(image[0])
+ [[temp_image[y].append(image[rows - 1 - x][y])
+ for y in range(columns)]
+ for x in range(rows)]
+ return temp_image
+
+
+def invert(image):
+ rows = len(image)
+ columns = len(image[0])
+ temp_image = [[] for x in range(rows)]
+ [[temp_image[x].append(tuple(map(lambda x: 255 - x, image[x][y])))
+ for y in range(columns)]
+ for x in range(rows)]
+ return temp_image
+
+
+def lighten(image, coefficient):
+ rows = len(image)
+ columns = len(image[0])
+ temp_image = [[] for x in range(rows)]
+ [[temp_image[x].append(tuple(
+ map(lambda x: (int)(x + coefficient * (255 - x)), image[x][y])))
+ for y in range(columns)]
+ for x in range(rows)]
+ return temp_image
+
+
+def darken(image, coefficient):
+ rows = len(image)
+ columns = len(image[0])
+ temp_image = [[] for x in range(rows)]
+ [[temp_image[x].append(tuple(
+ map(lambda x: (int)(x - coefficient * (0 + x)), image[x][y])))
+ for y in range(columns)]
+ for x in range(rows)]
+ return temp_image
+
+
+def create_histogram(image):
+ histogram = dict()
+ histogram['red'] = dict()
+ histogram['blue'] = dict()
+ histogram['green'] = dict()
+ for x in range(len(image)):
+ for y in range(len(image[0])):
+ if image[x][y][0] not in histogram['red']:
+ histogram['red'][image[x][y][0]] = 1
+ else:
+ histogram['red'][image[x][y][0]] += 1
+ if image[x][y][1] not in histogram['green']:
+ histogram['green'][image[x][y][1]] = 1
+ else:
+ histogram['green'][image[x][y][1]] += 1
+ if image[x][y][2] not in histogram['blue']:
+ histogram['blue'][image[x][y][2]] = 1
+ else:
+ histogram['blue'][image[x][y][2]] += 1
+ return histogram
  • rotate_left и rotate_right са почти еднакви. Същото важи и за invert, lighten, darken. Помисли как да избегнеш това.
  • Ако стигнеш до нещо такова, значи list comprehension не е подходящото решение:
    [[temp_image[y].append(image[rows - 1 - x][y])
      for y in range(columns)]
     for x in range(rows)]
  • В този ред на мисли, идентацията на тези list comprehension-и са неконсистентни.
  • temp_* не е добро име за променлива. Всяка променлива в твоя код е "temp"
  • Нямаш добра причина не да създаваш празен речник с литерала {}. Винаги го предпочитай пред dict()
  • Трите if-else блока за red, green и blue са следствие от copy -> paste. Представи си, че цветовете в един пиксел не са 3, а 300. Как ги избегнал 300 if-else-a?

Мартин обнови решението на 12.03.2016 17:25 (преди над 5 години)

+from itertools import chain
+
+
def rotate_left(image):
- temp_image = [[] for x in range(len(image[0]))]
- rows = len(image)
- columns = len(image[0])
- [[temp_image[columns - 1 - y].append(image[x][y])
- for y in range(columns)]
- for x in range(rows)]
- return temp_image
+ return list(zip(*image))[::-1]
def rotate_right(image):
- temp_image = [[] for x in range(len(image[0]))]
- rows = len(image)
- columns = len(image[0])
- [[temp_image[y].append(image[rows - 1 - x][y])
- for y in range(columns)]
- for x in range(rows)]
- return temp_image
+ return list(zip(*image[::-1]))
+def reducer(image, function):
+ unpacked = list(chain(*image))
+ unpacked = [tuple(map(function, x)) for x in unpacked]
+ result = [[] for x in range(len(image))]
+ for x in range(len(image)):
+ for y in range(len(image[0])):
+ result[x].append(unpacked[x*len(image) + y])
+ return result
+
+
def invert(image):
- rows = len(image)
- columns = len(image[0])
- temp_image = [[] for x in range(rows)]
- [[temp_image[x].append(tuple(map(lambda x: 255 - x, image[x][y])))
- for y in range(columns)]
- for x in range(rows)]
- return temp_image
+ return reducer(image, lambda x: 255 - x)
def lighten(image, coefficient):
- rows = len(image)
- columns = len(image[0])
- temp_image = [[] for x in range(rows)]
- [[temp_image[x].append(tuple(
- map(lambda x: (int)(x + coefficient * (255 - x)), image[x][y])))
- for y in range(columns)]
- for x in range(rows)]
- return temp_image
+ return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))
def darken(image, coefficient):
- rows = len(image)
- columns = len(image[0])
- temp_image = [[] for x in range(rows)]
- [[temp_image[x].append(tuple(
- map(lambda x: (int)(x - coefficient * (0 + x)), image[x][y])))
- for y in range(columns)]
- for x in range(rows)]
- return temp_image
+ return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))
def create_histogram(image):
- histogram = dict()
- histogram['red'] = dict()
- histogram['blue'] = dict()
- histogram['green'] = dict()
- for x in range(len(image)):
- for y in range(len(image[0])):
- if image[x][y][0] not in histogram['red']:
- histogram['red'][image[x][y][0]] = 1
- else:
- histogram['red'][image[x][y][0]] += 1
- if image[x][y][1] not in histogram['green']:
- histogram['green'][image[x][y][1]] = 1
- else:
- histogram['green'][image[x][y][1]] += 1
- if image[x][y][2] not in histogram['blue']:
- histogram['blue'][image[x][y][2]] = 1
- else:
- histogram['blue'][image[x][y][2]] += 1
+ rows = len(image)
+ columns = len(image[0])
+ hui = {'red': 0, 'green': 1, 'blue': 2}
+ histogram = {'red': {}, 'blue': {}, 'green': {}}
+ for x in range(rows):
+ for y in range(columns):
+ for hist, count in histogram.items():
+ if image[x][y][hui[hist]] not in count:
+ count[image[x][y][hui[hist]]] = 1
+ else:
+ count[image[x][y][hui[hist]]] += 1
return histogram

Мартин обнови решението на 12.03.2016 19:03 (преди над 5 години)

from itertools import chain
def rotate_left(image):
return list(zip(*image))[::-1]
def rotate_right(image):
return list(zip(*image[::-1]))
def reducer(image, function):
unpacked = list(chain(*image))
unpacked = [tuple(map(function, x)) for x in unpacked]
result = [[] for x in range(len(image))]
for x in range(len(image)):
for y in range(len(image[0])):
result[x].append(unpacked[x*len(image) + y])
return result
def invert(image):
return reducer(image, lambda x: 255 - x)
def lighten(image, coefficient):
return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))
def darken(image, coefficient):
return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))
def create_histogram(image):
rows = len(image)
columns = len(image[0])
- hui = {'red': 0, 'green': 1, 'blue': 2}
+ rgb = {'red': 0, 'green': 1, 'blue': 2}
histogram = {'red': {}, 'blue': {}, 'green': {}}
for x in range(rows):
for y in range(columns):
for hist, count in histogram.items():
- if image[x][y][hui[hist]] not in count:
- count[image[x][y][hui[hist]]] = 1
+ if image[x][y][rgb[hist]] not in count:
+ count[image[x][y][rgb[hist]]] = 1
else:
- count[image[x][y][hui[hist]]] += 1
+ count[image[x][y][rgb[hist]]] += 1
return histogram

Мартин обнови решението на 12.03.2016 20:09 (преди над 5 години)

from itertools import chain
def rotate_left(image):
return list(zip(*image))[::-1]
def rotate_right(image):
return list(zip(*image[::-1]))
def reducer(image, function):
unpacked = list(chain(*image))
unpacked = [tuple(map(function, x)) for x in unpacked]
result = [[] for x in range(len(image))]
for x in range(len(image)):
for y in range(len(image[0])):
- result[x].append(unpacked[x*len(image) + y])
+ result[x].append(unpacked[x * len(image[0]) + y])
return result
def invert(image):
return reducer(image, lambda x: 255 - x)
def lighten(image, coefficient):
return reducer(image, lambda x: (int)(x + coefficient * (255 - x)))
def darken(image, coefficient):
return reducer(image, lambda x: (int)(x - coefficient * (0 + x)))
def create_histogram(image):
- rows = len(image)
- columns = len(image[0])
rgb = {'red': 0, 'green': 1, 'blue': 2}
- histogram = {'red': {}, 'blue': {}, 'green': {}}
- for x in range(rows):
- for y in range(columns):
- for hist, count in histogram.items():
- if image[x][y][rgb[hist]] not in count:
- count[image[x][y][rgb[hist]]] = 1
- else:
- count[image[x][y][rgb[hist]]] += 1
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+ unpacked = list(zip(*list(chain(*image))))
+ for key, value in rgb.items():
+ for element in unpacked[value]:
+ histogram[key][element] = histogram[key].get(element, 0) + 1
return histogram