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

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

Към профила на Виктор Маринов

Резултати

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

Код

from collections import defaultdict
def rotate_left(image):
rotated_image = []
columns_count = len(image[0])
for j in range(columns_count):
new_row = []
for row in image:
new_row.append(row[j])
rotated_image.append(new_row)
return rotated_image[::-1]
def rotate_right(image):
rotated_image = []
columns_count = len(image[0])
for j in range(columns_count):
new_row = []
for row in image:
new_row.append(row[j])
rotated_image.append(new_row[::-1])
return rotated_image
def inverted_pixel(pixel):
r, g, b = pixel
return (255 - r, 255 - g, 255 - b)
def invert(image):
inverted_image = []
for row in image:
inverted_row = []
for pixel in row:
inverted_row.append(inverted_pixel(pixel))
inverted_image.append(inverted_row)
return inverted_image
def lightened_pixel(pixel, lighten_value):
r, g, b = pixel
lightened_r = int(r + lighten_value*(255-r))
lightened_g = int(g + lighten_value*(255-g))
lightened_b = int(b + lighten_value*(255-b))
return (lightened_r, lightened_g, lightened_b)
def lighten(image, lighten_coef):
lightened_image = []
for row in image:
lightened_row = []
for pixel in row:
lightened_row.append(lightened_pixel(pixel, lighten_coef))
lightened_image.append(lightened_row)
return lightened_image
def darkened_pixel(pixel, darken_coef):
r, g, b = pixel
return (r - darken_coef*r, g - darken_coef*g, b - darken_coef*b)
def darken(image, darken_coef):
darkened_image = []
for row in image:
darkened_row = []
for pixel in row:
darkened_row.append(darkened_pixel(pixel, darken_coef))
darkened_image.append(darkened_row)
return darkened_image
def create_histogram(image):
red = defaultdict(int)
green = defaultdict(int)
blue = defaultdict(int)
for row in image:
for pixel in row:
r, g, b = pixel
red[r] += 1
green[g] += 1
blue[b] += 1
return {'red': red, 'green': green, 'blue': blue}

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

............
----------------------------------------------------------------------
Ran 12 tests in 0.101s

OK

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

Виктор обнови решението на 10.03.2016 14:30 (преди над 5 години)

+from collections import Counter
+
+def rotate_left(image):
+ result = []
+ num_cols = len(image[0])
+ for j in range(num_cols):
+ new_row = []
+ for row in image:
+ new_row.append(row[j])
+ result.append(new_row)
+ return result[::-1]
+
+def rotate_right(image):
+ result = []
+ num_cols = len(image[0])
+ for j in range(num_cols):
+ new_row = []
+ for row in image:
+ new_row.append(row[j])
+ result.append(new_row[::-1])
+ return result
+
+def inverted_pixel(pixel):
+ r, g, b = pixel
+ inverted_r = 255 - r
+ inverted_g = 255 - g
+ inverted_b = 255 - b
+ return (inverted_r, inverted_g, inverted_b)
+
+def invert(image):
+ result = []
+ for row in image:
+ inverted_row = []
+ for pixel in row:
+ inverted_row.append(inverted_pixel(pixel))
+ result.append(inverted_row)
+ return result
+
+def lightened_pixel(pixel, lighten_value):
+ r, g, b = pixel
+ lightened_r = (int)(r + lighten_value*(255 - r))
+ lightened_g = (int)(g + lighten_value*(255 - g))
+ lightened_b = (int)(b + lighten_value*(255 - b))
+ return (lightened_r, lightened_g, lightened_b)
+
+
+def lighten(image, lighten_coef):
+ result = []
+ for row in image:
+ lightened_row = []
+ for pixel in row:
+ lightened_row.append(lightened_pixel(pixel, lighten_coef))
+ result.append(lightened_row)
+ return result
+
+def darkened_pixel(pixel, darken_coef):
+ r, g, b = pixel
+ darkened_r = r - darken_coef*r
+ darkened_g = g - darken_coef*g
+ darkened_b = b - darken_coef*b
+ return (darkened_r, darkened_g, darkened_b)
+
+def darken(image, darken_coef):
+ result = []
+ for row in image:
+ darkened_row = []
+ for pixel in row:
+ darkened_row.append(darkened_pixel(pixel, darken_coef))
+ result.append(darkened_row)
+ return result
+
+def create_histogram(image):
+ red = Counter()
+ green = Counter()
+ blue = Counter()
+
+ for row in image:
+ for pixel in row:
+ r, g, b = pixel
+ red[r] += 1
+ green[g] += 1
+ blue[b] += 1
+ return {'red': red, 'green': green, 'blue': blue}
  • На места има променливи, които не са ти нужни (например inverted_r, lighten_g, darken_b), без тях можеш да направиш функциите си на един ред :)
  • В create_histogram не използваш всички предимства на Counter, в момента няма разлика, ако го замениш с defaultdict
  • Погледни пак в PEP8 къде ти трябват интервали около операторите
  • Не е нужно да съкращаваш имената на променливите (num_cols)
  • В случай можеш да избереш по-описателно име от result

Аз бих го написал така: int(r + lighten_value*(255-r))

Относно интервалите:

If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator.

Пълно описание с малко примери

Обърни внимание, че не слагам скоби около int при кастването. C-style кастването работи, просто защото Python е доста толерантен към скобите и в случая (int)(...) се обръща до int(...). Поради тази причина и (print)("Hello world") работи :)

Виктор обнови решението на 13.03.2016 10:40 (преди над 5 години)

-from collections import Counter
+from collections import defaultdict
+
def rotate_left(image):
- result = []
- num_cols = len(image[0])
- for j in range(num_cols):
+ rotated_image = []
+ columns_count = len(image[0])
+ for j in range(columns_count):
new_row = []
for row in image:
new_row.append(row[j])
- result.append(new_row)
- return result[::-1]
+ rotated_image.append(new_row)
+ return rotated_image[::-1]
+
def rotate_right(image):
- result = []
- num_cols = len(image[0])
- for j in range(num_cols):
+ rotated_image = []
+ columns_count = len(image[0])
+ for j in range(columns_count):
new_row = []
for row in image:
new_row.append(row[j])
- result.append(new_row[::-1])
- return result
+ rotated_image.append(new_row[::-1])
+ return rotated_image
+
def inverted_pixel(pixel):
r, g, b = pixel
- inverted_r = 255 - r
- inverted_g = 255 - g
- inverted_b = 255 - b
- return (inverted_r, inverted_g, inverted_b)
+ return (255 - r, 255 - g, 255 - b)
+
def invert(image):
- result = []
+ inverted_image = []
for row in image:
inverted_row = []
for pixel in row:
inverted_row.append(inverted_pixel(pixel))
- result.append(inverted_row)
- return result
+ inverted_image.append(inverted_row)
+ return inverted_image
+
def lightened_pixel(pixel, lighten_value):
r, g, b = pixel
- lightened_r = (int)(r + lighten_value*(255 - r))
- lightened_g = (int)(g + lighten_value*(255 - g))
- lightened_b = (int)(b + lighten_value*(255 - b))
+ lightened_r = int(r + lighten_value*(255-r))
+ lightened_g = int(g + lighten_value*(255-g))
+ lightened_b = int(b + lighten_value*(255-b))
return (lightened_r, lightened_g, lightened_b)
def lighten(image, lighten_coef):
- result = []
+ lightened_image = []
for row in image:
lightened_row = []
for pixel in row:
lightened_row.append(lightened_pixel(pixel, lighten_coef))
- result.append(lightened_row)
- return result
+ lightened_image.append(lightened_row)
+ return lightened_image
+
def darkened_pixel(pixel, darken_coef):
r, g, b = pixel
- darkened_r = r - darken_coef*r
- darkened_g = g - darken_coef*g
- darkened_b = b - darken_coef*b
- return (darkened_r, darkened_g, darkened_b)
+ return (r - darken_coef*r, g - darken_coef*g, b - darken_coef*b)
+
def darken(image, darken_coef):
- result = []
+ darkened_image = []
for row in image:
darkened_row = []
for pixel in row:
darkened_row.append(darkened_pixel(pixel, darken_coef))
- result.append(darkened_row)
- return result
+ darkened_image.append(darkened_row)
+ return darkened_image
+
def create_histogram(image):
- red = Counter()
- green = Counter()
- blue = Counter()
+ red = defaultdict(int)
+ green = defaultdict(int)
+ blue = defaultdict(int)
for row in image:
for pixel in row:
r, g, b = pixel
red[r] += 1
green[g] += 1
blue[b] += 1
return {'red': red, 'green': green, 'blue': blue}