In preparation for an exam in python I came across the following question:
Write a recursive function "minus_plus" that receives a list containing non-zero integers. The function should return "True" if the length of the list is even, and for every element in the list there exists a matching pair (for example if 5 is in the list, so as (-5)). In any other case, the function will return False.
For example:
- minus_plus([-5,-7,2,5,-2,7]) = True
- minus_plus([5,-5,3,5]) = False (-3 is not in the list)
- minus_plus([5,-5,5]) = False (length of the list is odd)
We are not allowed to use extra lists (create additional lists inside the function). We are not allowed to modify the given list using any list methods (i.e "sort(), sorted(), etc.).
We are allowed to use "helping" recursive functions, and parameter overloading (I am not sure what that means), nor are we allowed to use the "in" operator.
Here is the beginning of the code I wrote, I couldn't complete the recursion:
def minus_plus(lst):
if len(lst) <= 1:
return False
elif len(lst)%2 != 0: #This deals with lst with len(lst)%2==1
return False
elif len(lst)==2: #The "optimal" case, possibly the base case for the recursion
return lst[0]==(-1)*lst[1]
In preparation for an exam in python I came across the following question:
Write a recursive function "minus_plus" that receives a list containing non-zero integers. The function should return "True" if the length of the list is even, and for every element in the list there exists a matching pair (for example if 5 is in the list, so as (-5)). In any other case, the function will return False.
For example:
- minus_plus([-5,-7,2,5,-2,7]) = True
- minus_plus([5,-5,3,5]) = False (-3 is not in the list)
- minus_plus([5,-5,5]) = False (length of the list is odd)
We are not allowed to use extra lists (create additional lists inside the function). We are not allowed to modify the given list using any list methods (i.e "sort(), sorted(), etc.).
We are allowed to use "helping" recursive functions, and parameter overloading (I am not sure what that means), nor are we allowed to use the "in" operator.
Here is the beginning of the code I wrote, I couldn't complete the recursion:
def minus_plus(lst):
if len(lst) <= 1:
return False
elif len(lst)%2 != 0: #This deals with lst with len(lst)%2==1
return False
elif len(lst)==2: #The "optimal" case, possibly the base case for the recursion
return lst[0]==(-1)*lst[1]
Share
Improve this question
edited yesterday
Daniel
asked 2 days ago
DanielDaniel
1374 bronze badges
7
|
Show 2 more comments
2 Answers
Reset to default 1This seems like hopeless overkill (and highly inefficient) for what amounts to simply iterating through a list, but it appears to meet your criteria requiring a "recursive" function that doesn't change the original list or create another one.
Most non-trivial recursions are likely to either copy your list or create a reduced list in the function call itself. (For example, you could .find( -value )
and then make a recursive call with a new list recreated from the old list without the value/-value elements. Unclear if that fits your criteria.)
def minus_plus( L, i = 0 ):
if i >= len( L ):
return True # Take your pick, depending on what you want to
# return len( L ) > 0 # answer for an empty list
if len( L ) % 2 == 1:
return False
value = L[i]
if L.count( value ) != L.count( -value ):
return False
else:
return minus_plus( L, i + 1)
someTests = [ [], [-5,-7,2,5,-2,7], [5,-5,3,5], [5,-5,5], [5,-5,-5,-5], [2, -2], [2,2] ]
for test in someTests: print( test, '--->', minus_plus( test ) )
Output:
[] ---> True
[-5, -7, 2, 5, -2, 7] ---> True
[5, -5, 3, 5] ---> False
[5, -5, 5] ---> False
[5, -5, -5, -5] ---> False
[2, -2] ---> True
[2, 2] ---> False
If you are not having your hands tied with recursions, you can simply run
def minus_plus(lst):
return sorted(lst) == sorted(map(lambda x: -x, lst))
However, as per your constraints, i.e., recursion + no in
+ no new list
+ no list annotation, you probably could try an option like below
def minus_plus(lst):
if len(lst)%2:
return False
if len(lst)==2:
return sum(lst)==0
return (
minus_plus(lst[1:-1]) and (lst[0] == -lst[-1])
if sorted(lst) == lst
else minus_plus(sorted(lst))
)
and you will see
print(minus_plus([-5, -7, 2, 5, -2, 7]))
# True
print(minus_plus([5, -5, 3, 5]))
# False
print(minus_plus([5, -5, 5]))
# False
minus_plus([5,-5,-5,-5])
? – no comment Commented 2 days ago