j178 / leetgo

Best LeetCode friend for geek. :snowboarder:
MIT License
535 stars 32 forks source link

[Bug] panic when picking question #273

Closed mkornat closed 9 months ago

mkornat commented 9 months ago

leetgo debug

Leetgo version info :

1.4
commit: 65d21e431204bc508012b3f6b6e9d1942d3a3857
built at: 2024-01-06T08:07:17Z
goos: linux
goarch: amd64

Home dir : /home/[user]/.config/leetgo Project root : /home/[user]/Work/learning/leetcode Working dir : /home/[user]/Work/learning/leetcode Project config file : /home/[user]/Work/learning/leetcode/leetgo.yaml Project configuration:

# Leetgo configuration file, see more at https://github.com/j178/leetgo

# Your name
author: user
# Language of the question description: zh or en
language: en
code:
  # Language of code generated for questions: go, cpp, python, java... 
  # (will be overridden by command line flag -l/--lang)
  lang: rust
  # The default template to generate filename (without extension), e.g. {{.Id}}.{{.Slug}}
  # Available attributes: Id, Slug, Title, Difficulty, Lang, SlugIsMeaningful
  # Available functions: lower, upper, trim, padWithZero, toUnderscore, group
  filename_template: '{{ .Id | padWithZero 4 }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  # Generate question description into a separate question.md file
  separate_description_file: true
  # Default modifiers for all languages
  modifiers:
    - name: removeUselessComments
  go:
    out_dir: go
    # Functions that modify the generated code
    modifiers:
      - name: removeUselessComments
      - name: changeReceiverName
      - name: addNamedReturn
      - name: addMod
  python3:
    out_dir: python
    # Python executable that creates the venv
    executable: python3
  cpp:
    out_dir: cpp
    # C++ compiler
    cxx: g++
    # C++ compiler flags (our Leetcode I/O library implementation requires C++17)
    cxxflags: -O2 -std=c++17
  rust:
    out_dir: rust
  java:
    out_dir: java
leetcode:
  # LeetCode site, https://leetcode.com or https://leetcode.cn
  site: https://leetcode.com
  # Credentials to access LeetCode
  credentials:
    # How to provide credentials: browser, cookies, password or none
    from: cookies
    # Browsers to get cookies from: chrome, safari, edge or firefox. If empty, all browsers will be tried
contest:
  # Base dir to put generated contest questions
  out_dir: contest
  # Template to generate filename of the question
  filename_template: '{{ .ContestShortSlug }}/{{ .Id }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  # Open the contest page in browser after generating
  open_in_browser: true
# Editor settings to open generated files
editor:
  # Use a predefined editor: vim, vscode, goland
  # Set to 'none' to disable, set to 'custom' to provide your own command
  use: none
  # Custom command to open files
  command: ""
  # Arguments to the command.
  # String contains {{.CodeFile}}, {{.TestFile}}, {{.DescriptionFile}}, {{.TestCasesFile}} will be replaced with corresponding file path.
  # {{.Folder}} will be substituted with the output directory.
  # {{.Files}} will be substituted with the list of all file paths.
  args: ""

Full configuration :

author: user
language: en
code:
  lang: rust
  filename_template: '{{ .Id | padWithZero 4 }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  separate_description_file: true
  modifiers:
    - name: removeUselessComments
  go:
    out_dir: go
    modifiers:
      - name: removeUselessComments
      - name: changeReceiverName
      - name: addNamedReturn
      - name: addMod
  python3:
    out_dir: python
    executable: python3
  cpp:
    out_dir: cpp
    cxx: g++
    cxxflags: -O2 -std=c++17
  rust:
    out_dir: rust
  java:
    out_dir: java
leetcode:
  site: https://leetcode.com
  credentials:
    from: cookies
    browsers: []
contest:
  out_dir: contest
  filename_template: '{{ .ContestShortSlug }}/{{ .Id }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'
  open_in_browser: true
editor:
  use: none
  command: ""
  args: ""

Debug log

