-
Notifications
You must be signed in to change notification settings - Fork 0
20 Valid Parentheses #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここの空白は1行にした方がいいと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP 8的には2行ですね。
Surround top-level function and class definitions with two blank lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
全体的にとても読みやすくて良いコードだと思いました。
stack: List[str] = [] | ||
for i in range(len(s)): | ||
if s[i] in brackets.keys(): | ||
if len(stack) == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好みの問題だと思いますが、以下のように条件文をひとつづきにするのも自然に感じます。
(スタックが空か、もしくはスタックの頂上が目当てのものでないなら False )
if len(stack) == 0 or stack.pop() != brackets[s[i]]:
return False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
全体的に良さそうです。
- `if c in open_to_close.keys()`もより短く`if c in open_to_close`に変えた | ||
|
||
```Python3 | ||
from queue import LifoQueue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LifoQueueはスレッドセーフな設計になっていて、ロックを取得するためパフォーマンス面では劣りそうです。ユースケースによって使い分けるといいと思います。https://docs.python.org/3/library/queue.html
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP 8的には2行ですね。
Surround top-level function and class definitions with two blank lines.
``` | ||
|
||
### Step 3 | ||
- `for i in range(len(s))`を`for c in s`に変えて、Pythonらしくシンプルさを追求した |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ今回はどちらでもいいですが、例えばsがdeque等だった場合を考えると、ランダムアクセスがO(1)で出来ないので、場合によっては気をつけた方が良さそうです。
### Step 1 | ||
- stackのカテゴリの問題であるというヒントもあり、すぐに方針は立った | ||
- 実装も割とすぐにできた | ||
- 他のArai60の問題はgolangで解いてきたが、golangでの文字列処理は面倒なのでPythonを使うことにした |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[質問]
Goに詳しくないので質問させてください
Goのどのような部分が文字列処理を面倒にしていると思いますか??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goでは文字列に対してインデックスで一文字取得するとbyte型で、rangeでループするとrune型という別の型になります。
for i, c := range s
としてループの中でs[i]とcを出力すると、例えば'('の場合、どちらも41という出力結果になります。しかし、型が違うのでs[i] == cはエラーになってしまいます。一方、Pythonだとsもs[i]も同じstr型なので文字列の処理の際に余計なことを考えずに済むので今回はPythonを選びました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もう一つgoではなくpythonを選んだ大きな理由として、goにはスタックを自分で実装する必要があるからです。今回だとスタックからのpopを以下のようにやらないといけません。
l := len(openBracketsStack)
if l == 0 {
return false
}
top := openBracketsStack[l-1]
openBracketsStack = openBracketsStack[:l-1]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以上二つの理由から今回はgoを避けました。というより、面接で同じ問題が出されてgoを選んだらその時点で知識とセンスを疑われるような気がしたことが理由です。
以前勉強会で、面接で使う言語はその会社で使っている言語と必ずしも同じでなくて良いと伺いましたが、与えられた問題に対してどの言語を選ぶかというセンスは見られているでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
言語を切り替えられないこともありますし、何だと書きやすいという話をすれば十二分だと思いますね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど〜
標準ライブラリにデータ構造が乏しいのはGoにGenericsがなかったのも関係してるんですかね
サードパーティのライブラリ見てるとinterfaceでデータ構造を抽象化してるみたいですね
こっちが主流になっちゃって標準ライブラリのサポートがないままって感じなんでしょうか
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「サードパーティが主流になっちゃった」というより最初から標準で用意するつもりがなかったと思います。goはミニマリスト思考が大きなコンセプトになるので
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
G 社内であんまり困った記憶ないんですよね。ちょっと複雑なことをすると、RPC を飛ばすことになっていたかと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
G 社内であんまり困った記憶ないんですよね。ちょっと複雑なことをすると、RPC を飛ばすことになっていたかと思います。
理解の確認をさせてください
これって、ある程度複雑なデータ処理は他のサービスをcallして任せていたからGo側の標準ライブラリにデータ構造が乏しくても困らなかったってことであってますか??
https://leetcode.com/problems/valid-parentheses/description/