Решение на Работа с картинки от Даниел Делчев

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

Към профила на Даниел Делчев

Резултати

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

Код

def reverse(matrix_line):
length = len(matrix_line)
return[matrix_line[length-1-x] for x in range(0, length)]
def reverse_lines(matrix):
length = len(matrix)
return [reverse(matrix[x]) for x in range(0, length)]
def transponate(matrix):
lines_count = len(matrix)
rows_count = len(matrix[0])
return [[matrix[i][j] for i in range(0, lines_count)] for j in
range(0, rows_count)]
def rotate_right(matrix):
return reverse_lines(transponate(matrix))
def rotate_left(matrix):
return transponate(reverse_lines(matrix))
def invert_pixel(pixel):
red = pixel[0]
green = pixel[1]
blue = pixel[2]
return 255 - red, 255 - green, 255 - blue
def invert_line(matrix_line):
return[invert_pixel(matrix_line[x]) for x in range(0, len(matrix_line))]
def invert(matrix):
return [invert_line(matrix[x]) for x in range(0, len(matrix))]
def darken_pixel(pixel, coefficient):
red = pixel[0]
green = pixel[1]
blue = pixel[2]
darkened_red = red - coefficient * (red - 0)
darkened_green = green - coefficient * (green - 0)
darkened_blue = blue - coefficient * (blue - 0)
return int(darkened_red), int(darkened_green), int(darkened_blue)
def lighten_pixel(pixel, coefficient):
red = pixel[0]
green = pixel[1]
blue = pixel[2]
lightened_red = red + coefficient * (255 - red)
lightened_green = green + coefficient * (255 - green)
lightened_blue = blue + coefficient * (255 - blue)
return int(lightened_red), int(lightened_green), int(lightened_blue)
def alter_line(matrix_line, function, coefficient):
length = len(matrix_line)
return [function(matrix_line[x], coefficient) for x in range(0, length)]
def lighten(matrix, coefficient):
return [alter_line(matrix[x], lighten_pixel, coefficient) for x in
range(0, len(matrix))]
def darken(matrix, coefficient):
return [alter_line(matrix[x], darken_pixel, coefficient) for x in
range(0, len(matrix))]
def create_histogram(matrix):
lines_count = len(matrix)
rows_count = len(matrix[0])
red_values = {matrix[i][j][0]: 0 for j in range(0, rows_count) for i
in range(0, lines_count)}
green_values = {matrix[i][j][1]: 0 for j in range(0, rows_count) for i
in range(0, lines_count)}
blue_values = {matrix[i][j][2]: 0 for j in range(0, rows_count) for i
in range(0, lines_count)}
for i in range(0, lines_count):
for j in range(0, rows_count):
pixel = matrix[i][j]
red_values[pixel[0]] += 1
green_values[pixel[1]] += 1
blue_values[pixel[2]] += 1
return {'red': red_values, 'green': green_values, 'blue': blue_values}

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.085s

OK

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

Даниел обнови решението на 09.03.2016 23:58 (преди над 5 години)

