LeetCodeCopyTestcase2

自动将 LeetCode 测试用例转化为 SQL 和 Pandas 语句

目前為 2024-11-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         LeetCodeCopyTestcase2
// @namespace    https://leetcode.cn/
// @version      0.1
// @description  自动将 LeetCode 测试用例转化为 SQL 和 Pandas 语句
// @author       wangxm
// @match        https://leetcode.cn/problems/*
// @match        https://leetcode-cn.com/submissions/detail/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 获取表的对象集合信息
    let createTableSQL = "";
    let insertSQL = "";
    let dropTableSQL = "";
    let pandasCode = "";

    const tableInfos = JSON.parse(pageData.submissionData.input).headers;
    const tableRows = JSON.parse(pageData.submissionData.input).rows;

    // 生成表创建和删除语句
    for (const name in tableInfos) {
        const columns = tableInfos[name].map(col => `${col} VARCHAR(200)`);
        createTableSQL += `CREATE TABLE IF NOT EXISTS ${name} (${columns.join(", ")});\n`;
        dropTableSQL += `DROP TABLE IF EXISTS ${name};\n`;

        // 生成 Pandas 代码
        const pandasColumns = tableInfos[name].map(col => `"${col}"`);
        const pandasData = tableRows[name].map(row => `[${row.map(value => value === null ? "None" : `"${value}"`).join(", ")}]`);
        pandasCode += `import pandas as pd\n${name}_df = pd.DataFrame(columns=[${pandasColumns.join(", ")}], data=[${pandasData.join(", ")}])\n\n`;
    }

    // 处理数据并生成插入语句
    for (const tableName in tableRows) {
        tableRows[tableName].forEach(row => {
            const values = row.map(value => value === null ? "NULL" : `"${value}"`);
            insertSQL += `INSERT INTO ${tableName} VALUES (${values.join(", ")});\n`;
        });
    }

    // 添加到页面上
    $("#details-summary").append(`
        <input type="button" value="复制 SQL" id="copy-sql" class="btn btn-primary">
        <input type="button" value="复制 Pandas" id="copy-pandas" class="btn btn-primary" style="margin-left:10px">
        <input type="button" value="隐藏" id="toggle" style="margin-left:10px" class="btn btn-primary">
        <textarea style="margin-top:10px;height:200px;display:block" id="sql" class="form-control"></textarea>
        <textarea style="margin-top:10px;height:200px;display:none" id="pandas" class="form-control"></textarea>
    `);

    $("#sql").val(dropTableSQL + createTableSQL + insertSQL);
    $("#pandas").val(pandasCode);

    // 添加滑动及其处理
    $("#toggle").click(function() {
        const isHidden = $("#sql").css("display") === "none";
        $("#sql").css("display", isHidden ? "block" : "none");
        $("#pandas").css("display", isHidden ? "none" : "block");
        $(this).val(isHidden ? "隐藏" : "查看");
    });

    $("#copy-sql").click(function() {
        const sqlTextarea = document.getElementById("sql");
        sqlTextarea.select();
        document.execCommand("Copy");
        alert("SQL 已复制好,可贴粘。");
    });

    $("#copy-pandas").click(function() {
        const pandasTextarea = document.getElementById("pandas");
        pandasTextarea.select();
        document.execCommand("Copy");
        alert("Pandas 代码已复制好,可贴粘。");
    });
})();