So, I've tried solving the following question, given to me through HackerRank:
the following HackerRank question:
Write a function
blur_image()that takes as a parameter an image in the form of a nested listAand blurs it by averaging each pixel value with four of its neighboring pixels (Top, Bottom, Right, and Left). Note: not all of the neighbors are available in boundary cases. You have to write suitable conditions accordingly.
My Solution:
import ast
A = input()
A = ast.literal_eval(A)
def blur_image(a):
result = []
for i in range(len(a)):
row = []
for j in range(len(a[i])):
total, count = a[i][j], 1
if i + 1 < len(a): total, count = total + a[i+1][j], count + 1
if j + 1 < len(a[i]): total, count = total + a[i][j+1], count + 1
if i - 1 > -1: total, count = total + a[i-1][j], count + 1
if j - 1 > -1: total, count = total + a[i][j-1], count + 1
row.append(round(total/count, 2))
result.append(row)
return result
print(blur_image(A))
I would appreciate any suggestions and advice you can give me to improve this solution. Please note that my focus is to solve this without using any modules. Thank you!