0
class Solution:
    def cuttingRope(self,n):
        dp = [0] * (n + 1)
        dp[2] = 1
        dp[1] = 1
        for i in range(3, n + 1):
            for j in range(i):
                dp[i] = max(dp[i], max(dp[i - j] * j, (i - j) * j))
        return dp[n]


print(Solution.cuttingRope(Solution, 10))

In the print statement, the "self" parameter is designated as the class "Solution", I don't understand this very well, can you give me a simple explanation

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • Well, *you are passing that parameter*. What is there to explain? Nothing is done with `self`, so you could pass whatever you want, as long as you pass something, because it is a required positional parameter – juanpa.arrivillaga Nov 24 '20 at 09:00
  • 4
    Does this answer your question? [What is the purpose of the word 'self'?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self) – Daemon Painter Nov 24 '20 at 09:01
  • That's a method, not just a function. This should have been covered by [the Python tutorial](https://docs.python.org/3/tutorial/index.html). – MisterMiyagi Nov 24 '20 at 09:06
  • If you create an instance of `Solution` and then call `cuttingRope` using that instance you do not need to pass `Solution` as `self`: `sol = Solution(); print(sol.cuttingRope(10))` – Sash Sinha Nov 24 '20 at 09:06
  • 1
    When writing a class with a method (which is not designated as `@classmethod`), then you're supposed to use it like `s = Solution(); s.cuttingRope(10)`. If you don't need a class—and there doesn't appear to be any obvious reason here why you would—then just write it as a plain function instead…?! – deceze Nov 24 '20 at 09:07
  • Note: The ``class Solution:`` (anti-) pattern is expected by some coding challenge sites. – MisterMiyagi Nov 24 '20 at 09:55

0 Answers0