# 4 lines

• ``````def maxAreaOfIsland(self, grid):
grid = {i + j*1j: val for i, row in enumerate(grid) for j, val in enumerate(row)}
def area(z):
return grid.pop(z, 0) and 1 + sum(area(z + 1j**k) for k in range(4))
return max(map(area, set(grid)))
``````

Complex numbers making things simple...

• @StefanPochmann Really like your idea of using complex numbers which greatly shortens the code.

I have a question, though: for integers `a` and `b`: is

``````x = a and b
``````

the same as

``````if a:
x = b
else:
x = 0
``````

?

Or if you could explain how in general `and` operator works in such case that would be very helpful. Thanks!

• @flamesofmoon I think the documentation explains it well, have you read it yet?

• @StefanPochmann You are right! Thanks for answering.

• @flamesofmoon Your question is still a little tricky, though. What do you think now, is it the same as that if-else?

• @StefanPochmann In the integer case, yes. In general, if I get it right,

``````x = a and b
``````

is equivalent to

``````if a:
x = b
else:
x = a
``````

• @flamesofmoon Just a liiiiittle remaining issue: I'd say long integers are integers, so there can be a difference:

``````>>> a = 0L
>>> x1 = a and b
>>> if a:
x2 = b
else:
x2 = 0

>>> x1 is x2
False
``````

I do need to use `0L` there, it doesn't work with `0` because that's among the few cached integers and I appear to be unable to create two different `0`-objects. But that might be just a CPython implementation detail and not defined by Python in general, so maybe in some other Python implementation you could have different `0`-objects just like you can have different `1234`-objects in CPython:

``````>>> y1 = 1234
>>> y2 = 1234
>>> y1 is y2
False
``````

• @StefanPochmann Aha! Success! I'm able to create different `0`-objects after all! In Python 3:

``````>>> a = int.from_bytes(bytes([0]), 'big')
>>> a
0
>>> a is 0
False
``````

And then of course my above counter-example works nicely, the `and` and the `if-else` having different results:

``````>>> x1 = a and b
>>> if a:
x2 = b
else:
x2 = 0

>>> x1 is x2
False
``````

• This post is deleted!

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.