V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
udtrokia
V2EX  ›  分享创造

Rust 写了个 leetcode-cli, 诚邀大佬们来体验 🐳

  •  2
     
  •   udtrokia ·
    udtrokia · 2020-01-09 17:56:31 +08:00 · 3671 次点击
    这是一个创建于 1813 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本来想老老实实刷下 leetcode 年后认认真真找个工作...结果 20 道没刷够就跑神写了这个 proj,蛮有意思的,参考了 skygragon 大牛的 node 版本,在里面添了个 python 解释器,可以通过 python 脚本过滤题目。

    (粘一下 README 过来...)

    leetcode-cli

    doc Crates.io Crates.io LICENSE

    Features

    • [x] the edit flow —— solution files will generate automatically!
    • [x] support python scripts to filter questions
    • [ ] doc support, lc-rs can compile the annotation of your solutions to markdown!
    • [ ] support local signal to keep coding as longer as you want.

    Building

    cargo install leetcode-cli
    

    Usage

    Please make sure you have logined in leetcode.com with chrome, more info plz checkout this

    leetcode 0.2.6
    clearloop <[email protected]>
    Here's to the crazy ones 👻
    
    USAGE:
        leetcode [FLAGS] [SUBCOMMAND]
    
    FLAGS:
        -d, --debug      debug mode
        -h, --help       Prints help information
        -V, --version    Prints version information
    
    SUBCOMMANDS:
        data    Manage Cache [aliases: d]
        edit    Edit question by id [aliases: e]
        exec    Submit solution [aliases: x]
        list    List problems [aliases: l]
        pick    Pick a problem [aliases: p]
        stat    Show simple chart about submissions [aliases: s]
        test    Edit question by id [aliases: t]
        help    Prints this message or the help of the given subcommand(s)
    

    Example

    For example, if your config is:

    [code]
    lang = "rust"
    editor = "emacs"
    

    1. pick

    leetcode pick 1
    
    [1] Two Sum is on the run...
    
    
    Given an array of integers, return indices of the two numbers such that they add up to a specific target.
    
    You may assume that each input would have exactly one solution, and you may not use the same element twice.
    
    --------------------------------------------------
    
    Example:
    
    
    Given nums = [2, 7, 11, 15], target = 9,
    
    Because nums[0] + nums[1] = 2 + 7 = 9,
    return [0, 1].
    

    2. edit

    leetcode edit 1
    
    impl Solution {
        pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
            use std::collections::HashMap;
            let mut m: HashMap<i32, i32> = HashMap::new();
    
            for (i, e) in nums.iter().enumerate() {
                if let Some(v) = m.get(&(target - e)) {
                    return vec![*v, i as i32];
                }
    
                m.insert(*e, i as i32).unwrap_or_default();
            }
    
            return vec![];
        }
    }
    

    3. test

    leetcode test 1
    
    
      Accepted       Runtime: 0 ms
    
      Your input:    [2,7,11,15], 9
      Output:        [0,1]
      Expected:      [0,1]
    
    

    4. submit

    leetcode submit 1
    
    
      Success
    
      Runtime: 0 ms, faster than 100% of Rustonline submissions for Two Sum.
    
      Memory Usage: 2.4 MB, less than 100% of Rustonline submissions for Two Sum.
    
    
    

    Cookies

    The cookie plugin of leetcode-cil can work on OSX and Linux, If you are on other platforms or your cookies just don't want to be catched, you can handwrite your LeetCode Cookies to ~/.leetcode/leetcode.toml

    # Make sure `leetcode.toml` file is placed at `~/.leetcode/leetcode.toml`
    [cookies]
    csrf = "..."
    session = "..."
    

    For Example, if you're using chrome to login to leetcode.com.

    Step 1

    Open chrome and paste the link below to the chrome linkbar.

    chrome://settings/cookies/detail?site=leetcode.com
    

    Step 2

    Copy the contents of LEETCODE_SESSION and csrftoken.

    Step 3

    Paste them to session and csrf.

    # Make sure `leetcode.toml` file is placed at `~/.leetcode/leetcode.toml`
    [cookies]
    csrf = "${csrftoken}"
    session = "${LEETCODE_SESSION}"
    

    Programmable

    If we want to filter leetcode questions using our own python scripts, what should we do?

    For example, our config is:

    # Make sure `leetcode.toml` file is placed at `~/.leetcode/leetcode.toml`
    [storage]
    scripts = "scripts"
    

    We write our python scripts:

    # ~/.leetcode/scripts/plan1.py
    import json;
    
    def plan(sps, stags):
        ##
        # `print` in python is supported, 
        # if you want to know the data structures of these two args, 
        # just print them
        ##
        problems = json.loads(sps)
        tags = json.loads(stags)
    	
        ret = []
        tm = {}
        for tag in tags:
            tm[tag["tag"]] = tag["refs"];
    
        for i in problems:
            if i["level"] == 1 and str(i["id"]) in tm["linked-list"]:
                ret.append(str(i["id"]))
    
        # return is `List[string]`
        return ret
    

    Then we can run filter as what we write now:

    leetcode list -p plan1
    

    Well done, enjoy it!

    PR

    PR is welcome, here it is.

    LICENSE

    MIT


    欢迎来提 issues 和 pr~

    计划是在这一个多月内达到 medium 不卡壳的难度,如果碰巧有小伙伴这阵子也在刷 leetcode,请联系小子,求搭伙儿!

    8 条回复    2020-02-18 18:54:52 +08:00
    ZiLong
        1
    ZiLong  
       2020-01-09 22:05:46 +08:00
    点赞,顺便搭车看看有没有一起刷的....code 不易,期 leet code nan 上加难
    udtrokia
        2
    udtrokia  
    OP
       2020-01-10 00:27:52 +08:00
    在 Trello 上列了个比较平稳的计划 hhh

    https://trello.com/b/hGBvNy0r

    里面有微信群的二维码,一起刷的朋友点进来看看嘿嘿,Master LeetCode.
    hardwork
        3
    hardwork  
       2020-01-10 09:38:56 +08:00 via Android
    我也在学 rust,学着玩
    metrue
        4
    metrue  
       2020-01-11 15:06:12 +08:00 via iPhone
    因为时间问题,现在改为每周周赛打卡.
    f1ren2es
        5
    f1ren2es  
       2020-01-13 20:00:56 +08:00
    登录 leetcode 怎么做的呢,我记得之前 leetcode 登录用上了 recaptcha,然后就只能曲线救国用三方登录了。
    udtrokia
        6
    udtrokia  
    OP
       2020-01-14 12:34:20 +08:00
    @f1ren2es

    recaptcha 确实搞不定...直接去掉了账号密码,读 chrome cookies 登录 = =
    udtrokia
        7
    udtrokia  
    OP
       2020-01-14 12:51:53 +08:00
    打一下卡.

    + 前天做了 10 道 easy 的 Linked-List
    + 昨天做了 21 道 easy 的 tree

    Rust 刷 LeetCode 的感受是,我现在开始怀疑我以前写的都是假 Rust....好在越写越顺,昨天晚上的最后几道题都没有看讨论区的答案,包括之前面试 1 个小时做不出来的题,现在能够很轻松地写出来。

    趁着讨论区的 Rust 解法还总是突破不了个位数 LOL,也 Post 上去一些小子自己的答案。

    自学编程 3 年多,至今还是算法 0 基础(抱着我要是能够做出牛逼的项目,算法再渣也能混口饭吃的心态...) —— 打算这周奋发图强刷 300 道 easy,是不是怂货,够不够努力,自我验证下 🦀
    udtrokia
        8
    udtrokia  
    OP
       2020-02-18 18:54:52 +08:00
    更新一下哈哈

    事实证明 300 道题的计划失败了,认怂!

    刷完 easy 的链表和二叉树后,突然有灵感去做一个 wasm UI library,昨天刚刚阶段性完工,再一次开启刷题计划嘿嘿,但愿这次能够老老实实刷题直到找到工作吧!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5353 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:39 · PVG 15:39 · LAX 23:39 · JFK 02:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.