Решение на Работа с картинки от Тодор Димов

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

Към профила на Тодор Димов

Резултати

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

Код

def rotate_left(source):
new_image = [
[row[-ind] for row in source]
for ind in sorted(range(1, len(source) + 1))
]
return new_image
def rotate_right(source):
new_image = [
[row[ind] for row in reversed(source)]
for ind in range(len(source))
]
return new_image
def invert(source):
new_image = [
[
(255 - red, 255 - green, 255 - blue)
for red, green, blue in row
]
for row in source
]
return new_image
def lighten(source, factor):
new_image = [
[
(
red + int(factor * (255 - red)),
green + int(factor * (255 - green)),
blue + int(factor * (255 - blue))
)
for red, green, blue in row
]
for row in source
]
return new_image
def darken(source, factor):
new_image = [
[
(
red - int(factor * red),
green - int(factor * green),
blue - int(factor * blue)
)
for red, green, blue in row
]
for row in source
]
return new_image
def create_histogram(source):
reds = [red for row in source for (red, green, blue) in row]
greens = [green for row in source for (red, green, blue) in row]
blues = [blue for row in source for (red, green, blue) in row]
histogram = {
'red': colour_values(reds),
'green': colour_values(greens),
'blue': colour_values(blues)
}
return histogram
def colour_values(values):
colour = {}
for value in values:
if value in colour:
colour[value] += 1
else:
colour[value] = 1
return colour

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

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

OK

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

Тодор обнови решението на 09.03.2016 22:53 (преди над 5 години)

+def rotate_left(source):
+ new_image = [
+ [row[-ind] for row in source]
+ for ind in sorted(range(1, len(source) + 1))]
+ return new_image
+
+
+def rotate_right(source):
+ new_image = [
+ [row[ind] for row in reversed(source)]
+ for ind in range(len(source))]
+ return new_image
+
+
+def invert(source):
+ new_image = [
+ [(255 - red, 255 - green, 255 - blue)
+ for red, green, blue in row]
+ for row in source]
+ return new_image
+
+
+def lighten(source, factor):
+ new_image = [
+ [(red + int(factor * (255 - red)),
+ green + int(factor * (255 - green)),
+ blue + int(factor * (255 - blue)))
+ for red, green, blue in row]
+ for row in source]
+ return new_image
+
+
+def darken(source, factor):
+ new_image = [
+ [(red - int(factor * red),
+ green - int(factor * green),
+ blue - int(factor * blue)) for red, green, blue in row]
+ for row in source]
+ return new_image
+
+
+def create_histogram(source):
+ histogram = {'red': {}, 'green': {}, 'blue': {}}
+ for row in source:
+ for red, green, blue in row:
+
+ if red in histogram['red']:
+ histogram['red'][red] += 1
+ else:
+ histogram['red'][red] = 1
+
+ if green in histogram['green']:
+ histogram['green'][green] += 1
+ else:
+ histogram['green'][green] = 1
+
+ if blue in histogram['blue']:
+ histogram['blue'][blue] += 1
+ else:
+ histogram['blue'][blue] = 1
+
+ return histogram
  • Индентацията на многоредовите ти comprehension-и не е много ок. Виждам, че pep8 tool-а не се кара за това, но погледни секцията за индентация в PEP8 и ще намериш частта за списъци.
  • Вложените for-ове и дългия if/else чаршаф могат да се преобразят в доста по-стегнат и приятен за гледане код.

Тодор обнови решението на 10.03.2016 21:25 (преди над 5 години)

def rotate_left(source):
new_image = [
- [row[-ind] for row in source]
- for ind in sorted(range(1, len(source) + 1))]
+ [row[-ind] for row in source]
+ for ind in sorted(range(1, len(source) + 1))
+ ]
return new_image
def rotate_right(source):
new_image = [
- [row[ind] for row in reversed(source)]
- for ind in range(len(source))]
+ [row[ind] for row in reversed(source)]
+ for ind in range(len(source))
+ ]
return new_image
def invert(source):
new_image = [
- [(255 - red, 255 - green, 255 - blue)
- for red, green, blue in row]
- for row in source]
+ [
+ (255 - red, 255 - green, 255 - blue)
+ for red, green, blue in row
+ ]
+ for row in source
+ ]
return new_image
def lighten(source, factor):
new_image = [
- [(red + int(factor * (255 - red)),
- green + int(factor * (255 - green)),
- blue + int(factor * (255 - blue)))
- for red, green, blue in row]
- for row in source]
+ [
+ (
+ red + int(factor * (255 - red)),
+ green + int(factor * (255 - green)),
+ blue + int(factor * (255 - blue))
+ )
+ for red, green, blue in row
+ ]
+ for row in source
+ ]
return new_image
def darken(source, factor):
new_image = [
- [(red - int(factor * red),
- green - int(factor * green),
- blue - int(factor * blue)) for red, green, blue in row]
- for row in source]
+ [
+ (
+ red - int(factor * red),
+ green - int(factor * green),
+ blue - int(factor * blue)
+ )
+ for red, green, blue in row
+ ]
+ for row in source
+ ]
return new_image
def create_histogram(source):
- histogram = {'red': {}, 'green': {}, 'blue': {}}
- for row in source:
- for red, green, blue in row:
+ reds = [red for row in source for (red, green, blue) in row]
+ greens = [green for row in source for (red, green, blue) in row]
+ blues = [blue for row in source for (red, green, blue) in row]
- if red in histogram['red']:
- histogram['red'][red] += 1
- else:
- histogram['red'][red] = 1
+ histogram = {
+ 'red': colour_values(reds),
+ 'green': colour_values(greens),
+ 'blue': colour_values(blues)
+ }
- if green in histogram['green']:
- histogram['green'][green] += 1
- else:
- histogram['green'][green] = 1
+ return histogram
- if blue in histogram['blue']:
- histogram['blue'][blue] += 1
- else:
- histogram['blue'][blue] = 1
- return histogram
+def colour_values(values):
+ colour = {}
+ for value in values:
+ if value in colour:
+ colour[value] += 1
+ else:
+ colour[value] = 1
+ return colour