美文网首页
C# wwm.LeetcodeHelper工具的使用

C# wwm.LeetcodeHelper工具的使用

作者: wwmin_ | 来源:发表于2022-02-06 15:36 被阅读0次

    说明

    本文是使用C#刷Leetcode的本地调试配置说明

    使用vscode配合命令行方式

    此文是使用vscode创建console项目刷leetcode题的步骤

    使用vs配置方式可参考作者的刷题链接作者刷leetcode代码库 gitee

    创建项目步骤

    新建一个文件夹,然后cmd到此文件夹路径执行如下命令

    //新建项目
    dotnet new console
    //添加leetcode帮助依赖
    dotnet add package wwm.leetcodehelper
    //添加操作配置文件依赖
    dotnet add package Microsoft.Extensions.Configuration
    dotnet add package Microsoft.Extensions.Configuration.UserSecrets
    //初始化用户机密文件
    dotnet user-secrets init
    //设置用户账户
    dotnet user-secrets set "user_password_login:email" "your email"
    //设置用户密码
    dotnet user-secrets set "user_password_login:password" "your password"
    //如果不适用账户密码登录则可再web端登录然后将web端的LEETCODE_SESSION cookie设置到机密文件即可,如下
    dotnet user-secrets set "cookie_login:LEETCODE_SESSION" "your cookie"
    //查看已添加的机密信息
    dotnet user-secrets list
    //查看package list
    dotnet list package
    //更新pacakge ,指定version
    dotnet add package wwm.leetcodehelper -v 0.1.1
    //安装依赖包
    dotnet restore
    

    添加.gitignore 文件

    .vs/
    bin/
    obj/
    /content
    cookies.txt
    

    初始化程序

    在Program.cs文件中写入如下内容

    global using System.Text;
    global using wwm.LeetCodeHelper;
    global using System.Text.Json;
    using Microsoft.Extensions.Configuration;
    #region init
    var config = new ConfigurationBuilder()
        .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
        .AddUserSecrets<Program>().Build();
    var config_cookie = config.GetSection("cookie_login:LEETCODE_SESSION")?.Value;
    if (!string.IsNullOrEmpty(config_cookie))
    {
        await LeetCodeHelper.InitAsync(config_cookie);
    }
    else
    {
        var config_email = config.GetRequiredSection("user_password_login:email")?.Value;
        var config_password = config.GetRequiredSection("user_password_login:password")?.Value;
        if (string.IsNullOrEmpty(config_email) || string.IsNullOrEmpty(config_password))
        {
            throw new Exception("cookie或者email/password 其中之一是必须的");
        }
        await LeetCodeHelper.InitAsync(config_email, config_password);
    }
    #endregion
    #if Release
    string url = "";
    if (string.IsNullOrEmpty(url))
    {
        TestResultHelper.InvokeAllTest();
    }
    else
    {
        await LeetCodeHelper.GetQuestionAsync(url);
    }
    #else
    //方式二: 方便刷每日一题
    await LeetCodeHelper.GetTodayQuestionOrInvokeTestAsync();
    #endif
    Console.Write("按任意键退出...");
    Console.ReadKey();
    

    配置vscode debug环境

    安装vscode插件 C#

    配置launch.json 和 tasks.json

    按F5 在下拉的选择环境框中选择 .NET5+ and .NET Core
    此时会创建launch.json,注意此时要修改configurations:program 下的参数:
    "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
    中的<insert-target-framework-here>修改为你自己的net6.0或这你自己的版本(此处推荐最新的net6.0),
    然后修改<insert-project-name-here>为你自己的项目名称,如我自己的leetcode_test,
    然后将configurations:console换成integratedTerminal.
    此时再在Program.cs文件下按F5,应该会弹出 Count not find the task 'build'警告框,此时选择Configure Task按钮
    然后在弹出选择框中选择Create task.json file from template,然后再在弹出的选择框中选择.NET Core.此时就在.vscode文件夹中创建好了tasks.json

    运行

    直接按F5就会编译,并启动下载LeetCode题,或者测试

    运行截图:


    运行测试

    开始调试自己的算法代码

    分两种方式:

    1. 使用自定义的题url的方式获取题目
      将 Program.cs中url值改成自己想要下载的题目地址
      如果开始测试,则将此url置空, 即可测试class中集成ITest接口的类文件了

    2. 使用每日一题方式刷题
      将方式1中的代码注释掉,或使用环境变量注释如#if DEBUG
      如果还没有拉取当天的每日一题,则会拉取题目.
      如果已拉取则自动开始测试继承ITest接口的类

    • 注意,不需要测试的类需要把ITest删除掉,防止无关的测试类

    编译错误解答

    问题1:
    如果出现了如下错误

    Exception has occurred: CLR/System.InvalidOperationException
    “System.InvalidOperationException”类型的异常在 System.Console.dll 中发生,但未在用户代码中进行处理: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'
    

    可将launch.json文件下的configurations:console换成integratedTerminal,或者删除Program.cs文件中的Console.ReadKey();这行代码

    问题2:
    如果出现了如下错误

    “Microsoft.Playwright.PlaywrightException”类型的异常在 System.Private.CoreLib.dll 中发生,但未在用户代码中进行处理: 'Chromium distribution 'chrome' is not found at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    Run "playwright install chrome"'
    

    需要安装playwright, 前往playwright官网
    执行

    # Install required browsers
    pwsh bin\Debug\netX\playwright.ps1 install
    

    mac 下执行pwsh时会出现bash: pwsh: command not found则需要下载安装powershell,地址https://github.com/PowerShell/PowerShell/releases
    选择合适的系统下载安装,然后再次执行

    # Install required browsers
    pwsh bin\Debug\netX\playwright.ps1 install
    # max下 \ 应变成 /
    

    如果出现

    Unhandled exception. Microsoft.Playwright.PlaywrightException: Chromium distribution 'chrome' is not found at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    Run "playwright install chrome"
       at Microsoft.Playwright.Transport.Connection.SendMessageToServerAsync[T](String guid, String method, Object args)
    

    则需要执行

    playwright install chrome
    

    相关链接

    作者:wwmin
    微信公众号: DotNet技术说
    本文链接:https://www.jianshu.com/p/b038e21032ab
    评论和私信会在第一时间回复。转载请注明出处!
    如果您觉得文章对您有帮助,关注点赞,您的鼓励是博主的最大动力!

    相关文章

      网友评论

          本文标题:C# wwm.LeetcodeHelper工具的使用

          本文链接:https://www.haomeiwen.com/subject/hrvukrtx.html