I'm writing in Python (3.4) a double search function that looks in a list for one value and then the other alternatively. That is, start searching the second value once you find first the first value (and start searching from where you found the first value) and vice-versa.
My first approach was to write a recursive function to do it, only to find out they don't end up as neat in Python as they do in Haskell. This is a basic sketch of the function:
def double_search(input_list, first_condition, second_condition, condition_to_search, output_list):
if input_list == []:
return output_list
else:
if condition_to_search == 1:
if input_list[0] == first_condition:
return output_list + double_search(input_list[1:], first_condition, second_condition, 2, output_list + ['First'])
else:
return output_list + double_search(input_list[1:], first_condition, second_condition, 1, output_list + ['None'])
if condition_to_search == 2:
if input_list[0] == second_condition:
return output_list + double_search(input_list[1:], first_condition, second_condition, 1, output_list + ['Second'])
else:
return output_list + double_search(input_list[1:], first_condition, second_condition, 2, output_list + ['None'])
I have several problems with the way it turned out, looks forced and ugly in Python. And produces different outputs in Python interpreter and pydev debugger!
Pydev debugger gives the proper result while in the interpreter I have to take the tail of the result:
result[-len(input_list):]
To get a list of the appropriate length, which I find unacceptable because I have no clue where the garbage in front comes from.
Would appreciate any suggestion to improve this code, make it more pythonic and easier to maintain as well as any performance improvements you can suggest.
EDIT: As per request, the following is an example of the function in use with some sample data.
example_list = [5,4,1,2,0,3,1,3,4,2,5,0]
double_search(example_list, 1, 3, 1, [])
Should result in: ['None', 'None', 'First', 'None', 'None', 'Second', 'First', 'Second', 'None', 'None', 'None', 'None']
While:
double_search(example_list, 1, 5, 1, [])
Results in: ['None', 'None', 'First', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'Second', 'None']
Notice how in the second case only matches the second to last '5', because it needs to find first a '1'.