利用OpenAI?ChatGPT進(jìn)行編碼嘗試體驗(yàn)
ChatGPT 是一種以類似聊天的方式進(jìn)行交互的人工智能網(wǎng)絡(luò),從名字上看起來像是另一個(gè)“聊天機(jī)器人”,但實(shí)際上已經(jīng)遠(yuǎn)超聊天的范疇了,當(dāng)要求它編寫一些代碼時(shí),結(jié)果相等震撼,它可以寫出一些意想不到的連貫代碼,而這些代碼對(duì)于開發(fā)者很有啟發(fā)。
開始要求它使用 JavaScript 編寫一個(gè)散列(HASH)密碼的函數(shù)。具體一點(diǎn)就是編寫一個(gè) JavaScript 函數(shù),它接受密碼的字符串輸入并輸出散列(HASH)密碼。
function hashPassword(password) {
return crypto.createHash("sha256").update(password).digest("hex");
}然后逐步提高編碼的難度,實(shí)現(xiàn)一些更加符合項(xiàng)目開發(fā)的需求,于是輸入:
編寫一個(gè)在請(qǐng)求正文中包含用戶名和密碼的 Express 路由處理程序,對(duì)密碼進(jìn)行哈希處理,在 MySQL 數(shù)據(jù)庫中查找現(xiàn)有用戶并檢查哈希后的密碼是否匹配。
const express = require("express");
const bcrypt = require("bcrypt");
const mysql = require("mysql");
// 創(chuàng)建 MySQL 數(shù)據(jù)庫連接
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb",
});
// 連接數(shù)據(jù)庫
db.connect((err) => {
if (err) {
throw err;
}
console.log("MySQL connected...");
});
// 創(chuàng)建 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);
// 在數(shù)據(jù)庫中查找用戶名稱
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 代碼基本是沒什么問題的,可以直接復(fù)制到項(xiàng)目中進(jìn)行調(diào)試。接下來使用別的語言來試一下:
用C語言編寫一個(gè)鏈表結(jié)構(gòu)的代碼,具有根據(jù)“標(biāo)識(shí)”屬性在鏈表中添加、刪除和查找節(jié)點(diǎn)的功能。
#include <stdio.h>
#include <stdlib.h>
// 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;
}于是似乎開始擔(dān)心未來的程序員,對(duì)于AI的機(jī)器學(xué)習(xí)能力,肯定是強(qiáng)于人類的。一個(gè)程序員寶貴的職業(yè)生涯就算 20 年,就假設(shè)這個(gè)程序員在職業(yè)生涯中相當(dāng)勤奮好學(xué),可AI應(yīng)該可以通過學(xué)習(xí)收集全世界所有程序員的經(jīng)驗(yàn)!人腦無法與在極其龐大的數(shù)據(jù)集上訓(xùn)練有素的 AI 模型的計(jì)算能力相提并論。未來是不是有可能程序員通過AI把自己這個(gè)職業(yè)淘汰掉!
總結(jié)
到此這篇關(guān)于利用OpenAI ChatGPT進(jìn)行編碼嘗試體驗(yàn)的文章就介紹到這了,更多相關(guān)OpenAI ChatGPT編碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解通用webpack多頁面自動(dòng)導(dǎo)入方案
本文主要介紹了通用webpack多頁面自動(dòng)導(dǎo)入方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
微信小程序防止多次點(diǎn)擊跳轉(zhuǎn)(函數(shù)節(jié)流)
這篇文章主要介紹了微信小程序防止多次點(diǎn)擊跳轉(zhuǎn)問題(函數(shù)節(jié)流),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
uniapp項(xiàng)目引入?js文件以及全局使用方法
這篇文章主要給大家介紹了關(guān)于uniapp項(xiàng)目引入?js文件以及全局使用方法的相關(guān)資料,在Uniapp中引入JS文件是一項(xiàng)常見的操作,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
JavaScript實(shí)現(xiàn)移動(dòng)端滑動(dòng)選擇日期功能
這篇文章主要介紹了JavaScript實(shí)現(xiàn)滑動(dòng)選擇日期功能,基于sui-mobile的移動(dòng)端實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-06-06
原生js實(shí)現(xiàn)addClass,removeClass,hasClass方法
這篇文章主要介紹了原生js實(shí)現(xiàn)addClass,removeClass,hasClass方法和使用原生JS實(shí)現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能,需要的朋友可以參考下2016-04-04
JS實(shí)現(xiàn)獲取圖片大小和預(yù)覽的方法完整實(shí)例【兼容IE和其它瀏覽器】
這篇文章主要介紹了JS實(shí)現(xiàn)獲取圖片大小和預(yù)覽的方法,結(jié)合完整實(shí)例形式分析了javascript針對(duì)不同瀏覽器處理圖片上傳與預(yù)覽等操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04

