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
#11
Can you replace your code with below code:
class LinearSearch:
    def __init__(self, entered_list=None, item=None):
        self.entered_list = entered_list
        self.item = item
         
    def seek(self, item):
        for index, element in enumerate(self.entered_list):
            if element == item:
                return index
        return -1  # Return -1 if the item is not found in the list

    def __str__(self):
        result = self.seek(self.item)
        if result != -1:
            return f"Item found at index {result}"
        else:
            return "Not Present"
Thanks
deanhystad write Jan-10-2024, 03:19 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


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