Skip to content

define the part of code to submit #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
magic-akari opened this issue Sep 15, 2019 · 5 comments
Closed

define the part of code to submit #418

magic-akari opened this issue Sep 15, 2019 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@magic-akari
Copy link
Contributor

🚀 Feature Proposal

We need a magic comment to define where is the actual user code to submit.

Motivation

I am playing with rust. The code structure is as follow.

pub struct Solution;

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
    // ...
    }
}

look at the first line(Line 9).
I will get an error in vs code without that line.
And I will get an error if I submit code with that line.

✘ Compile Error
  ✘ 0/0 cases passed (N/A)
  ✘ Error: Line 54, Char 1: the name `Solution` is defined multiple times (solution.rs)
  ✘ Error: Line 54, Char 1: the name `Solution` is defined multiple times (solution.rs)
   |
9 | pub struct Solution {}
   | ------------------- previous definition of the type `Solution` here
...
54 | struct Solution;
   | ^^^^^^^^^^^^^^^^ `Solution` redefined here
   |
   = note: `Solution` must be defined only once in the type namespace of this module

The Line 54 is at LeetCode test side.

It is better to define which is the actual user code to submit and which is not.


For example:

pub struct Solution;

// @lc user code start

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
        unimplemented!()
    }
}

// @lc user code end

#[cfg(test)]
mod tests {
    use super::Solution;

    #[test]
    fn it_works() {
        assert_eq!(vec![1, 2], Solution::two_sum(vec![3, 2, 4], 6));
    }
}
# @lc user code start


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        pass

# @lc user code end


if __name__ == '__main__':
    s = Solution()
    print s.twoSum([3, 2, 4], 6)
@jdneo jdneo added the enhancement New feature or request label Sep 16, 2019
@jdneo
Copy link
Member

jdneo commented Sep 16, 2019

So each time we will extract out the content between the special tag comments, save it to a temp file and submit through CLI.

Another usage for this tag is to provide a better place to put the Code Lens (issue #407 ). Currently the Code Lens is always put at the end of the file.

@magic-akari Would you like to contribute for this? I guess we need also update the template here: https://github.com/leetcode-tools/leetcode-cli/tree/master/templates

@magic-akari
Copy link
Contributor Author

I tried to make a prototype here leetcode-tools/leetcode-cli#23
Would you like to review it?

@jdneo
Copy link
Member

jdneo commented Sep 16, 2019

Thanks, I just left some comments there.

@bethandtownes
Copy link

I have a python script that does it better. It does the following things:

  1. generate a local runnable cpp file linked to my own header only cpp-stl pretty pretty printer.
  2. run it and test against the test cases.
  3. also generate a submittable file without the main and all the includes.

The most straight forward way to do this is to define a runtests functions inside the solution class and only call the runtests function inside the main. Scripting becomes much easier.

Let me know if you would like to make this a multi-language script. Right now what is missing is the ability to catch failed cases and convert it to appropriate types in the language. However, this is easily achievable if you are willing to use some Haskell or Ocaml to do pattern match!

@jdneo
Copy link
Member

jdneo commented Sep 30, 2019

Thank you @jasonsun0310 ,

So far the problem is addressed in leetcode-tools/leetcode-cli#23.

Let's see if the users need more powerful support later...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants