Python Forum
Writing a Linear Search algorithm - malformed string representation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing a Linear Search algorithm - malformed string representation
#10
__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().
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.
Reply


Messages In This Thread
RE: Writing a Linear Search algorithm - malformed string representation - by deanhystad - Jan-08-2024, 07:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic binary search algorithm - using a while loop Drone4four 1 442 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 2,094 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,457 Sep-27-2022, 01:38 PM
Last Post: buran
  Search multiple CSV files for a string or strings cubangt 7 8,256 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Search string in mutliple .gz files SARAOOF 10 7,058 Aug-26-2021, 01:47 PM
Last Post: SARAOOF
  fuzzywuzzy search string in text file marfer 9 4,727 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  I want to search a variable for a string D90 lostbit 3 2,686 Mar-31-2021, 07:14 PM
Last Post: lostbit
  platform binary representation --- 3 questions Skaperen 4 2,847 Dec-05-2020, 03:50 AM
Last Post: Skaperen
  String search in different excel Kristenl2784 0 1,730 Jul-20-2020, 02:37 PM
Last Post: Kristenl2784
  Why int() cannot pass a string representation of a float into int? majorjohnusa 1 1,923 Jul-09-2020, 05:26 AM
Last Post: Knight18

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020