Since binary search is \$O(log(n))\$. Consider this example where I am searching for the existence of e
and l
and f
in the word.
Is this \$O(n log(n))\$ or \$O(3 log(n))\$ and thereby still \$O(log(n))\$?
def search(array, item):
if not array:
return False
if len(array) == 1:
return array[0] == item
mid = len(array) // 2
left = array[:mid]
right = array[mid:]
return search(left, item) or search(right, item)
def elfish(word, letters):
"""
>>> elfish('', 'efl')
False
>>> elfish('whiteleaf', 'elf')
True
>>> elfish('whiteleaf', 'elf')
True
>>> elfish('unfriendly', 'elf')
True
>>> elfish('waffles', 'elf')
True
>>> elfish('waffles', 'elf')
True
>>> elfish('sameh', 'elf')
False
>>> elfish('wolf', 'elf')
False
"""
result = True
for letter in letters:
result = result and search(word, letter)
return result
elfish('waffles', 'elf')
The pure Python way of doing this is:
if 'e' in word and 'l' in word and 'f' in word:
return True
Is Python doing a binary search in the background?
O(n)
.) – kfx 17 hours ago