Jun-10-2020, 02:42 PM
As a beginner, I aim to write pythonic code because that would get me motivated into doing python the pythonic way. I was doing this challenge on 2d lists (or arrays) from hackerrank, and got the answer right. But the way I wrote the code bugs me. I really did not think it was pythonic. Here is the code:
Could someone tell if it was pythonic and where I could have improved on it?
Thanks
arr = [] count = [] for _ in range(6): arr.append(list(map(int, input().strip().split()))) for i in range(4) : for j in range(4) : if arr[i][j] >= -9 and arr[i][j] <= 9 : dlist = [arr[i][j], arr[i][j+1], arr[i][j+2], arr[i+1][j+1], arr[i+2][j], arr[i+2][j+1], arr[i+2][j+2]] count.append(sum(dlist)) count.sort(reverse=True) print(count[0])The part that didn't seem right to me was line 8, dlist, initialization. Also, I thought I could do this using list comprehension, but the if conditional there made me not to go for list comprehension.
Could someone tell if it was pythonic and where I could have improved on it?
Thanks