exercism / python

Exercism exercises in Python.
https://exercism.org/tracks/python
MIT License
1.94k stars 1.29k forks source link

Code passes `binary_search_test.py`although malfunctioning #3494

Closed muritz closed 1 year ago

muritz commented 1 year ago

Hi, this is my first issue, so be easy on me, if I make any formal mistake.

I successfully submitted my solution to binary search. However I came across a strange behaviour in my own dev environment. Although the values 4 and 8 are contained in the list [1, 3, 4, 6, 8, 9, 11], my code raises ValueError("value not in array") only on these two values. Because these values are not tested explicitly, the code passes the test.

I tested out the values by manually changing the binary_search_test.py values and as expected, the test didn't pass, see code.

This is the test file on GitHub https://github.com/exercism/python/blob/main/exercises/practice/binary-search/binary_search_test.py Test starts at line17

def test_finds_a_value_in_the_middle_of_an_array(self):

        self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 6), 3)
        # fails => self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 4), 2)
        # fails => self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 8), 4)

I still don't understand, why my code behaves like that and I am not proficient enough, to help solve this issue in the testing.

For reference, here is my code, but I don't expect it to be solved here. I just want the test to catch errors in the code

Thanks Murat

def find(search_list: list[int], value: int) -> int:
    sorted_list = sorted(search_list)

    left, right = 0, len(search_list) - 1

    if not search_list:
        raise ValueError("value not in array")
    if search_list[0] == value:
        return 0
    elif search_list[-1] == value:
        return len(search_list) - 1
    while left < right:
        mid = left + (right - left) // 2
        if sorted_list[mid] == value:
            return mid
        elif sorted_list[mid] < value:
            left = mid + 1
        else:
            right = mid - 1

    raise ValueError("value not in array")
github-actions[bot] commented 1 year ago

Hello. Thanks for opening an issue on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use this link%0D%0A%20%20%20%20%20%20%20%20#%20fails%20=%3E%20self.assertEqual(find(%5B1,%203,%204,%206,%208,%209,%2011%5D,%204),%202)%0D%0A%20%20%20%20%20%20%20%20#%20fails%20=%3E%20self.assertEqual(find(%5B1,%203,%204,%206,%208,%209,%2011%5D,%208),%204)%0D%0A%60%60%60%0D%0AI%20still%20don't%20understand,%20why%20my%20code%20behaves%20like%20that%20and%20I%20am%20not%20proficient%20enough,%20to%20help%20solve%20this%20issue%20in%20the%20testing.%0D%0A%0D%0AFor%20reference,%20here%20is%20my%20code,%20but%20I%20don't%20expect%20it%20to%20be%20solved%20here.%20I%20just%20want%20the%20test%20to%20catch%20errors%20in%20the%20code%0D%0A%0D%0AThanks%20Murat%0D%0A%0D%0A%0D%0A%60%60%60%0D%0Adef%20find(search_list:%20list%5Bint%5D,%20value:%20int)%20-%3E%20int:%0D%0A%20%20%20%20sorted_list%20=%20sorted(search_list)%0D%0A%0D%0A%20%20%20%20left,%20right%20=%200,%20len(search_list)%20-%201%0D%0A%0D%0A%20%20%20%20if%20not%20search_list:%0D%0A%20%20%20%20%20%20%20%20raise%20ValueError(%22value%20not%20in%20array%22)%0D%0A%20%20%20%20if%20search_list%5B0%5D%20==%20value:%0D%0A%20%20%20%20%20%20%20%20return%200%0D%0A%20%20%20%20elif%20search_list%5B-1%5D%20==%20value:%0D%0A%20%20%20%20%20%20%20%20return%20len(search_list)%20-%201%0D%0A%20%20%20%20while%20left%20%3C%20right:%0D%0A%20%20%20%20%20%20%20%20mid%20=%20left%20+%20(right%20-%20left)%20//%202%0D%0A%20%20%20%20%20%20%20%20if%20sorted_list%5Bmid%5D%20==%20value:%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mid%0D%0A%20%20%20%20%20%20%20%20elif%20sorted_list%5Bmid%5D%20%3C%20value:%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20left%20=%20mid%20+%201%0D%0A%20%20%20%20%20%20%20%20else:%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20right%20=%20mid%20-%201%0D%0A%0D%0A%20%20%20%20raise%20ValueError(%22value%20not%20in%20array%22)%0D%0A%60%60%60%0D%0A&category=python) to copy this into a new topic there.


Note: If this issue has been pre-approved, please link back to this issue on the forum thread and a maintainer or staff member will reopen it.