Jan-08-2024, 07:51 PM
(This post was last modified: Jan-08-2024, 07:51 PM by deanhystad.)
__str__() doesn't work at all anymore. Call it and you get an Attribute error.
I also think that seek() doesn't work. seek() returns the index of the value in the list, or False if not found. When I see that a function returns False, I think that I can treat the return value as a bool. This does not work for seek().
I rewrite my code like this to make it work:
Even though seek() technically works, it is too easy to mess up the results. If seek() returns a non-negative integer when it succeeds, I would have it return -1 when it fails, or raise an exception (list.index() does this). Even None makes more sense than returning False.
I also think that seek() doesn't work. seek() returns the index of the value in the list, or False if not found. When I see that a function returns False, I think that I can treat the return value as a bool. This does not work for seek().
x = LinearSearch([1, 2, 3]) if (index := x.seek(1)): print(1, "found at", index) else: print(1, "is not in list")
Output:1 is not in list
1 is found at index==0. When I use the .seek() return value as a bool, 0 is treated the same as False. The method works if the value not in the list, or if it is anywhere except the front of the list.I rewrite my code like this to make it work:
x = LinearSearch([1, 2, 3]) if (index := x.seek(1)) != False: print(1, "found at", index) else: print(1, "is not in list")
Output:1 is not in list
Oops! That doesn't work either. I try this:x = LinearSearch([1, 2, 3]) if (index := x.seek(1)) is not False: print(1, "found at", index) else: print(1, "is not in list")
Output:1 found at 0
That was found. False == 0, but False is not 0.Even though seek() technically works, it is too easy to mess up the results. If seek() returns a non-negative integer when it succeeds, I would have it return -1 when it fails, or raise an exception (list.index() does this). Even None makes more sense than returning False.