in Education by
This is a moronic question. Here goes: I have two lists, candidates_input and constraint_input. The function below finds the winning candidate in candidates_input for a given ordering of constraints in constraint_input by eliminating candidates that can't be the winner until only one is left. So I'm removing items from both input lists -- losing candidates and constraints that have already told all they can tell, then moving on to the next constraint. I don't want to actually modify the original input lists, though, because I'll need them again. But inserting something like this at the beginning of the function: remaining_candidates = candidates_input[:] remaining_constraints = constraint_input[:] And then substituting those new names for the old ones within the function seems to break the recursion. What am I doing wrong? def OT_eval(candidates_input, constraint_input): #chooses an optimal candidate given candidates and constraint ranking constraint = constraint_input[0] #highest ranked constraint violations_list = [(len(re.findall(constraint, candidate)), candidate) for candidate in candidates_input] violations_list.sort() """ Creating list of (num violations, candidate) duples for each candidate, then sorting on num violations """ for pair in violations_list: #checking each candidate against known optimal candidate if pair[0] > violations_list[0][0]: #num violations > minimal violations candidates_input.remove(pair[1]) #removing suboptimal candidate if len(candidates_input) > 1 and len(constraint_input) > 0: #if further constraints needed, constraint_input.remove(constraint) #remove current constraint and recurse OT_eval(candidates_input, constraint_input) elif len(candidates_input) == 1: optimal_candidate = candidates_input[0] print "Optimal Candidate: ", optimal_candidate return optimal_candidate elif len(candidates_input) > 1 and len(constraint_input) == 0: #more than one candidate left, but no more constraints raise ValueError("Optimal candidate cannot be determined: check constraints") JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The reason it "works" without making a copy is that when you make the recursive call, you don't return anything from it. Each recursive call re-filters the original data, and once you've returned from everything, the original data has been completely re-filtered. If you make copies, then successively more-filtered copies are created, but the original data is unchanged, and there is no way to access the more-filtered copies from the calling scope. This is fixed by returning the result of the recursive call. At the point where you make the initial call, you would capture the return value (and perhaps reassign it to the original variable). Of course, for things to work properly, you'd need to return the same kind of thing everywhere that you return. So you'd return a (candidates_input, constraint_input) tuple so that you have that data, and let the call site interpret the result. Your code is confused; responsibility is not properly separated. There are two tasks here: filtering the data, and determining what the filtered data means. When you write recursive code, you want it to be in a functional style, generally. That means: don't modify things in place, but instead create the modified versions. For consistency and neatness, you should be doing this even for sub-steps. def OT_eval(candidates_input, constraint_input): # It's much cleaner to handle base cases for recursion **first**. if not (constraint_input and candidates_input): # We ran out of one or the other. BTW, your original code doesn't # consider the case of an overly constrained situation. return (candidates_input, constraint_input) constraint = constraint_input[0] # At first I was going to replace the call to `.sort()` by using # the free function `sorted` to maintain the "functional" theme. # However, you don't really need to sort the list, as far as I can # tell; you just need to find the minimum and use it as a basis for # comparison. violations = [ (len(re.findall(constraint, candidate)), candidate) for candidate in candidates_input ] # Now we create "all candidates with more than the minimum violations" minimal_violations = min(violations)[0] violators = [ candidate for violation_count, candidate in violations if violation_count > minimal_violations ] # And hence "all candidates without that many violations" good_candidates = [ candidate for candidate in input_candidates if candidate not in violators ] # And now we can recurse. return OT_eval(good_candidates, constraint_input[1:]) # Then, outside, we do the actual result processing and output: final_candidates, final_constraints = OT_eval(candidates, constraints) if final_constraints: print "No candidates survived the selection process." elif len(final_candidates) != 1: print "Couldn't determine a winner." else: print "The winner is:", final_candidates[0] Of course, now that the body of the function is cleaned up, you should be able to see trivially how to convert to iteration. Also, there's a needless complication here: since we're determining the number of violations for every candidate, it makes no sense to determine all the violators and filter them out: we could just determine all the good candidates directly via the opposite condition (left as an exercise).

Related questions

0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Dec 7, 2021 in Education by JackTerrance
0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    Point out the wrong statement. (a) Each universal function takes array inputs and produces array outputs (b) Broadcasting ... input arguments are ndarrays (d) All of the mentioned...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
    What are the inputs passed to OLS function from statsmodels.spi Module? (1)design matrix y (2)patsy formula (3)design matrices y and X (4)patsy formula and data dictionary...
asked Aug 17, 2021 in Technology by JackTerrance
0 votes
    state one advantage and one disavantage of using recursion over iteration Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    I need to use recursion in angular directives. Follow the code with the template without recursion. It is a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I want to understand the following code: struct element{ element *a1; element *a2; int value; }; ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Given the following F# snippet: type A(children: A list) = member val P1 = "" member val P2 = " ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    What is Recursion in Java? (a) Recursion is a class (b) Recursion is a process of defining a ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Which of these data types is used by operating system to manage the Recursion in Java? (a) Array ( ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    What is recursion?...
asked Jan 18, 2021 in Technology by JackTerrance
...