2024/01/26 22:54:36 DEBU cache loaded path=/home/[user]/.config/leetgo/cache/leetcode-questions.json elapsed=15.929004ms
2024/01/26 22:54:36 DEBU request method=POST url=https://leetcode.com/graphql
  body=
  │ {"operationName":"questionData","query":"\n\tquery questionData($titleSlug: String!) {\n\t\tquestion(titleSlug: $titleSlug) {\n\t\t\tquestionId\n\t\t\tquestionFrontendId\n\t\t\tcategoryTitle\n\t\t\ttitle\n\t\t\ttitleSlug\n\t\t\tcontent\n\t\t\tisPaidOnly\n\t\t\ttranslatedTitle\n\t\t\ttranslatedContent\n\t\t\tdifficulty\n\t\t\tstatus\n\t\t\tstats\n\t\t\thints\n\t\t\tsimilarQuestions\n\t\t\tsampleTestCase\n\t\t\texampleTestcases\n\t\t\texampleTestcaseList\n\t\t\tmetaData\n\t\t\tcodeSnippets {\n\t\t\t\tlang\n\t\t\t\tlangSlug\n\t\t\t\tcode\n\t\t\t}\n\t\t\ttopicTags {\n\t\t\t\tname\n\t\t\t\tslug\n\t\t\t\ttranslatedName\n\t\t\t}\n\t\t}\n\t}","variables":{"titleSlug":"two-sum"}}

