-
Notifications
You must be signed in to change notification settings - Fork 0
125. Valid Palindrome #63
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
|
|
||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| normalized = re.findall(r"[a-zA-Z0-9]", s.lower()) |
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.lower()した後なので、r"[a-z0-9]"で動作します。個人的には解釈の余地がないほうが好きですが、好みの問題かもしれません。
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.
ありがとうございます。
気づかず大文字を残したままにしていました...
自分もr"[a-z0-9]"の方が良いと感じます。
| normalized = re.findall(r"[a-zA-Z0-9]", s.lower()) | ||
| left = 0 | ||
| right = len(normalized) - 1 | ||
| while left < right: |
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.
i/leftのわかりやすさ・良し悪しは一長一短ありますが、以下のようにもかけます。参考まで。
half = len(normalized) // 2
return all(normalized[i] == normalized[-i - 1] for i in range(half))単純な実行速度だけでいうと、多分[::-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.
ありがとうございます。
シンプルで分かりやすいですね。
3つの方法の実行時間を比較してみました。
=== 回文の場合 ===
サイズ 100:
two_pointer: 2.430 ms
all_range: 5.449 ms
slice_reverse: 0.468 ms
サイズ 1,000:
two_pointer: 34.838 ms
all_range: 54.488 ms
slice_reverse: 3.875 ms
サイズ 10,000:
two_pointer: 399.618 ms
all_range: 565.837 ms
slice_reverse: 28.039 ms
サイズ 100,000:
two_pointer: 3807.737 ms
all_range: 5622.369 ms
slice_reverse: 287.209 ms
=== 非回文の場合 ===
サイズ 100:
two_pointer: 0.121 ms
all_range: 0.939 ms
slice_reverse: 0.399 ms
サイズ 1,000:
two_pointer: 0.148 ms
all_range: 0.976 ms
slice_reverse: 2.736 ms
サイズ 10,000:
two_pointer: 0.149 ms
all_range: 0.987 ms
slice_reverse: 17.218 ms
サイズ 100,000:
two_pointer: 0.149 ms
all_range: 0.969 ms
slice_reverse: 172.222 ms
回文の場合は[::-1]が、C実装とかの恩恵で速く、非回文の場合は逆に[::-1]だけはearly returnが効かないので遅い、という解釈です。
huyfififi
left a comment
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.
良いと思います!関数型言語に明るくないので、Generatorがどう関数型っぽい (line 175) のか気になったくらいで、他は特に引っかかるところはなかったです
t-ooka
left a comment
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.
MCPサーバー使ってAIにそれぞれのPRレビューしていただいてるんですかね?
進め方とても参考になりました。
内容もとても良いと思いました、コメント特に思いつかなかったです。
1点だけ問題文のところでコメントしています!
| - Tags: Two Pointers, String | ||
| - Notebook: https://share.solve.it.com/d/e80e6c41b8c79d6a8e2915069f91e325 | ||
|
|
||
| ## 問題文 |
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.
問題文にHTMLタグ含まれていると読みにくいと感じる人がいるかもです。(少なくとも自分は読みにくいなと思いました。)
問題文のURLもあるのでもしかしたら不要かもですね 🙇♂️
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.
フィードバックありがとうございます。
確かにファイルは.mdですし、レビューが不要な部分なのでレビュワーからしたらノイジーに感じますよね。
こちら削除させていただきました。
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.
MCPサーバー使ってAIにそれぞれのPRレビューしていただいてるんですかね?
MCPサーバーは使っていないですね。
やっていること自体はわりとシンプルで、jupyterのwrapperを作り、promptセルというのを作ります。これは実際にはcodeセルであり、claudeのAPIを叩いていますがそれはwrapper内部実装に隠蔽されており、promptセルにプロンプトを直接書くだけでAPIを叩けるようになっています。またこのとき内部で、そのセルより上のすべてのセルの入出力もcontextとして含まれるようにメッセージを修正してから送信しています。
promptセルの出力についても、APIレスポンスjsonからLLMの出力テキストだけを抽出し、output部分に表示させています。
このpromptセルのinput/outputをexport時にQ&Aとして整えています。
125. Valid Palindrome
Next: 226. Invert Binary Tree