Much like an API endpoint to search posts in a relational database or an algorithm performing binary search on a sorted list, finding specific data in our data structures is crucial. This operation is quite similar to a Kafka Stream filter or an SQL SELECT
query where we screen all the records to find the ones matching our criteria.
In python, we will implement the search_data
method within our MyDataStructure
class. This method will test every value in our data
list and return a boolean indicating whether a specific item exists.
For instance, we could consider the S&P500 stocks' list and search for AAPL
as we want to know its performance before making any financial decision. If AAPL
is present in our structure, it will return True
; otherwise, False
.
Our data structure provides a simple, yet efficient way to perform a search operation on our data set. However, depending on the nature of the data and its size, different data structures and algorithms may prove more efficient. We could consider it as a simplistic version of a git grep
command that sifts through project files to find a desired piece of information.
xxxxxxxxxx
class MyDataStructure:
def __init__(self):
self.data = []
def add_item(self, item):
self.data.append(item)
def remove_item(self, item):
self.data.remove(item)
def update_item(self, index, newItem):
self.data[index] = newItem
def search_data(self, item):
return item in self.data
if __name__ == "__main__":
myDataStructure = MyDataStructure()
myDataStructure.add_item('AAPL')
print(myDataStructure.search_data('AAPL'))
print(myDataStructure.search_data('GOOGL'))