Requesting help regarding count_rotations_binary(nums)

Hello all,

While applying Binary Search, could someone please explain: In Section 6, why this strategy works “If the middle element of the list is smaller than the last element of the range, then the answer lies to the right of it. Otherwise, the answer lies to the left.” when in the examples given in this section don’t align with this statement.

Thanks in advance.

It’s just a casual mistake. :slightly_smiling_face:

But you get the idea that how to proceed.

Thanks for your reply!

As far as Binary Search goes, I do get the basic idea on how to proceed but if this statement is a mistake like you pointed, should we be thinking in completely opposite direction to this statement. Also, in the function definition, please correct me if I’m wrong here, shouldn’t we interchange the lines of code under elif and else given what is mentioned in the comments?

Thanks

You can correct the mistakes in notebook. I am sure this will not affect the grades as long as our code passes all the test cases possible.

Yes, you can interchange the lines of code or correct the comment whatever you like depending on what condition are you taking in elif.

I get that we can change anything in the code and the notebook but that was not what I wanted to know. Anyways, thank you again for your time and inputs.
Cheers

If the middle element of the list is smaller than the last element of the range, then the answer lies to the right of it. Otherwise, the answer lies to the left.
what does it mean?

As per this statement, if we take an example with this list: [7, 8, 1, 3, 4, 5, 6]. Middle element is 3 which is smaller than last element of the range which is 6. So, the answer lies to the right of it (middle element: 3) but here it lies on the left (1). This is puzzling for me as well but I got a reply above mentioning that this is a mistake. I’m not sure what I’m missing here, or if my understanding is completely wrong.

1 Like

Yes, opposite is right. The statement should have been ''If the middle element of the list is smaller than the last element of the list then the answer lies to the left of it (middle element). Otherwise, the answer lies to the right.

1 Like

You are understanding right, it was just a mistake in notebook while writing that statement.

1 Like

@ankitparashar88 Check this post Update - Assignment 1 Notebook of the Python DSA course

1 Like

@ankitparashar88 You can change the code with caution from the instructor that do not delete any cells, but you can add them. You should his variable names the same but feel free to add your own. He does have scripts for evaluation.

2 Likes

For example, in the list [6, 7, 1, 2, 3, 4, 5] the middle element is 2 and the last element is 5. 2 is smaller than 5, and the answer - that is, the only element smaller than the element before it - lies to the left of the middle element: [6, 7, 1, 2, 3, 4, 5] However in the list [4, 5, 6, 7, 1, 2, 3], the middle element is 7 and the last element is 3. 7 is greater than 3, and the answer lies to the right of the middle element: [4, 5, 6, 7, 1, 2, 3]
Hope this helps!

1 Like