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
#1
I am writing a linear search algorithm. I've got the main logic under my belt but it's the string representation that I haven't got right. Below is my script and output in my REPL. When I try to search (I call the method seek) for an integer within a list of intergers, the item I test is clearly present but my output shows: "False" as if it is not present.

What can you people tell me about what is wrong with my __str__ and what might you people recommend I use instead? Thanks.

Code:

class LinearSearch:
    # This class accepts an array and an item
    def __init__(self, entered_list=None, item=None):
        self.entered_list = entered_list
        self.item = item
        
    def seek(self, item):
      # Loop through the array and check if the current array element is equal to the item
      for index in self.entered_list:
        if index == item:
            # If it is True, return the index at which the element is found
            return index
        # If the item is never found, return -1
        return False
    
    def __str__(self):
        result = self.seek(self.item)
        if result:
            return str(result) 
        else:
            return "Not Present"
Here is the REPL inputs and outputs:

$ python
Python 3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from linear import LinearSearch
>>> list_obj = [1,2,4,5,7,12,256]
>>> item = 7
>>> item_exists = LinearSearch(list_obj,item)
>>> item_exists
<linear.LinearSearch object at 0x7ff5338add50>
>>> item_exists.seek(7)
False
>>> item_exists.seek(256)
False
>>>
Reply


Messages In This Thread
Writing a Linear Search algorithm - malformed string representation - by Drone4four - Jan-08-2024, 01:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 959 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Basic binary search algorithm - using a while loop Drone4four 1 2,203 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 4,338 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 3,572 Sep-27-2022, 01:38 PM
Last Post: buran
  Search multiple CSV files for a string or strings cubangt 7 12,803 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Search string in mutliple .gz files SARAOOF 10 9,616 Aug-26-2021, 01:47 PM
Last Post: SARAOOF
  fuzzywuzzy search string in text file marfer 9 8,200 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  I want to search a variable for a string D90 lostbit 3 3,408 Mar-31-2021, 07:14 PM
Last Post: lostbit
  platform binary representation --- 3 questions Skaperen 4 3,805 Dec-05-2020, 03:50 AM
Last Post: Skaperen
  String search in different excel Kristenl2784 0 2,096 Jul-20-2020, 02:37 PM
Last Post: Kristenl2784

Forum Jump:

User Panel Messages

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