2024/01/26 22:54:36 DEBU response url=https://leetcode.com/graphql code=200
  headers=
  │ Cf-Cache-Status: DYNAMIC\r
  │ Cf-Ray: 84bbfed05f2a34e6-WAW\r
  │ Content-Encoding: gzip\r
  │ Content-Type: application/json\r
  │ Nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}\r
  │ Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=UCYBiueDT6S%2B8cPBfOzqYKqZ8EtYwUk7mT50U5jaaLG319mNgZ2b%2Fpyizu0W4ujYSE%2FcpSgmhj2xQ5lLbxEwNPUBzwwSf1G%2BCJuTpMhdngZerULj%2FA43QVNsy1eacA%3D%3D"}],"group":"cf-nel","max_age":604800}\r
  │ Server: cloudflare\r
  │ X-Content-Type-Options: nosniff\r
 data="{\"data\":{\"question\":{\"questionId\":\"1\",\"questionFrontendId\":\"1\",\"categoryTitle\":\"Algorithms\",\"title\":\"Two Sum\",\"titleSlug\":\"two-sum\",\"content\":\"<p>Given an array of integers <code>nums</code>&nbsp;and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>\n\n<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>\n\n<p>You can return the answer in any order.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\\"example\\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,7,11,15], target = 9\n<strong>Output:</strong> [0,1]\n<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].\n</pre>\n\n<p><strong class=\\"example\\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,2,4], target = 6\n<strong>Output:</strong> [1,2]\n</pre>\n\n<p><strong class=\\"example\\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,3], target = 6\n<strong>Output:</strong> [0,1]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>\n\t<li><strong>Only one valid answer exists.</strong></li>\n</ul>\n\n<p>&nbsp;</p>\n<strong>Follow-up:&nbsp;</strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code><font face=\\"monospace\\">&nbsp;</font>time complexity?\",\"isPaidOnly\":false,\"translatedTitle\":null,\"translatedContent\":null,\"difficulty\":\"Easy\",\"status\":null,\"stats\":\"{\\"totalAccepted\\": \\"12.1M\\", \\"totalSubmission\\": \\"23.4M\\", \\"totalAcceptedRaw\\": 12068392, \\"totalSubmissionRaw\\": 23368562, \\"acRate\\": \\"51.6%\\"}\",\"hints\":[\"A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.\",\"So, if we fix one of the numbers, say <code>x</code>, we have to scan the entire array to find the next number <code>y</code> which is <code>value - x</code> where value is the input parameter. Can we change our array somehow so that this search becomes faster?\",\"The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?\"],\"similarQuestions\":\"[{\\"title\\": \\"3Sum\\", \\"titleSlug\\": \\"3sum\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"4Sum\\", \\"titleSlug\\": \\"4sum\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Two Sum II - Input Array Is Sorted\\", \\"titleSlug\\": \\"two-sum-ii-input-array-is-sorted\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Two Sum III - Data structure design\\", \\"titleSlug\\": \\"two-sum-iii-data-structure-design\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Subarray Sum Equals K\\", \\"titleSlug\\": \\"subarray-sum-equals-k\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Two Sum IV - Input is a BST\\", \\"titleSlug\\": \\"two-sum-iv-input-is-a-bst\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Two Sum Less Than K\\", \\"titleSlug\\": \\"two-sum-less-than-k\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Max Number of K-Sum Pairs\\", \\"titleSlug\\": \\"max-number-of-k-sum-pairs\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Count Good Meals\\", \\"titleSlug\\": \\"count-good-meals\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Count Number of Pairs With Absolute Difference K\\", \\"titleSlug\\": \\"count-number-of-pairs-with-absolute-difference-k\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Number of Pairs of Strings With Concatenation Equal to Target\\", \\"titleSlug\\": \\"number-of-pairs-of-strings-with-concatenation-equal-to-target\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Find All K-Distant Indices in an Array\\", \\"titleSlug\\": \\"find-all-k-distant-indices-in-an-array\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"First Letter to Appear Twice\\", \\"titleSlug\\": \\"first-letter-to-appear-twice\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Number of Excellent Pairs\\", \\"titleSlug\\": \\"number-of-excellent-pairs\\", \\"difficulty\\": \\"Hard\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Number of Arithmetic Triplets\\", \\"titleSlug\\": \\"number-of-arithmetic-triplets\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Node With Highest Edge Score\\", \\"titleSlug\\": \\"node-with-highest-edge-score\\", \\"difficulty\\": \\"Medium\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Check Distances Between Same Letters\\", \\"titleSlug\\": \\"check-distances-between-same-letters\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Find Subarrays With Equal Sum\\", \\"titleSlug\\": \\"find-subarrays-with-equal-sum\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Largest Positive Integer That Exists With Its Negative\\", \\"titleSlug\\": \\"largest-positive-integer-that-exists-with-its-negative\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Number of Distinct Averages\\", \\"titleSlug\\": \\"number-of-distinct-averages\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}, {\\"title\\": \\"Count Pairs Whose Sum is Less than Target\\", \\"titleSlug\\": \\"count-pairs-whose-sum-is-less-than-target\\", \\"difficulty\\": \\"Easy\\", \\"translatedTitle\\": null}]\",\"sampleTestCase\":\"[2,7,11,15]\n9\",\"exampleTestcases\":\"[2,7,11,15]\n9\n[3,2,4]\n6\n[3,3]\n6\",\"exampleTestcaseList\":[\"[2,7,11,15]\n9\",\"[3,2,4]\n6\",\"[3,3]\n6\"],\"metaData\":\"{\n  \\"name\\": \\"twoSum\\",\n  \\"params\\": [\n    {\n      \\"name\\": \\"nums\\",\n      \\"type\\": \\"integer[]\\"\n    },\n    {\n      \\"name\\": \\"target\\",\n      \\"type\\": \\"integer\\"\n    }\n  ],\n  \\"return\\": {\n    \\"type\\": \\"integer[]\\",\n    \\"size\\": 2\n  },\n  \\"manual\\": false\n}\",\"codeSnippets\":[{\"lang\":\"C++\",\"langSlug\":\"cpp\",\"code\":\"class Solution {\npublic:\n    vector<int> twoSum(vector<int>& nums, int target) {\n        \n    }\n};\"},{\"lang\":\"Java\",\"langSlug\":\"java\",\"code\":\"class Solution {\n    public int[] twoSum(int[] nums, int target) {\n        \n    }\n}\"},{\"lang\":\"Python\",\"langSlug\":\"python\",\"code\":\"class Solution(object):\n    def twoSum(self, nums, target):\n        \\"\\"\\"\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[int]\n        \\"\\"\\"\n        \"},{\"lang\":\"Python3\",\"langSlug\":\"python3\",\"code\":\"class Solution:\n    def twoSum(self, nums: List[int], target: int) -> List[int]:\n        \"},{\"lang\":\"C\",\"langSlug\":\"c\",\"code\":\"/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* twoSum(int* nums, int numsSize, int target, int* returnSize) {\n    \n}\"},{\"lang\":\"C#\",\"langSlug\":\"csharp\",\"code\":\"public class Solution {\n    public int[] TwoSum(int[] nums, int target) {\n        \n    }\n}\"},{\"lang\":\"JavaScript\",\"langSlug\":\"javascript\",\"code\":\"/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[]}\n */\nvar twoSum = function(nums, target) {\n    \n};\"},{\"lang\":\"TypeScript\",\"langSlug\":\"typescript\",\"code\":\"function twoSum(nums: number[], target: number): number[] {\n    \n};\"},{\"lang\":\"PHP\",\"langSlug\":\"php\",\"code\":\"class Solution {\n\n    /**\n     * @param Integer[] $nums\n     * @param Integer $target\n     * @return Integer[]\n     */\n    function twoSum($nums, $target) {\n        \n    }\n}\"},{\"lang\":\"Swift\",\"langSlug\":\"swift\",\"code\":\"class Solution {\n    func twoSum(_ nums: [Int], _ target: Int) -> [Int] {\n        \n    }\n}\"},{\"lang\":\"Kotlin\",\"langSlug\":\"kotlin\",\"code\":\"class Solution {\n    fun twoSum(nums: IntArray, target: Int): IntArray {\n        \n    }\n}\"},{\"lang\":\"Dart\",\"langSlug\":\"dart\",\"code\":\"class Solution {\n  List<int> twoSum(List<int> nums, int target) {\n    \n  }\n}\"},{\"lang\":\"Go\",\"langSlug\":\"golang\",\"code\":\"func twoSum(nums []int, target int) []int {\n    \n}\"},{\"lang\":\"Ruby\",\"langSlug\":\"ruby\",\"code\":\"# @param {Integer[]} nums\n# @param {Integer} target\n# @return {Integer[]}\ndef two_sum(nums, target)\n    \nend\"},{\"lang\":\"Scala\",\"langSlug\":\"scala\",\"code\":\"object Solution {\n    def twoSum(nums: Array[Int], target: Int): Array[Int] = {\n        \n    }\n}\"},{\"lang\":\"Rust\",\"langSlug\":\"rust\",\"code\":\"impl Solution {\n    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {\n        \n    }\n}\"},{\"lang\":\"Racket\",\"langSlug\":\"racket\",\"code\":\"(define/contract (two-sum nums target)\n  (-> (listof exact-integer?) exact-integer? (listof exact-integer?))\n  )\"},{\"lang\":\"Erlang\",\"langSlug\":\"erlang\",\"code\":\"-spec two_sum(Nums :: [integer()], Target :: integer()) -> [integer()].\ntwo_sum(Nums, Target) ->\n  .\"},{\"lang\":\"Elixir\",\"langSlug\":\"elixir\",\"code\":\"defmodule Solution do\n  @spec two_sum(nums :: [integer], target :: integer) :: [integer]\n  def two_sum(nums, target) do\n    \n  end\nend\"}],\"topicTags\":[{\"name\":\"Array\",\"slug\":\"array\",\"translatedName\":null},{\"name\":\"Hash Table\",\"slug\":\"hash-table\",\"translatedName\":null}]}}}"
