Conversation
| ```py | ||
| class Solution: | ||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| def dfs(i:int, j:int) -> None: |
There was a problem hiding this comment.
| # 範囲外または海なら返す | ||
| if not 0 <= i < len(grid) \ | ||
| or not 0 <= j < len(grid[0]) \ | ||
| or grid[i][j] == '0': |
There was a problem hiding this comment.
'0'や'1'だと味気ない(reader-friendlyでない)ので、'WATER'や'LAND'のような変数名やEnumを使う選択肢も一考かと思います。
| return num_of_islands | ||
| ``` | ||
|
|
||
| 2回目と3回目。 |
There was a problem hiding this comment.
他にもBFSやUnionFindで書いてみると勉強になるかもしれません。
|
|
||
| num_of_islands = 0 | ||
| for i, row in enumerate(grid): | ||
| for j, num in enumerate(grid[i]): |
There was a problem hiding this comment.
rowに対応するものだとcolが自然でしょうか。
あとrowとnumって使っていませんね。
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| def dfs(i:int, j:int) -> None: | ||
| # 範囲外または海なら返す | ||
| if not 0 <= i < len(grid) \ |
There was a problem hiding this comment.
width = len(grid)のようにしてwidthを使い回した方が良いかもしれません。
There was a problem hiding this comment.
個人的には、not A or not Bよりnot(A and B)の方が分かりやすいです。
There was a problem hiding this comment.
自分はtorusさんと逆で not A or not B の方が読みやすく感じました。
| or grid[i][j] == '0': | ||
| return | ||
|
|
||
| grid[i][j] = '0' |
There was a problem hiding this comment.
gridを直接書き換えても良いのかどうかも少し考えたいポイントですよね。
There was a problem hiding this comment.
これは本当にそうですね。あとで書いてみます。
| ```py | ||
| class Solution: | ||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| def dfs(i:int, j:int) -> None: |
There was a problem hiding this comment.
: のあとにスペースを空けることをお勧めいたします。
https://peps.python.org/pep-0008/#other-recommendations
There was a problem hiding this comment.
リンク先だとむしろ行末にスペースを入れるなと書いてありませんか?
Avoid trailing whitespace anywhere.
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| def dfs(i:int, j:int) -> None: | ||
| # 範囲外または海なら返す | ||
| if not 0 <= i < len(grid) \ |
There was a problem hiding this comment.
\ による行継続は避け、 () による行結合を使うことをお勧めいたします。
https://google.github.io/styleguide/pyguide.html#s3.6-whitespace
Do not use a backslash for explicit line continuation.
| or grid[i][j] == '0': | ||
| return | ||
| grid[i][j] = '0' | ||
| dfs(i+1, j) |
There was a problem hiding this comment.
1 回目のように + や - の左右にスペースを空けたほうが自然に感じました。
| if not 0 <= i < len(grid) \ | ||
| or not 0 <= j < len(grid[0]) \ | ||
| or grid[i][j] == '0': |
There was a problem hiding this comment.
改行がかえって見づらいように感じました。改行する場合にも if と同じインデントの部分に or がくるのは違和感があります。
There was a problem hiding this comment.
こんな感じでしょうか
if (a
or b
or c
):
pass| dfs(i + 1, j) | ||
| dfs(i, j + 1) | ||
| dfs(i - 1, j) | ||
| dfs(i, j - 1) |
| for i, _ in enumerate(grid): | ||
| for j, char in enumerate(grid[i]): | ||
| if char == '1': | ||
| res += 1 |
There was a problem hiding this comment.
enumerate を使うことで可読性が落ちていそうです。シンプルに二重で for ループを回し、grid[i][j] にアクセスするほうがわかりやすいと思いました。
There was a problem hiding this comment.
個人的に range(len(grid)) と2回関数を適用すると見づらいと思っており、enumerateを使いました。
There was a problem hiding this comment.
見づらいというのは僕も同意です。なので m = len(grid), n = len(grid[0]) とはじめに宣言しておくと、for i in range(m) のようになるのでわかりやすそうに思いました。(問題文中で m x n のサイズと明記されているので grid の範囲内かどうかの判定でも m, n を使うとわかりやすくなりそうです)
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| def dfs(i:int, j:int) -> None: | ||
| # 範囲外または海なら返す | ||
| if not 0 <= i < len(grid) \ |
There was a problem hiding this comment.
自分はtorusさんと逆で not A or not B の方が読みやすく感じました。
| if not 0 <= i < len(grid) \ | ||
| or not 0 <= j < len(grid[0]) \ | ||
| or grid[i][j] == '0': | ||
| return |
There was a problem hiding this comment.
この条件を再帰呼び出しの前に移動することで、再帰呼び出しの回数を最小化することもできます。
There was a problem hiding this comment.
逆に関数側で処理したほうが呼び出すときに柔軟に扱えて便利だとも思っています。関数の呼び出しは(ほかの処理に比べて)パフォーマンスに大きく影響するのでしょうか?
There was a problem hiding this comment.
関数呼び出しは足し算や掛け算と比べると遅いですが、大きく影響するものではないです。
各オペレーションに必要なCPUクロック数の目安です -> http://ithare.com/infographics-operation-costs-in-cpu-clock-cycles/
自分も両方書くので好みの問題ですが、例えば再帰を非再帰に書き換えるときなどに有用になると思います。
| dfs(i, j+1) | ||
| dfs(i-1, j) | ||
| dfs(i, j-1) | ||
| return |
There was a problem hiding this comment.
好みかもしれませんが、Pythonはインデントで関数の範囲?が解釈されるので、returnを明示したほうがわかりやすいかな、と思っています。
https://leetcode.com/problems/number-of-islands/description/