利用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ù)庫(kù)中查找現(xiàn)有用戶并檢查哈希后的密碼是否匹配。
const express = require("express"); const bcrypt = require("bcrypt"); const mysql = require("mysql"); // 創(chuàng)建 MySQL 數(shù)據(jù)庫(kù)連接 const db = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "mydb", }); // 連接數(shù)據(jù)庫(kù) 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ù)庫(kù)中查找用戶名稱 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)文章
JavaScript中Function與Object的關(guān)系
本文主要介紹了JavaScript中Function與Object的關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05一百行JS代碼實(shí)現(xiàn)一個(gè)校驗(yàn)工具
這篇文章主要介紹了一百行JS代碼實(shí)現(xiàn)一個(gè)校驗(yàn)工具,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04firefo xml 讀寫實(shí)現(xiàn)js代碼
firefo xml 讀寫實(shí)現(xiàn) 不過要是你的xml要編碼成功還得在str前面加上xml頭,千萬別忘了啊。2009-06-06詳解微信小程序與內(nèi)嵌網(wǎng)頁交互實(shí)現(xiàn)支付功能
這篇文章主要介紹了詳解微信小程序與內(nèi)嵌網(wǎng)頁交互實(shí)現(xiàn)支付功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10javascript實(shí)現(xiàn)文字無縫滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)文字無縫滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08一道面試題引發(fā)的對(duì)javascript類型轉(zhuǎn)換的思考
本文主要介紹了javascript類型轉(zhuǎn)換的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03