Unexpected output

How the actual output is 4 ???,
it should be 5.

Maybe you can share notebook link
Moreover actual output depends on your code so maybe your code is wrong somewhere
Also the Excpected output shown is 3 which is also wrong and it should be 5 too…

Don’t forget to share the notebook link…

1 Like

Actually i have passed that 3 as a Expected output, You can say that’s a typo from my side.

It seems like the test case is defined incorrectly.

In this case the array should contain only one place where the consequent values decrease.

Here it’s 2 times: 43 to 34, and 34 to 3.

1 Like

Woah, nice observation thanks for your reply

my test cases #1 got failed however rest all the cases are passes for binary search.

test case #1 : list contains 8 numbers and rotated 5 times.

my test code is as follows:

def count_rotations_linear(nums):
position = 1 # What is the intial value of position?

while position < len(nums):                     # When should the loop be terminated?
    
    # Success criteria: check whether the number at the current position is smaller than the one before it
    if position > 0 and nums[position]<nums[position-1]:   # How to perform the check?
        return position
    
    # Move to the next position
    position += 1

return 0                     # What if none of the positions passed the check

what is the issue in my code ??? please help?

Please Share the actual test_case which is giving wrong output…

Your code seems fine…maybe the problem lies in the Test Case…

below the the test which is failing:

# A list of size 8 rotated 5 times.
test1 = {
    'input': {
        'nums': [5,6,7,8,1,2,3,4]
    },
    'output': 5
}

sorry i have mentioned the wrong test case above. Below is the test case which failed:

TEST CASE #0

Input:
{‘nums’: [19, 25, 29, 3, 5, 6, 7, 9, 11, 14]}

Expected Output:
3

Actual Output:
None

Execution Time:
0.002 ms

Test Result:
FAILED

The expected output is ok.

The actual output is wrong, so the implementation is incorrect. Fix the function responsible for calculating the answer and test again.

Hey According to the code you sent earlier this test_case is giving the output 3…
Maybe the problem is somewhere else because your code is fine…

Maybe you can share the link of the notebook so that we can help you…

Hi I am stuck in Binary search now:
my code is running for all the cases except the empty case where there is no element so out put should be zero but it is giving none.

below is my code:

def count_rotations_binary(nums):
n = len(nums)
start = 0
end = n-1

finding the index of minimum of the array

index of minimum would be equal to number of rotation

while start <= end:
    mid = start + (end - start) // 2

calculating the previous (prev) and next (nex) index of mid

    prev = (mid - 1 + n) % n
    nex = (mid + 1) % n

checking if mid is minimum

    if nums[mid] < nums[prev] and nums[mid] <= nums[nex]:
        return mid

if not selecting the unsorted part of the array

    elif nums[mid] < nums[start]: end = mid - 1
    elif nums[mid] > nums[end]: start = mid + 1
    else:
        return 0
        if n == 0 :
            return 0

please suggest ??

Your loop is never executed.

Therefore, (probably, because I’m not that omnipotent, and I can’t see your code) nothing is returned.

If you want to consider that empty list has 0 rotations, then just return 0 if the loop has never run.

The problem is present in the first / second part of the code if you’re having trouble understanding…

def count_rotations_binary(nums):
    n = len(nums)
    start = 0
    end = n-1
    while start <= end:

where when your list is empty then n = 0 which makes end = -1 so your start is greater than your end due to which the loop never starts running…

So as Sebastian Sir pointed out you have to create a separate condition for returning 0 when list is empty…

Thanks sir…I got my mistake and corrected it. I have submitted my assignment now.
Thanks a lot :relaxed:

1 Like