Python , C And ChatGPT

I coded for the classic question 'two sum' with Python and C. You should not be surprised by Python's ease of use in getting the job done. Just by using a dictionary as a hashtable to check the difference between the given number and the array value. If the difference is not in the dictionary, push it in. If the difference equals the array value, then you have found the number you need. You could do it with Python in only 10 minutes.

But for C, it is a long story. At first, I need to structure a data format for the hashtable. At this point, as there is only one solution, so I don’t need to worry about collision. Thus, I did not use the classic linked list for the collision impact. I used the difference as the hash key; it will improve the code's runtime efficiency. However, there is a problem: the difference could be a negative integer. I can’t use it as an index. If I simply call the ABS function, collisions will occur. (1 and -1 will return the same value of 1). So the solution is to use a large enough number as an offset to turn the negative number to positive. However, this creates another problem: if the input number is too big, the offset result will be greater than an unsigned int can hold and cause a wraparound. I had to implement control flow for the negative and positive integers, which is not ideal. So I asked ChatGPT for a hint.

The first answer didn't surprise me; it just showed me the offset method. I gave it low feedback. The second time, it returned me this: '& 0x7FFFFFFF'. That is ingenious; it turns a negative int into a positive int without any collision.

n the end, ChatGPT isn't quite ready to replace programmers entirely, but it's a big help to them. It used to take around 10,000 hours to become a language master, but now, with ChatGPT, you might get there in just 3,000 hours. And when we look at how fast it can churn out code, using LeetCode as an example, C code for the "twoSum" problem takes only 2 milliseconds, while Python needs 60 milliseconds. This shows why C and C++ are likely to stick around for a long time – they're tough and efficient

Back to Home

Description of Image 1