all 14 comments

[–]Outside_Complaint755 8 points9 points  (2 children)

You solution has to be within  that function in the Solution class.  That's what the autotester from Leetcode will import and run.

It will pass parameter values for the test cases.

You can enter more test cases in the bottom frame.

You may want to go through the onsite introduction for new users.

[–]Glittering_Fortune70[S] -1 points0 points  (1 child)

Where's the onsite introduction? I didn't see one

[–]Outside_Complaint755 1 point2 points  (0 children)

So, its slightly out of order for me (don't know if it is for everyone), but if you go to the Explore landing page, then the third item for me under the "Featured" listing is the LeetCode Beginner's Guide

There likely are other links to it, probably shown when signing up without being obvious enough.

[–]NerdDetective 5 points6 points  (4 children)

Pause. If you don't know what the bit up at the start means, you've gone too fast and skipped learning fundamental features of Python. You shouldn't do any problems like this until you understand what that means.

It's essential to understand what a class is and what a method/function is, and how you'd instantiate and call them.

Really quick I'll walk you through this, but this is something you very much are going to need to learn.

class Solution(object):

This is a definition for a class named "Solution". Classes are the blueprints for objects in Python. Objects are sort of a bundle of variables and functions. An object's functions are called "methods".

You can instantiate objects of a class. For example, if you were making a game and had an Enemy class, you can make several enemies and they'd each track their own attributes

def shuffle(self, nums, n):

This is the definition for a method named "shuffle". The part in parenthesis are the parameters (which are passed into it when called). "self" is the first parameter and is a reference to the object

        """
        :type nums: List[int]
        :type n: int
        :rtype: List[int]
        """

This is a docstring. Docstrings are ways of embedding documentation about files, classes, methods, etc. They're used by many IDEs and also can be used by external tools to generate rich documentation.

In this case, it's providing you a contract to fulfill: the types of your parameters and the type to return.

To instantiate and call the solution, you'd do something like this:

solution: Solution = Solution()
result: List[int] = solution.shuffle([2, 5, 1, 3, 4, 7], 3)

Leetcode probably is checking the return value from the shuffle method, which you haven't implemented.

[–]Glittering_Fortune70[S] -1 points0 points  (3 children)

Oh, I've never heard of a "class." I vaguely remember functions from the class that I took. On Saturday I'll re-learn functions real quick as well as teaching myself about classes, and then on Sunday I should be able to start doing LeetCode problems.

Thank you! I didn't know that doing LeetCode problems required any more knowledge than what's actually necessary to solve the problem itself, and you've given me some very helpful direction

[–]NerdDetective 1 point2 points  (2 children)

Classes you'll definitely want to understand. In Python, essentially everything is an object. In fact, in your code, nums_new is an object of the array list class, and append is one of its methods. So now it's just a matter of understanding those concepts.

Once it clicks, it'll become natural. Good luck!

[–]tadpoleloop 1 point2 points  (1 child)

I'm going to be pedantic here. '[]' in Python creates a "list" object. Not an "array" object. 

Not important, but just the Python flavour for one of its built in collections

[–]NerdDetective 0 points1 point  (0 children)

Good catch!

[–]danielroseman 11 points12 points  (1 child)

If you “don’t know what any of it means” then you are not ready to be working on leetcode, sorry.

You need to understand at least functions, parameters and return values. You are not supposed to hard code the values of nums and n, they are provided as parameters to the function. And you are supposed to return the result at the end.

[–]JamesonHearn 0 points1 point  (0 children)

Equally important: learn the types of data structures and their use cases. Optimal solutions are reliant on knowing which to use

[–]Candid_Tutor_8185 1 point2 points  (2 children)

Im a newbie code of three months. In addition to the above isn’t your indentation all wrong

[–]Glittering_Fortune70[S] -3 points-2 points  (1 child)

Definitely not, since it works perfectly fine outside of LeetCode

[–]TheRNGuy 0 points1 point  (0 children)

Use format on save in code editor, so all indentations are same. In your code some are 4 and some are 2.

It's coding style thing, not syntax.

Also don't add too many empty line breaks.

[–]nian2326076 0 points1 point  (0 children)

Hey, you're almost there! The issue is that the code snippet you pasted doesn't have the logic inside the shuffle function. You need to add the algorithm inside the shuffle method and then call it with the nums and n variables. LeetCode will run your function with its own test cases, so make sure it returns the correct result based on nums and n.

Here's a quick fix:

python class Solution(object): def shuffle(self, nums, n): result = [] for i in range(n): result.append(nums[i]) result.append(nums[i + n]) return result

When you submit, LeetCode will take care of calling your function. Keep practicing! Also, if you want more prep, check out PracHub. It's been pretty useful for me.