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))

--

--

--

Data Scientist/MLE/SWE @takemobi

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Determined Now Supports Kubernetes!

Our Blog Tracking and Interlinking System

Getting Started With Spring Boot … Part 1

Laravel Request Lifecycle

Kid’s Eye Safe Smart TV — ESP32 — HCSR04 — MIcropython — IOT

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
Jimmy Shen

Jimmy Shen

Data Scientist/MLE/SWE @takemobi

More from Medium

ZigZag String Conversion in python

Pytest in Python…

SciPy-Tutorial

Python functional programming processing functions: map () , Filter () , reduce ()