ChatGPT代码生成能力初步测试
ChatGPT 是一款支持类对话交互的人工智能服务,虽名为“聊天机器人”,但其功能已大幅超越单纯闲聊。在代码编写任务中,其表现令人印象深刻,能够生成连贯且具有启发性的代码片段,为开发者提供新思路。
初始测试指令要求使用 JavaScript 编写一个用于密码散列(HASH)的函数。具体需求为:创建一个接收字符串类型密码输入,并返回对应散列值的 JS 函数。
function hashPassword(password) {
return crypto.createHash("sha256").update(password).digest("hex");
}
随后逐步提升编码复杂度,模拟更贴近实际项目开发的需求,输入如下指令:
构建一个 Express 路由处理程序,该程序需从请求正文中提取用户名和密码,对密码进行哈希处理,并在 MySQL 数据库中检索现有用户,最后比对哈希后的密码是否匹配。
const express = require("express");
const bcrypt = require("bcrypt");
const mysql = require("mysql");
// 创建 MySQL 数据库连接
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb",
});
// 连接数据库
db.connect((err) => {
if (err) {
throw err;
}
console.log("MySQL connected...");
});
// 创建 express app
const app = express();
// 路由 handler
app.post("/login", (req, res) => {
// get username and password from request body
const { username, password } = req.body;
// HASH 密码
const hashedPassword = bcrypt.hashSync(password, 10);
// 在数据库中查找用户名称
let sql = `SELECT * FROM users WHERE username = '${username}'`;
let query = db.query(sql, (err, result) => {
if (err) throw err;
// 检查密码是否匹配
if (bcrypt.compareSync(result[0].password, hashedPassword)) {
res.send("Login successful!");
} else {
res.send("Incorrect username or password");
}
});
});
上述生成的 JavaScript 代码质量较高,基本无错误,可直接复制至项目中投入调试。接下来尝试其他编程语言:
使用 C 语言编写一个链表结构代码,要求实现根据“标识”属性在链表中添加节点、删除节点以及查找节点的功能。
#include
#include
// Structure for a node in the linked list
typedef struct Node {
int identity; // Property of the node
struct Node *next; // Pointer to the next node
} Node;
// Function to add a node to the linked list
void addNode(Node **head, int identity) {
// Create a new node
Node *newNode = (Node*)malloc(sizeof(Node));
// Set the identity of the new node
newNode->identity = identity;
// Set the next pointer of the new node to point to the head
newNode->next = *head;
// Set the head to point to the new node
*head = newNode;
}
// Function to delete a node from the linked list
void deleteNode(Node **head, int identity) {
// Create a temporary node to traverse the linked list
Node *temp = *head;
// Check if the head is the node to be deleted
if (temp != NULL && temp->identity == identity) {
// Set the head to point to the next node
*head = temp->next;
// Free the memory allocated for the node
free(temp);
return;
}
// Traverse the linked list to find the node to be deleted
while (temp != NULL && temp->identity != identity) {
// Set the previous node to the current node
Node *prev = temp;
// Set the current node to the next node
temp = temp->next;
}
// Check if the node was found
if (temp == NULL) return;
// Set the previous node to point to the next node
prev->next = temp->next;
// Free the memory allocated for the node
free(temp);
}
// Function to find a node in the linked list
Node* findNode(Node *head, int identity) {
// Create a temporary node to traverse the linked list
Node *temp = head;
// Traverse the linked list to find the node
while (temp != NULL) {
// Check if the current node has the given identity
if (temp->identity == identity) {
return temp;
}
// Set the current node to the next node
temp = temp->next;
}
// Return NULL if the node was not found
return NULL;
}
int main() {
// Create a head node for the linked list
Node *head = NULL;
// Add some nodes to the linked list
addNode(&head, 1);
addNode(&head, 2);
addNode(&head, 3);
// Find a node in the linked list
Node *node = findNode(head, 2);
// Print the identity of the node
printf("Node with identity 2: %d\n", node->identity);
// Delete a node from the linked list
deleteNode(&head, 2);
// Find the deleted node in the linked list
node = findNode(head, 2);
// Check if the node was deleted
if (node == NULL) {
printf("Node with identity 2 was deleted\n");
}
return 0;
}
看着 AI 的表现,不禁让人担忧程序员的未来。鉴于 AI 机器学习能力远超人类,假设一位程序员职业生涯长达 20 年且勤奋好学,AI 却可通过学习收集全球所有程序员的经验。人脑的计算能力无法与在超大规模数据集上训练有素的 AI 模型相比。未来,程序员是否会因 AI 的强大而面临被职业淘汰的风险?
总结
至此,关于利用 OpenAI ChatGPT 进行编码尝试体验的介绍已结束。如需了解更多相关 OpenAI ChatGPT 编码内容,建议搜索本站历史文章或浏览下方推荐链接。感谢大家的支持!