panic: interface conversion: interface {} is nil, not []interface {}

goroutine 1 [running]:
github.com/j178/leetgo/lang.addBinSection(0xc0000f43f0)
    github.com/j178/leetgo/lang/rust.go:384 +0x7b7
github.com/j178/leetgo/lang.generate(0xc000fa9180)
    github.com/j178/leetgo/lang/gen.go:103 +0x57f
github.com/j178/leetgo/lang.Generate(0xc000fa9180)
    github.com/j178/leetgo/lang/gen.go:126 +0x2a
github.com/j178/leetgo/cmd.glob..func16(0x203bc60?, {0xc000452700, 0x1, 0x13452c3?})
    github.com/j178/leetgo/cmd/pick.go:135 +0x205
github.com/spf13/cobra.(*Command).execute(0x203bc60, {0xc0004526d0, 0x1, 0x1})
    github.com/spf13/cobra@v1.8.0/command.go:983 +0xabc
github.com/spf13/cobra.(*Command).ExecuteC(0x203b3c0)
    github.com/spf13/cobra@v1.8.0/command.go:1115 +0x3ff
github.com/spf13/cobra.(*Command).Execute(...)
    github.com/spf13/cobra@v1.8.0/command.go:1039
github.com/j178/leetgo/cmd.Execute()
    github.com/j178/leetgo/cmd/root.go:51 +0x1e
main.main()
    github.com/j178/leetgo/main.go:6 +0xf

Description

I've got this panic message when when running command: leetgo pick two-sum right after configuring with leetgo init -t us -l rust

j178 commented 9 months ago

Thanks for the report! This should be fixed in v1.4.1.