Engineering #21: how many different-length "normal" implementations?
And "The Bathers" by Fernand Leger
This isn’t an important question, but as I was doing this exercise —
The
destinations
list contains a list of travel destinations.
destinations = ['Prague', 'London', 'Sydney', 'Belfast',
'Rome', 'Aruba', 'Paris', 'Bora Bora',
'Barcelona', 'Rio de Janeiro', 'Marrakesh',
'New York City']
Write a function that, without using the built-in
in
operator, checks whether a specific destination is included withindestinations
. For example: When checking whether'Barcelona'
is contained indestinations
, the expected output isTrue
, whereas the expected output for'Nashville'
isFalse
.
And saw three different solutions of increasing length for it —
def contains(element, lst):
return element in lst
def contains(element, lst):
for item in lst:
if item == element:
return True
return False
def contains(element, lst):
index = 0
while index < len(lst):
if lst[index] == element:
return True
index += 1
return False
— I was thinking: how many distinct (and non-redundant) implementations of this are there, where each is at least two lines longer than the last?
I don’t know; just thinking. My ChatGPT gave a detailed answer, I don’t know enough yet to verify quickly, but I’ll quote this: “~5–8 more sane versions up to ~21 lines; past 25 lines you’re just writing a different program that coincidentally answers a yes/no question.”
Curious about your approach to this question with an additional condition: the user wants to perform case insensitive lookups in O(1) time.