【ChatGPT Commands Encyclopedia】How to use ChatGPT to assist program development

Table of contents

write programs

unscramble code

refactor code

solution bug

write tests

write Regex

Summarize


In today's fast-paced digital world, programming is becoming more and more important and commonplace. Whether developing applications, websites, or other software, the need for programming is constantly increasing. However, sometimes we may encounter various problems that affect our work progress. If ChatGPT is used as an auxiliary tool, it will greatly improve work efficiency.

 

write programs

You are now an expert in [programming language], please help me write a function in [programming language] that needs to do [some function]

Question: You are now a JavaScript expert, please help me write a function in JavaScript, which needs to input a one-dimensional array and convert this one-dimensional array into a two-dimensional array. At the same time I want to be able to freely decide what the length of the subarrays in the 2D array will be

Answer: Of course, I can help you write this function. Here is a function implemented in JavaScript that converts a 1D array to a 2D array, dividing it by a given subarray length:

function convertTo2DArray(arr, subarrayLength) {
  var result = [];
  for (var i = 0; i < arr.length; i += subarrayLength) {
    result.push(arr.slice(i, i + subarrayLength));
  }
  return result;
}

This function takes two arguments: `arr` is the input one-dimensional array, and `subarrayLength` is the length of the subarray.

We use a loop to iterate over the 1D array, then use the `slice` method to extract each subarray from the original array and push it into the new 2D array `result`. Finally return this new 2D array.

Here is an example showing how to use this function:

var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var subarrayLength = 3;
var result = convertTo2DArray(arr, subarrayLength);
console.log(result);

In this example, we convert the 1D array `[1, 2, 3, 4, 5, 6, 7, 8]` into a 2D array where each subarray has length 3. The output is as follows:
[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8]
]
 

Hope this function can meet your needs! If you have any questions, please feel free to ask me.

 

unscramble code

You are now a [programming language] expert, please tell me what the following code is doing. [attach code]

refactor code

You are now a Clean Code expert, I have the following code, please rewrite it in a cleaner and more concise way, so that my colleagues can maintain the code more easily. In addition, explain why you want to refactor in this way, so that I can add the description of the refactoring method to the Pull Request. [attach code]

solution bug

You are now a [programming language] expert, I have a piece of code, I expect this code to [do something], it just fails the test case [test case]. Please help me find out where I am writing wrong and rewrite it in the correct way. [attach code]

You are now a python expert, and I have a piece of code that I expect can determine whether a string is a mirror palindrome, but it fails the aacdeedcc test case. Please help me find out where I am writing wrong and rewrite it in the correct way. [attach code]

write tests

You are now a [programming language] expert, I have a piece of code [attach code], please help me write a test, please provide at least five test cases, including extreme cases, so that I can determine this The output of the snippet code is correct.

 

write Regex

You are now a Regex expert, please help me write a Regex that takes [requirement]

Question: You are now a Regex expert, please help me write a Regex, which can input a string and extract all the numbers in the string

Answer: Of course, I can help you write a regular expression to extract all numbers in a string. Here's an example using JavaScript:

var str = "Hello, 123 World! 456";
var regex = /\d+/g;
var result = str.match(regex);
console.log(result);

In this example, we use the regular expression `/ \d+ /g` to match all digits in the string. This regular expression consists of two parts:

- `\d`: Match any one digit character.
- `+`: Indicates to match one or more consecutive numeric characters.

The `g` flag means glob matching, which matches all digits in the string.

Next, we use the `match` function to find all results in the string that match the regular expression and store them in the `result` variable.

In the above example, the input string is `"Hello, 123 World! 456"`, and the output result is an array `[ "123", "456" ]`, that is, all the numbers extracted from the string.

You can embed the above code into your JavaScript program as needed, and use different strings for testing. Hope this regular expression helps you! If you have any questions, please feel free to ask me.

Summarize

As an auxiliary tool for program development, ChatGPT provides a powerful resource for programmers. Whether it's solving coding problems, optimizing algorithms, or getting programming advice, ChatGPT can provide helpful assistance. By having a conversation with ChatGPT, we can quickly get solutions and interact with a smart and flexible virtual partner.

However, keep in mind that while ChatGPT can provide us with useful suggestions, it is only an auxiliary tool. During the programming process, our expertise and judgment are still required to decide the best solution.

Guess you like

Origin blog.csdn.net/weixin_43856625/article/details/132201752