mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Adds search method in the implementation
This commit is contained in:
@@ -91,6 +91,19 @@ class SingleLinkedList():
|
||||
print "Going there!"
|
||||
raise Exception("Value not found in the list")
|
||||
|
||||
def search(self, value):
|
||||
"""
|
||||
Returns True if the value is in the List.
|
||||
"""
|
||||
item = self._root
|
||||
while(item != None):
|
||||
if item.value == value:
|
||||
return True
|
||||
|
||||
item = item.nexti
|
||||
|
||||
return False
|
||||
|
||||
def __len__(self):
|
||||
# Returns the number of elements in the list
|
||||
return self._size
|
||||
|
||||
@@ -91,5 +91,23 @@ class test_single_linked_list(unittest.TestCase):
|
||||
self.assertEqual("Empty List", sl.__str__())
|
||||
self.assertEqual(0, len(sl))
|
||||
|
||||
def test_search(self):
|
||||
|
||||
sl = SingleLinkedList()
|
||||
|
||||
sl.add(2)
|
||||
sl.add(1)
|
||||
sl.add(3)
|
||||
self.assertEqual(3, len(sl))
|
||||
|
||||
self.assertEqual(True, sl.search(2))
|
||||
self.assertEqual(True, sl.search(3))
|
||||
self.assertEqual(True, sl.search(1))
|
||||
|
||||
self.assertEqual(False, sl.search("a"))
|
||||
self.assertEqual(False, sl.search(4))
|
||||
self.assertEqual(False, sl.search(12))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user