Python generator1 + generator2

Example problem

Solution

class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
grid_r = ['0'+''.join(str(r) for r in row)+'0' for row in grid]
grid_c = ['0'+''.join(str(c) for c in col)+'0' for col in zip(*grid)]
return sum(row.count('01')+row.count('10') for row in grid_r+grid_c)
from itertools import chain
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
grid_r = ('0'+''.join(str(r) for r in row)+'0' for row in grid)
grid_c = ('0'+''.join(str(c) for c in col)+'0' for col in zip(*grid))
return sum(row.count('01')+row.count('10')
for row in chain(grid_r, grid_c))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store