Dec-23-2018, 10:10 AM
(This post was last modified: Dec-23-2018, 10:11 AM by Gribouillis.)
Recursion works by supposing that you have the solution for a shorter copy of the problem. In your case, assuming that
In the case where
list2
is not empty, the listresult = compare(list1, list2[1:])contains all the items that are in
list1
but not in list2[1:]
or in list2[1:]
but not in list1
. If list2[0]
is in list1
, it needs to be removed from the result. If it is not in list1
, it needs to be added to the result.In the case where
list2
is empty you can return a copy of list1
(don't return list1
itself because the result is modified by the recursive calls)