+def reverse(matrix_line):
+ result = []
+ length = len(matrix_line)
+ [result.append(matrix_line[length-1-x]) for x in range(0, length)]
+ return result
+
+
+def reverse_lines(matrix):
+ return [reverse(matrix[x]) for x in range(0, len(matrix))]
+
+
+def transponate(matrix):
+ result = []
+ lines_count = len(matrix)
+ rows_count = len(matrix[0])
+ rows_iterator = 0
+ while rows_iterator < rows_count:
+ lines_iterator = 0
+ list_to_add = []
+ while lines_iterator < lines_count:
+ list_to_add.append(matrix[lines_iterator][rows_iterator])
+ lines_iterator += 1
+ result.append(list_to_add)
+ rows_iterator += 1
+ return result
+
+
+def rotate_right(matrix):
+ result = matrix
+ result = transponate(result)
+ result = reverse_lines(result)
+ return result
+
+
+def rotate_left(matrix):
+ result = matrix
+ result = reverse_lines(result)
+ result = transponate(result)
+ return result
+
+
+def invert_pixel(pixel):
+ red = pixel[0]
+ green = pixel[1]
+ blue = pixel[2]
+ return 255 - red, 255 - green, 255 - blue
+
+
+def invert_line(matrix_line):
+ length = len(matrix_line)
+ result = []
+ [result.append(invert_pixel(matrix_line[x])) for x in range(0, length)]
+ return result
+
+
+def invert(matrix):
+ result = []
+ length = len(matrix)
+ [result.append(invert_line(matrix[x])) for x in range(0, length)]
+ return result
+
+
+def darken_pixel(pixel, coefficient):
+ red = pixel[0]
+ green = pixel[1]
+ blue = pixel[2]
+ darkened_red = red - coefficient * (red - 0)
+ darkened_green = green - coefficient * (green - 0)
+ darkened_blue = blue - coefficient * (blue - 0)
+ return int(darkened_red), int(darkened_green), int(darkened_blue)
+
+
+def lighten_pixel(pixel, coefficient):
+ red = pixel[0]
+ green = pixel[1]
+ blue = pixel[2]
+ lightened_red = red + coefficient * (255 - red)
+ lightened_green = green + coefficient * (255 - green)
+ lightened_blue = blue + coefficient * (255 - blue)
+ return int(lightened_red), int(lightened_green), int(lightened_blue)
+
+
+def alter_line(matrix_line, function, coefficient):
+ length = len(matrix_line)
+ result = []
+ [result.append(function(matrix_line[x], coefficient))
+ for x in range(0, length)]
+ return result
+
+
+def lighten(matrix, coefficient):
+ result = []
+ length = len(matrix)
+ [result.append(alter_line(matrix[x], lighten_pixel, coefficient))
+ for x in range(0, length)]
+ return result
+
+
+def darken(matrix, coefficient):
+ result = []
+ length = len(matrix)
+ [result.append(alter_line(matrix[x], darken_pixel, coefficient))
+ for x in range(0, length)]
+ return result
+
+
+def create_histogram(matrix):
+ red_values = {}
+ green_values = {}
+ blue_values = {}
+ lines_count = len(matrix)
+ rows_count = len(matrix[0])
+ for i in range(0, lines_count):
+ for j in range(0, rows_count):
+ pixel = matrix[i][j]
+ if pixel[0] in red_values:
+ red_values[pixel[0]] += 1
+ else:
+ red_values[pixel[0]] = 1
+ if pixel[1] in green_values:
+ green_values[pixel[1]] += 1
+ else:
+ green_values[pixel[1]] = 1
+ if pixel[2] in blue_values:
+ blue_values[pixel[2]] += 1
+ else:
+ blue_values[pixel[2]] = 1
+ return {'red': red_values, 'green': green_values, 'blue': blue_values}
  • Ползваш list comprehension-и където трябва, но по много, много грешен начин. Те буквално създават нови списъци, а не са просто fancy нотация за постфиксен for цикъл. Разгледай пак лекцията за колекции, за да схванеш какво точно правят.
  • Вложените for-ове и if/else върволицата могат да се пренапишат много по-стегнато.
  • В invert и rotate_{left,right} вместо да сменяш стойността на result няколко пъти можеш да направиш всичко на един ред.

Даниел обнови решението на 10.03.2016 16:49 (преди над 5 години)

