Reputation: 1511
I've seen similar questions to this but I can't figure it out for my own example, I have this code:
import ast
import pytest
import re
def autocomplete1(str,list_name):
return [i for i in list(set(list_name)) if i.startswith(str)]
def autocomplete2(str,list_name):
return(list(filter(lambda x: x.startswith(str), list(set(list_name)))))
def autocomplete3(str,list_name):
return([i for i in list_name if re.match(str,i)])
#fix the list
@pytest.mark.parametrize('input1, input2, output1', [('de',['dog','deer','deal'],['deer','deal']), ('ear',['earplug','earphone','airplane'],['earplug','earphone'])])
def test_function(input1,input2,output1):
assert autocomplete1(input1,input2) == output1
assert autocomplete2(input1,input2) == output1
assert autocomplete3(input1,input2) == output1
The Error is:
start_query_string.py FF [100%]
============================================================================================= FAILURES ==============================================================================================
________________________________________________________________________________ test_function[de-input20-output10] _________________________________________________________________________________
input1 = 'de', input2 = ['dog', 'deer', 'deal'], output1 = ['deer', 'deal']
@pytest.mark.parametrize('input1, input2, output1', [('de',['dog','deer','deal'],['deer','deal']), ('ear',['earplug','earphone','airplane'],['earplug','earphone'])])
def test_function(input1,input2,output1):
> assert autocomplete1(input1,input2) == output1
E AssertionError: assert ['deal', 'deer'] == ['deer', 'deal']
E At index 0 diff: 'deal' != 'deer'
E Use -v to get the full diff
start_query_string.py:27: AssertionError
________________________________________________________________________________ test_function[ear-input21-output11] ________________________________________________________________________________
input1 = 'ear', input2 = ['earplug', 'earphone', 'airplane'], output1 = ['earplug', 'earphone']
@pytest.mark.parametrize('input1, input2, output1', [('de',['dog','deer','deal'],['deer','deal']), ('ear',['earplug','earphone','airplane'],['earplug','earphone'])])
def test_function(input1,input2,output1):
> assert autocomplete1(input1,input2) == output1
E AssertionError: assert ['earphone', 'earplug'] == ['earplug', 'earphone']
E At index 0 diff: 'earphone' != 'earplug'
E Use -v to get the full diff
start_query_string.py:27: AssertionError
I've tried slightly editing the code in different ways (e.g. turning the input in a tuple) but I'd like to understand how to get this version working so I know what I'm doing wrong. Could someone show me what's wrong?
Upvotes: 0
Views: 733
Reputation: 86
The point is that in autocomplete1 and autocomplete2 - set is unordered type so there is two ways, as I see it, for function to return predictable results:
Sort list after all manipulations will be done (btw there is no need to do list(set(list_name))
, you can iterate over set)
If you need specific order you can use OrderedDict
from collections import OrderedDict
l = [1, 1, 2, 3, 4, 5, 5, 2, 1]
result = list(OrderedDict.fromkeys(l))
print(result) # [1, 2, 3, 4, 5]
Full working code is
import ast
import pytest
import re
from collections import OrderedDict
def autocomplete1(str,list_name):
return [i for i in list(OrderedDict.fromkeys(list_name)) if i.startswith(str)]
def autocomplete2(str,list_name):
return(list(filter(lambda x: x.startswith(str), list(OrderedDict.fromkeys(list_name)))))
def autocomplete3(str,list_name):
return([i for i in list_name if re.match(str,i)])
#fix the list
@pytest.mark.parametrize('input1, input2, output1', [('de',['dog','deer','deal'],['deer','deal']), ('ear',['earplug','earphone','airplane'],['earplug','earphone'])])
def test_function(input1,input2,output1):
assert autocomplete1(input1,input2) == output1
assert autocomplete2(input1,input2) == output1
assert autocomplete3(input1,input2) == output1
Upvotes: 1