Home > Software design >  NameError is not defined in leetcode problem
NameError is not defined in leetcode problem

Time:09-17

Please bear with me, I'm still getting used to OOP in python. I'm preparing for coding interviews, and find that Leetcode problems generally have the following structure.

class Solution:
   def solutionMethod(self, input):
      ...

I want to make an object to store and update data as I proceed through my solution. I structure my code as follows:

class Solution:

   def helperMethod(self, helperInput):
      self.var1 = helperInput[0]
      self.var2 = helperInput[1]
      self.var3 = self.var1   self.var2

   def solutionMethod(self, input):
      
      currObject = helperMethod(input)
      # do stuff with currObject
      ...

I get the nameerror: name 'helperMethod' is not defined.

I am thrown off because I would normally What am I doing wrong here? Do I need an init to be able to call helperMethod?

Is making a class like this generally a good idea when in the time constrained coding interview?

And is there anything I should be aware of with how leetcode (and coding interview stations) test my code? I imagine it runs as follows, is it correct?

soln = Solution()
soln.solutionMethod(input)

CodePudding user response:

You need to invoke the method on the self object:

class Solution:
   def solutionMethod(self, input):
      currObject = self.helperMethod(input)
  • Related