def reverse(matrix_line):
- result = []
length = len(matrix_line)
- [result.append(matrix_line[length-1-x]) for x in range(0, length)]
- return result
+ return[matrix_line[length-1-x] for x in range(0, length)]
def reverse_lines(matrix):
- return [reverse(matrix[x]) for x in range(0, len(matrix))]
+ length = len(matrix)
+ return [reverse(matrix[x]) for x in range(0, length)]
def transponate(matrix):
- result = []
lines_count = len(matrix)
rows_count = len(matrix[0])
- rows_iterator = 0
- while rows_iterator < rows_count:
- lines_iterator = 0
- list_to_add = []
- while lines_iterator < lines_count:
- list_to_add.append(matrix[lines_iterator][rows_iterator])
- lines_iterator += 1
- result.append(list_to_add)
- rows_iterator += 1
- return result
+ return [[matrix[i][j] for i in range(0, lines_count)] for j in
+ range(0, rows_count)]
def rotate_right(matrix):
- result = matrix
- result = transponate(result)
- result = reverse_lines(result)
- return result
+ return reverse_lines(transponate(matrix))
def rotate_left(matrix):
- result = matrix
- result = reverse_lines(result)
- result = transponate(result)
- return result
+ return transponate(reverse_lines(matrix))
def invert_pixel(pixel):
red = pixel[0]
green = pixel[1]
blue = pixel[2]
return 255 - red, 255 - green, 255 - blue
def invert_line(matrix_line):
- length = len(matrix_line)
- result = []
- [result.append(invert_pixel(matrix_line[x])) for x in range(0, length)]
- return result
+ return[invert_pixel(matrix_line[x]) for x in range(0, len(matrix_line))]
def invert(matrix):
- result = []
- length = len(matrix)
- [result.append(invert_line(matrix[x])) for x in range(0, length)]
- return result
+ return [invert_line(matrix[x]) for x in range(0, len(matrix))]
def darken_pixel(pixel, coefficient):
red = pixel[0]
green = pixel[1]
blue = pixel[2]
darkened_red = red - coefficient * (red - 0)
darkened_green = green - coefficient * (green - 0)
darkened_blue = blue - coefficient * (blue - 0)
return int(darkened_red), int(darkened_green), int(darkened_blue)
def lighten_pixel(pixel, coefficient):
red = pixel[0]
green = pixel[1]
blue = pixel[2]
lightened_red = red + coefficient * (255 - red)
lightened_green = green + coefficient * (255 - green)
lightened_blue = blue + coefficient * (255 - blue)
return int(lightened_red), int(lightened_green), int(lightened_blue)
def alter_line(matrix_line, function, coefficient):
length = len(matrix_line)
- result = []
- [result.append(function(matrix_line[x], coefficient))
- for x in range(0, length)]
- return result
+ return [function(matrix_line[x], coefficient) for x in range(0, length)]
def lighten(matrix, coefficient):
- result = []
- length = len(matrix)
- [result.append(alter_line(matrix[x], lighten_pixel, coefficient))
- for x in range(0, length)]
- return result
+ return [alter_line(matrix[x], lighten_pixel, coefficient) for x in
+ range(0, len(matrix))]
def darken(matrix, coefficient):
- result = []
- length = len(matrix)
- [result.append(alter_line(matrix[x], darken_pixel, coefficient))
- for x in range(0, length)]
- return result
+ return [alter_line(matrix[x], darken_pixel, coefficient) for x in
+ range(0, len(matrix))]
def create_histogram(matrix):
- red_values = {}
- green_values = {}
- blue_values = {}
lines_count = len(matrix)
rows_count = len(matrix[0])
+ red_values = {matrix[i][j][0]: 0 for j in range(0, rows_count) for i
+ in range(0, lines_count)}
+ green_values = {matrix[i][j][1]: 0 for j in range(0, rows_count) for i
+ in range(0, lines_count)}
+ blue_values = {matrix[i][j][2]: 0 for j in range(0, rows_count) for i
+ in range(0, lines_count)}
for i in range(0, lines_count):
for j in range(0, rows_count):
pixel = matrix[i][j]
- if pixel[0] in red_values:
- red_values[pixel[0]] += 1
- else:
- red_values[pixel[0]] = 1
- if pixel[1] in green_values:
- green_values[pixel[1]] += 1
- else:
- green_values[pixel[1]] = 1
- if pixel[2] in blue_values:
- blue_values[pixel[2]] += 1
- else:
- blue_values[pixel[2]] = 1
+ red_values[pixel[0]] += 1
+ green_values[pixel[1]] += 1
+ blue_values[pixel[2]] += 1
return {'red': red_values, 'green': green_values, 'blue': blue_values}