Monday, January 2, 2023

ChatGPT - Are Engineers Still Required

 



Recently there is a lot of buzz around ChatGPT, a trained language model that has answer to anything! This prevails many domains, including the programming domain, and causes some software engineers to ask whether this is the beginning of the end of this profession for human.

I've played with this bot, and checked its abilities. It is very impressive, and is probably the best of breed so far. I t can provide useful information for many questions, and the answers appear to be comprehensive.

Let's examine some good examples. I've asked the bot:

"create a go program that reads numbers from a file, and prints their average"

 The result code is:


import (
"bufio"
"fmt"
"os"
"strconv"
)

func main() {
// Open the file for reading
file, err := os.Open("numbers.txt")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()

// Initialize a scanner to read the file line by line
scanner := bufio.NewScanner(file)

// Initialize a variable to hold the sum of the numbers
var sum float64

// Initialize a variable to keep track of the number of numbers
var count int

// Read the file line by line
for scanner.Scan() {
// Convert the line to a float
number, err := strconv.ParseFloat(scanner.Text(), 64)
if err != nil {
fmt.Println(err)
continue
}

// Add the number to the sum
sum += number

// Increment the count
count++
}

// Check for any errors that occurred during the scan
if err := scanner.Err(); err != nil {
fmt.Println(err)
return
}

// Calculate the average
avg := sum / float64(count)

// Print the average
fmt.Printf("Average: %.2f\n", avg)
}



Too good, to be true!

A full program, including comments, and it is working (yes, I've test it). 

But wait...

What about an empty file? The Average will be printed as NaN, and no errors. Still this is a good answer that will pass you through an interview.


Let's make it harder: 

"create a go program that draw a 4X4 board allowing a player to move in the board up, down, left, and right"


package main

import (
"bufio"
"fmt"
"os"
)

const boardSize = 4

// Coord represents a coordinate on the board
type Coord struct {
x, y int
}

func main() {
board := make([][]bool, boardSize)
for i := range board {
board[i] = make([]bool, boardSize)
}

pos := Coord{0, 0}
board[pos.x][pos.y] = true

// Set up input reader
reader := bufio.NewReader(os.Stdin)

for {
fmt.Println("Move around the board using the arrow keys. Press 'q' to quit.")
drawBoard(board)

// Read input
input, _, err := reader.ReadRune()
if err != nil {
fmt.Println(err)
continue
}

// Clear current position on board
board[pos.x][pos.y] = false

// Update position based on input
switch input {
case 'q':
fmt.Println("Quitting...")
return
case 'w':
pos.x--
case 's':
pos.x++
case 'a':
pos.y--
case 'd':
pos.y++
default:
fmt.Println("Invalid input. Try again.")
continue
}

// Check for out-of-bounds error
if pos.x < 0 || pos.x >= boardSize || pos.y < 0 || pos.y >= boardSize {
fmt.Println("Invalid move. Try again.")
pos.x, pos.y = 0, 0
continue
}

// Update current position on board
board[pos.x][pos.y] = true
}
}

func drawBoard(board [][]bool) {
fmt.Println("-------------------------")
for i := 0; i < boardSize; i++ {
fmt.Print("|")
for j := 0; j < boardSize; j++ {
if board[i][j] {
fmt.Print(" X ")
} else {
fmt.Print(" ")
}
}
fmt.Println("|")
fmt.Println("-------------------------")
}
}



WOW!! I mean WOW!! I did not expect that to work. It looks amazing!!

But... wait, it is almost working. The input reading reads both the key and the enter, while it actually asks for arrow, and expects wasd keys. Also the printout of the board looks very bad. Still it is a good start that a programmer can start with a fix to work properly. It might save an hour of work.


Hey, this is a chat bot, right? So let's chat about the last question:

"now, add obstacles to the board that the play must not move into"


So the bot changed the board type from bool to string, and added this:


// Check for obstacle
if board[pos.x][pos.y] == "O" {
fmt.Println("You can't move there. There's an obstacle in the way.")
pos.x, pos.y = 0, 0
continue
}



But the draw board function was cut. Looks like it has some limit on the program length (Though I asked the bot and he denied it...)


Final Note

ChatGPT is a nice tool, for simple programming issues, it can be used as an advanced stackoverflow site, but it is limited by the complexity and length of the answers. While it is a step toward a programmer, it is a very small step for a long marathon run. Still, we might see a breakthrough in several years that will enhance it abilities. For now, while it might pass a very simple junior programmer interview, it cannot do a junior programmer work.
 










No comments:

Post a Comment