Skip to content

Create max_area_of_island.md#9

Open
tshimosake wants to merge 1 commit into
masterfrom
tshimosake-patch-7
Open

Create max_area_of_island.md#9
tshimosake wants to merge 1 commit into
masterfrom
tshimosake-patch-7

Conversation

@tshimosake

Copy link
Copy Markdown
Owner

Comment thread max_area_of_island.md
@@ -0,0 +1,61 @@
1回目。変数名などのコードの作法は気にしていない。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

人によるのかもしれませんが、step1も変数名を多少考える練習をしてみても良いのかなと思いました。
(少なくともdfsみたいな命名を禁止してみる、とか)

というのも、面接の場でパッと筋の良い変数名を命名するのは、普段から意識してやっていないと難しいと思うためです。(もう少し純粋な動機付けをした方が良いのかもしれませんが)

@tshimosake tshimosake Jun 30, 2024

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

面接の場でパッと筋の良い変数名を命名するのは、普段から意識してやっていないと難しい

確かにそうですね。ご指摘ありがとうございます!

Comment thread max_area_of_island.md
area += count_area(i + delta_i, j + delta_j)
return area

res = 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返り値に相当するものの変数名にansresを禁止してみるのも命名の良い練習になると思います。

Comment thread max_area_of_island.md
m = len(grid)
n = len(grid[0])

def dfs(i, j):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unionfind, stack使ってdfsなど、色々実装試されると練習になると思います。

Comment thread max_area_of_island.md
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数呼び出し前に条件チェックする選択肢もありますね。

Comment thread max_area_of_island.md
if (
not (0 <= i < m and 0 <= j < n)
or (i, j) in visited
or grid[i][j] == 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0, 1の数値をそのまま使うか、WATER = 0, LAND = 1のように定義して使うかも選択ですね。

Comment thread max_area_of_island.md
return 0

visited.add((i, j))
area = 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいかもしれませんが、areaの何が1なのか、少し気になりました。
少し下見たら area += count_area(... の記述があるので、数かな、となるのですが、
num_area のような感じだと親切かもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grid[i][j] の値である 1 と紛らわしいですね。WATER = 0, LAND = 1 を使う利点がわかった気がします。ご指摘ありがとうございます!

Comment thread max_area_of_island.md
m = len(grid)
n = len(grid[0])

def dfs(i, j):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i, j は個人的には何を表しているのか分かりづらく感じました。 x, y や row, column のほうが分かりやすく感じます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。やはり私は i, j のほうがわかりやすいと思います。
(x, y より原点が左上であることが伝わりやすく、row, column は配列かタプルなのかと思うので)
一般的には x, y や row, column のほうが伝わりやすいでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

X (Twitter) でアンケートを取ったところ、 i, j と x, y が同じくらいでした。
https://x.com/nodchip/status/1809223275442315414
実際にはチームの平均的なソフトウェアエンジニアが分かりやすいものを選ぶとよいと思います。また、面接の場で迷った場合は、面接官が i, j 嫌いだった場合を考慮し、「X (Twitter) 上でのアンケートでは i, j と x, y が同じくらいでした。自分はどちらで書くこともできます。今回は慣れている i, j のほうで書きます。」と excuse をしておくと安全だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

わざわざtwitterアンケートまでありがとうございます!! かしこまりました!

Comment thread max_area_of_island.md
return
count += 1
grid[i][j] = 0
dfs(i+1, j)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には二項演算子の両隣に空白を空けるほうが好みです。

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 (+, -, *, /, //, %, **, @).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants