利用OpenAI?ChatGPT進行編碼嘗試體驗
ChatGPT 是一種以類似聊天的方式進行交互的人工智能網(wǎng)絡(luò),從名字上看起來像是另一個“聊天機器人”,但實際上已經(jīng)遠超聊天的范疇了,當(dāng)要求它編寫一些代碼時,結(jié)果相等震撼,它可以寫出一些意想不到的連貫代碼,而這些代碼對于開發(fā)者很有啟發(fā)。
開始要求它使用 JavaScript 編寫一個散列(HASH)密碼的函數(shù)。具體一點就是編寫一個 JavaScript 函數(shù),它接受密碼的字符串輸入并輸出散列(HASH)密碼。
function hashPassword(password) { return crypto.createHash("sha256").update(password).digest("hex"); }
然后逐步提高編碼的難度,實現(xiàn)一些更加符合項目開發(fā)的需求,于是輸入:
編寫一個在請求正文中包含用戶名和密碼的 Express 路由處理程序,對密碼進行哈希處理,在 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ù)制到項目中進行調(diào)試。接下來使用別的語言來試一下:
用C語言編寫一個鏈表結(jié)構(gòu)的代碼,具有根據(jù)“標識”屬性在鏈表中添加、刪除和查找節(jié)點的功能。
#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)心未來的程序員,對于AI的機器學(xué)習(xí)能力,肯定是強于人類的。一個程序員寶貴的職業(yè)生涯就算 20 年,就假設(shè)這個程序員在職業(yè)生涯中相當(dāng)勤奮好學(xué),可AI應(yīng)該可以通過學(xué)習(xí)收集全世界所有程序員的經(jīng)驗!人腦無法與在極其龐大的數(shù)據(jù)集上訓(xùn)練有素的 AI 模型的計算能力相提并論。未來是不是有可能程序員通過AI把自己這個職業(yè)淘汰掉!
總結(jié)
到此這篇關(guān)于利用OpenAI ChatGPT進行編碼嘗試體驗的文章就介紹到這了,更多相關(guān)OpenAI ChatGPT編碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript中Function與Object的關(guān)系
本文主要介紹了JavaScript中Function與Object的關(guān)系,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05詳解微信小程序與內(nèi)嵌網(wǎng)頁交互實現(xiàn)支付功能
這篇文章主要介紹了詳解微信小程序與內(nèi)嵌網(wǎng)頁交互實現(xiàn)支付功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10一道面試題引發(fā)的對javascript類型轉(zhuǎn)換的思考
本文主要介紹了javascript類型轉(zhuǎn)換的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03