What is ChatGPT
ChatGPT (Generative Pre-training Transformer) is a chatbot built on top of OpenAI’s GPT-3.5. You can ask a large variety of questions, and it will generate responses in a way similar to how a human might respond.
In addition to answering standard questions that you could search on Google, ChatGPT can perform more complex tasks, such as writing an essay or even a poem on any topic you can think of.
How can ChatGPT help me with coding?
One of the most impressive skills of ChatGPT is its ability to write code. Even though it can’t program a video game or create a webpage for you, you can think about ChatGPT as an improved StackOverflow that can help you in many ways.
Writing a Function
I have asked ChatGPT to write a function in python to find the following prime number:
As you can see, not only has it written the function, but it also has added a brief description of what it is doing. This is an example, but feel free to try its limits with more complex functions!
Optimising Code
Here I’ve written a code to find the common elements in two arrays with a time complexity of O(n^2) and have asked ChatGPT to optimise it:
As expected, ChatbotGPT has removed the extra loop for the second array and used hash tables to reduce the time complexity to O(n). By the way, you can also use ChatGPT to calculate the time complexity of a given function!
Describing Code
One of the most impressive characteristics of ChatGPT is its ability to write responses humanly, so I have asked it to describe the code below:
First, it describes the code step-by-step, which is ok but could be better. Then it adds relevant information, such as the final set won’t contain duplicate elements. It can seem obvious since it is the way sets work, but it is still worth mentioning.
Finally, it has gone one step further and explained the use of the function, finding the intersection of two iterables in this case.
Writing Test Scenarios
We all know the benefits of writing unit tests and their importance, but we also know it is not as exciting as writing code, so I have asked ChatGPT to do it for me:
If we look at the text response, we can see that it says it also covers the scenario where both input lists are empty, but if we look at the code, we can see that’s not true, so always give a look before blindly copying code from ChatGPT! Apart from that, we could say that it has done a decent job with the tests.
Finding Bugs
One of the skills of ChatGPT is the ability to find bugs, so I have added a minor bug to the previous function and have asked ChatGPT to find it. You can also try to find the bug before checking its answer!
Yes, the bug was on the last line. I had my “Caps Lock” activated and was trying to add the entire set instead of the element. If we look at the ChatGPT response, we can see that it also has spotted the bug and, as always, has added some text explanation about what’s the bug and how to fix it.
Conclusion
ChatGPT is a really strong tool that can help you in your day-to-day tasks in coding and many different ways. Even though it is powerful and accurate in most cases, we must remember that it is built on top of an AI model, and it can be wrong sometimes, so always double-check its answers!