-3

enter image description here

what is causing issue in the code? here is the text of the code

import numpy as np
class Solution():
    def findMedianSortedArrays(n1,n2):
        a=0
        n1=np.array(n1)
        n2=np.array(n2)
        sum1=0
        n3=n1+n2
        for i in n3:
            sum1=sum1+i
            a=a+1
        return (sum1/a)
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 1
    When you call an instance method, the first argument is `self`, the object the method is bound to. If your method doesn't need `self`, it probably shouldn't be a method. – khelwood Aug 30 '20 at 00:15
  • 1
    As an addition to to @khelwood, if the method on a class doesn't need any class attributes then you can make it a static class method by adding `@staticmethod` above the `findMedianSortedArrays` method. – pythomatic Aug 30 '20 at 00:21

2 Answers2

1

The first default argument to a class method is self, in other words, the class instance itself. In your code, the n1 is going to take the value of the class instance, and n2 is going to take the value of 1, and there is no argument left for value 2, thus the error is thrown.

You can try print out your n1 and n2 to understand what I meant.

Solution:

def findMedianSortedArrays(self,n1,n2):
    ...

If you don't want to pass self, implement the method as staticmethod, like this:

@staticmethod
def findMedianSortedArrays(n1,n2):
    ...

This way, function call to findMedianSortedArrays can work with 2 arguments.

fusion
  • 1,327
  • 6
  • 12
0

The function invocation:

sum1 = Solution().findMedianSortedArrays(1, 2)

is equivalent to:

solution = Solution()
sum1 = solution.findMedianSortedArrays(1, 2)

Quoting from here: What is the purpose of self?

The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.

TLDR:

Use

def findMedianSortedArrays(self, n1, n2):
    ...

or make a static invocation:

sum1 = Solution.findMedianSortedArrays(1, 2)
Nihal Harish
  • 980
  • 5
  • 13
  • 31