Conversation
| @@ -0,0 +1,61 @@ | |||
| 1回目。変数名などのコードの作法は気にしていない。 | |||
There was a problem hiding this comment.
人によるのかもしれませんが、step1も変数名を多少考える練習をしてみても良いのかなと思いました。
(少なくともdfsみたいな命名を禁止してみる、とか)
というのも、面接の場でパッと筋の良い変数名を命名するのは、普段から意識してやっていないと難しいと思うためです。(もう少し純粋な動機付けをした方が良いのかもしれませんが)
There was a problem hiding this comment.
面接の場でパッと筋の良い変数名を命名するのは、普段から意識してやっていないと難しい
確かにそうですね。ご指摘ありがとうございます!
| area += count_area(i + delta_i, j + delta_j) | ||
| return area | ||
|
|
||
| res = 0 |
There was a problem hiding this comment.
返り値に相当するものの変数名にansやresを禁止してみるのも命名の良い練習になると思います。
| m = len(grid) | ||
| n = len(grid[0]) | ||
|
|
||
| def dfs(i, j): |
There was a problem hiding this comment.
unionfind, stack使ってdfsなど、色々実装試されると練習になると思います。
| area = 1 | ||
| directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] | ||
| for delta_i, delta_j in directions: | ||
| area += count_area(i + delta_i, j + delta_j) |
| if ( | ||
| not (0 <= i < m and 0 <= j < n) | ||
| or (i, j) in visited | ||
| or grid[i][j] == 0 |
There was a problem hiding this comment.
0, 1の数値をそのまま使うか、WATER = 0, LAND = 1のように定義して使うかも選択ですね。
| return 0 | ||
|
|
||
| visited.add((i, j)) | ||
| area = 1 |
There was a problem hiding this comment.
細かいかもしれませんが、areaの何が1なのか、少し気になりました。
少し下見たら area += count_area(... の記述があるので、数かな、となるのですが、
num_area のような感じだと親切かもしれません。
There was a problem hiding this comment.
grid[i][j] の値である 1 と紛らわしいですね。WATER = 0, LAND = 1 を使う利点がわかった気がします。ご指摘ありがとうございます!
| m = len(grid) | ||
| n = len(grid[0]) | ||
|
|
||
| def dfs(i, j): |
There was a problem hiding this comment.
i, j は個人的には何を表しているのか分かりづらく感じました。 x, y や row, column のほうが分かりやすく感じます。
There was a problem hiding this comment.
ご指摘ありがとうございます。やはり私は i, j のほうがわかりやすいと思います。
(x, y より原点が左上であることが伝わりやすく、row, column は配列かタプルなのかと思うので)
一般的には x, y や row, column のほうが伝わりやすいでしょうか?
There was a problem hiding this comment.
X (Twitter) でアンケートを取ったところ、 i, j と x, y が同じくらいでした。
https://x.com/nodchip/status/1809223275442315414
実際にはチームの平均的なソフトウェアエンジニアが分かりやすいものを選ぶとよいと思います。また、面接の場で迷った場合は、面接官が i, j 嫌いだった場合を考慮し、「X (Twitter) 上でのアンケートでは i, j と x, y が同じくらいでした。自分はどちらで書くこともできます。今回は慣れている i, j のほうで書きます。」と excuse をしておくと安全だと思います。
There was a problem hiding this comment.
わざわざtwitterアンケートまでありがとうございます!! かしこまりました!
| return | ||
| count += 1 | ||
| grid[i][j] = 0 | ||
| dfs(i+1, j) |
There was a problem hiding this comment.
個人的には二項演算子の両隣に空白を空けるほうが好みです。
https://peps.python.org/pep-0008/#other-recommendations
Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator:
https://google.github.io/styleguide/pyguide.html#36-whitespace
Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).
https://leetcode.com/problems/max-area-of-island/