-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 33. Search in Rotated Sorted Array #43
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
| 4. numが後半に属し、target以上 -> (True, True) | ||
| <- targetが後半ならこれを満たす初めを得る | ||
| """ | ||
| return (num <= nums[-1], target <= num) |
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.
あんまり考えていないのですが、これ、単に
(num <= nums[-1], num)
でもいいですかね。
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.
レビューありがとうございます。
これ指摘されて今気づけました。
前半と後半のどちらにいるかを分けた後に、target以上になる位置を探したいので、
(num <= nums[-1], num)で問題ないですね。
| return left | ||
| return -1 | ||
|
|
||
| if nums is None: |
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.
- numsが空のケースを弾く
空が空配列のことなら弾けてないですね。Noneと[]は別物なので。
$ ipython
Python 3.11.3 (main, Jun 5 2023, 00:49:13) [Clang 14.0.3 (clang-1403.0.22.14.1)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: [] is None
Out[1]: FalseThere 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.
ご指摘、検証ありがとうございます。
空配列とNoneを混同していました。
以下のようにすべきですね。
if nums is None:
raise TypeError("Input cannot be None")
if len(nums) == 0:
raise ValueError("Input cannot be empty array")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.
numsの型がList[int]と書いてある (List[int] | None ではない) のでNoneかどうかだけわざわざチェックするのはちょっと違和感ありました。じゃあnumsがfloatとかdictのときとかをスルーしてるの何でだろうと思うので。
自分があえてやるなら not isinstance(nums, list) とか type(nums) is not list でTypeError出すかなぁと思いました (サブタイプも受け入れたいから前者かな)。
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.
じゃあnumsがfloatとかdictのときとかをスルーしてるの何でだろうと思うので。
おっしゃる通りです。
今回は、if len(nums) == 0:だけしておけばよさそうですね。
自分があえてやるなら not isinstance(nums, list) とか type(nums) is not list でTypeError出すかなぁと思いました (サブタイプも受け入れたいから前者かな)。
確かに、型が一致するか(サブタイプ含めて一致するかみるならtype、そうでないならisinstanceを選ぶ)を見る方が、適切ですね。
| return target <= num | ||
| else: # numが後半の山にいる | ||
| return True | ||
| if target <= nums[-1]: # targetが後半の山にいると思われる |
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.
else: にしてもよいと思いました。
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.
レビューありがとうございます。
確かにこれくらいの距離ならelseでも条件を覚えておけそうですね。
33. Search in Rotated Sorted Array