0
class Solution:
  def remove(self,arr,target):
      if target in arr:
        arr.remove(target)
        remove(arr,target)
        return len(arr),arr
      else:
        return "not in the array"
ans=Solution()
print(ans.remove([3,2,2,3],3))

This is the Error

Traceback (most recent call last):
  File "c:\Users\ashut\Practice\scrap.py", line 10, in <module>
    print(ans.remove([3,2,2,3],3))
  File "c:\Users\ashut\Practice\scrap.py", line 5, in remove   
    remove(arr,target)
NameError: name 'remove' is not defined

Somehow the above program runs in google colab and I've tried restarting the runtime

3
  • Why are you trying to call remove recursively? Jan 19, 2021 at 6:51
  • It's what the error message says. There is no function named remove. There is a remove method in the class Solution but you are not calling that.
    – Selcuk
    Jan 19, 2021 at 6:52
  • In addition to what @Selcuk said, if you do want to call the remove method, you need to use self.remove(arr, target).
    – Roy Cohen
    Jan 19, 2021 at 7:02

2 Answers 2

2

You have to add the "self" tag before calling the function inside ur function.

     class Solution:
         def remove(self,arr,target):
         if target in arr:
             arr.remove(target)
             self.remove(arr,target)
             return len(arr),arr
         else:
             return "not in the array"
    ans=Solution()
    print(ans.remove([3,2,2,3],3))
0

The temp variable name is arr, is that mean Array? If so, the code "remove(arr, target)" is no need, remove this line, is OK.

Not the answer you're looking for? Browse other questions tagged or ask your own question.