Will Artificial Intelligence (AI) replace programmers? My take!

 

Introduction

So, as we all know, Artificial Intelligence or better known as AI is a hot topic today and everyone is talking about it. With development in NLP (Natural Language Processing) AI like Chat GPT by OpenAI or BARD by most popular search engine: Google, it is no doubt that the field of AI is advancing like never before. So, here is my take on what could be future of these AI bots and will they ever replace programmer?

Will AI replace us programmers?

This might be an over-simplification of things, but the answer to this question would be both yes and no. So lets settle in the middle and say maybe. So before you start thinking what I mean by that, hear me out.

Let's take ChatGPT for example here. With introduction of GPT-4 as of writing this article, the bot is already able to generate codes that are so sophisticated and complicated. Not saying, they write perfect and well optimized code, but they do write the code that somewhat works. Keeping optimizing and code safety aside, the codes generated by the AI can assist the human programmer in writing programs that could take them weeks to write, that too in just matter of minutes.

So, can they replace programmers? In just few years, I hope not but with this level of progress, its is not long before we see a robot sitting in the table, writing codes faster and better than human.

My experience with Chat GPT

For research purpose, I prompted chat gpt to generate some codes for me. And it kinda blew my mind. First I asked it to generate web based weather application using weather api, it was able to do so. Below is the code it generated using OpenWeather API in HTML and jQuery.

HTML

<!DOCTYPE html>
<html>
  <head>
    <title>Weather App</title>
  </head>
  <body>
    <h1>Weather App</h1>
    <div id="weather"></div>
    
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="app.js"></script>
  </body>
</html>


jQuery

$(document).ready(function() {
  // Replace YOUR_API_KEY with your actual API key from OpenWeatherMap
  var apiKey = "YOUR_API_KEY";
  
  // Replace YOUR_CITY_NAME with your actual city name
  var city = "YOUR_CITY_NAME";
  
  // Build the API URL
  var apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey;
  
  // Make the API request
  $.getJSON(apiUrl, function(data) {
    // Parse the response data
    var temperature = Math.round(data.main.temp - 273.15);
    var weatherDescription = data.weather[0].description;
    
    // Build the output HTML
    var html = "<p>It's currently " + temperature + "°C and " + weatherDescription + " in " + city + ".</p>";
    
    // Output the HTML
    $("#weather").html(html);
  });
});


The above code seems accurate so I wanted to take it one step further and gave it following prompt:

Write a python program to perform heap sort. Also make sure to include explanation of the program at the end!

And ya, I was satisfied by the code it generated and the explanation it gave.

def heapSort(arr):
    n = len(arr)
    
    # Build a max heap
    for i in range(n//2-1, -1, -1):
        heapify(arr, n, i)
    
    # Extract elements one by one
    for i in range(n-1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]
        heapify(arr, i, 0)
        
def heapify(arr, n, i):
    largest = i
    l = 2*i + 1
    r = 2*i + 2
    
    # Find the largest among the root, left child, and right child
    if l < n and arr[l] > arr[largest]:
        largest = l
    if r < n and arr[r] > arr[largest]:
        largest = r
        
    # If the largest is not the root, swap the root with the largest
    if largest != i:
        arr[i], arr[largest] = arr[largest], arr[i]
        heapify(arr, n, largest)

And here's the explanation of the code by chat gpt:

The heapSort function takes a list of integers (arr) and performs heap sort on it. Heap sort is a comparison-based sorting algorithm that works by first building a max heap, which is a complete binary tree where each node is greater than or equal to its children. Then, it repeatedly extracts the maximum element from the heap and places it at the end of the sorted list.

To build a max heap, we start from the middle of the list (the parent of the last element) and heapify each node from bottom-up until the root is reached. To heapify a node, we compare it to its children and swap it with the largest child if necessary, recursively applying this process to the swapped child until the heap property is satisfied.

Once the max heap is built, we extract the maximum element (which is the root) and swap it with the last element in the unsorted portion of the list. Then, we heapify the root to restore the heap property, and repeat this process until all elements have been extracted.

Ok, so AI is now able to generate basic codes. But what about interview codes? Can it do that as well? To know answer of this question, I decided to visit our favorite interview questions site Leetcode and hit it up with one of the question that is listed as hard by leetcode.

Here's the question if you want to solve it by yourself: Median of Two Sorted Arrays

Here's the code generated by Chat GPT and I specifically asked it to generate the code in C++. Well, it was amazing to see a bot generate the code that was passed all the test cases.

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size();
        int n = nums2.size();
        int i = 0, j = 0;
        vector<int> merged;
        while (i < m && j < n) {
            if (nums1[i] < nums2[j]) {
                merged.push_back(nums1[i]);
                i++;
            } else {
                merged.push_back(nums2[j]);
                j++;
            }
        }
        while (i < m) {
            merged.push_back(nums1[i]);
            i++;
        }
        while (j < n) {
            merged.push_back(nums2[j]);
            j++;
        }
        int len = merged.size();
        if (len % 2 == 0) {
            return (merged[len / 2] + merged[(len / 2) - 1]) / 2.0;
        } else {
            return merged[len / 2];
        }
    }
};

It even gave us following explanation on how the code works to amaze us more:

In this code, we define a Solution class that contains a function findMedianSortedArrays that takes in two vectors nums1 and nums2 as input. We initialize two pointers i and j to zero to point to the beginning of the two arrays. We also create a new vector merged to store the merged sorted array.

We then use a while loop to compare the values at the two pointers and add the smaller value to the merged array. We increment the corresponding pointer and repeat the process until we reach the end of one of the arrays. Finally, we append the remaining elements from the non-empty array to the end of the merged array.

We then calculate the length of the merged array and check if it has an even or odd number of elements. If it has an even number of elements, we return the average of the two middle elements. If it has an odd number of elements, we simply return the middle element.

The time complexity of this algorithm is O(m+n), where m and n are the lengths of the two input arrays. This is because we need to iterate over all the elements in both arrays to merge them into a single sorted array. The space complexity is also O(m+n), as we need to create a new merged array to store the elements.

Conclusion

So, there is no doubt that AI has revolutionized the way human programmer write code. But in my personal opinion, they are not yet in the stage where they could fully replace humans, but that day is not that far too. Humans may not be fully replaced by computers the same way mathmatician were not fully replaced by calculators. But it will replace people who are not able to adapt to emerging change in technology. It will surely replace programmer who are not willing to take on challenge and keep improving themselves.

I still remember when Github Copilot was introduced and the first time I used it, I was amazed. Now with current level of chat bot is making Copilot look bad. 

Post a Comment

0 Comments