DEV Community

Nw3965
Nw3965

Posted on

Search Insert Binary Python

Search Insert Position

I wanted to count numbers inside of "target" and I could not write what I wanted.

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        i = count.target.index
        return i
Enter fullscreen mode Exit fullscreen mode

AI checked this code and answered like this.

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        try:
            i = nums.index(target)
        except ValueError:
            nums.append(target)
            nums.sort()
            i = nums.index(target)
        return i
Enter fullscreen mode Exit fullscreen mode

I have to practice try except.

Top comments (0)