LeetCodeCopyTestcase2

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

2024-11-05 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name LeetCodeCopyTestcase2
  3. // @namespace https://leetcode.cn/
  4. // @version 0.1
  5. // @description 自动将 LeetCode 测试用例转化为 SQL 和 Pandas 语句
  6. // @author wangxm
  7. // @match https://leetcode.cn/problems/*
  8. // @match https://leetcode-cn.com/submissions/detail/*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 获取表的对象集合信息
  17. let createTableSQL = "";
  18. let insertSQL = "";
  19. let dropTableSQL = "";
  20. let pandasCode = "";
  21.  
  22. const tableInfos = JSON.parse(pageData.submissionData.input).headers;
  23. const tableRows = JSON.parse(pageData.submissionData.input).rows;
  24.  
  25. // 生成表创建和删除语句
  26. for (const name in tableInfos) {
  27. const columns = tableInfos[name].map(col => `${col} VARCHAR(200)`);
  28. createTableSQL += `CREATE TABLE IF NOT EXISTS ${name} (${columns.join(", ")});\n`;
  29. dropTableSQL += `DROP TABLE IF EXISTS ${name};\n`;
  30.  
  31. // 生成 Pandas 代码
  32. const pandasColumns = tableInfos[name].map(col => `"${col}"`);
  33. const pandasData = tableRows[name].map(row => `[${row.map(value => value === null ? "None" : `"${value}"`).join(", ")}]`);
  34. pandasCode += `import pandas as pd\n${name}_df = pd.DataFrame(columns=[${pandasColumns.join(", ")}], data=[${pandasData.join(", ")}])\n\n`;
  35. }
  36.  
  37. // 处理数据并生成插入语句
  38. for (const tableName in tableRows) {
  39. tableRows[tableName].forEach(row => {
  40. const values = row.map(value => value === null ? "NULL" : `"${value}"`);
  41. insertSQL += `INSERT INTO ${tableName} VALUES (${values.join(", ")});\n`;
  42. });
  43. }
  44.  
  45. // 添加到页面上
  46. $("#details-summary").append(`
  47. <input type="button" value="复制 SQL" id="copy-sql" class="btn btn-primary">
  48. <input type="button" value="复制 Pandas" id="copy-pandas" class="btn btn-primary" style="margin-left:10px">
  49. <input type="button" value="隐藏" id="toggle" style="margin-left:10px" class="btn btn-primary">
  50. <textarea style="margin-top:10px;height:200px;display:block" id="sql" class="form-control"></textarea>
  51. <textarea style="margin-top:10px;height:200px;display:none" id="pandas" class="form-control"></textarea>
  52. `);
  53.  
  54. $("#sql").val(dropTableSQL + createTableSQL + insertSQL);
  55. $("#pandas").val(pandasCode);
  56.  
  57. // 添加滑动及其处理
  58. $("#toggle").click(function() {
  59. const isHidden = $("#sql").css("display") === "none";
  60. $("#sql").css("display", isHidden ? "block" : "none");
  61. $("#pandas").css("display", isHidden ? "none" : "block");
  62. $(this).val(isHidden ? "隐藏" : "查看");
  63. });
  64.  
  65. $("#copy-sql").click(function() {
  66. const sqlTextarea = document.getElementById("sql");
  67. sqlTextarea.select();
  68. document.execCommand("Copy");
  69. alert("SQL 已复制好,可贴粘。");
  70. });
  71.  
  72. $("#copy-pandas").click(function() {
  73. const pandasTextarea = document.getElementById("pandas");
  74. pandasTextarea.select();
  75. document.execCommand("Copy");
  76. alert("Pandas 代码已复制好,可贴粘。");
  77. });
  78. })();