เมื่อท้า AI ตัวดังอย่าง ChatGPT และ Gemini มาดวลแข่งเขียน Python ด้วยโจทย์จาก LeetCode มาดูกันว่าจะเป็นยังไง

โดยโจทย์ที่เราเลือกมาจะมี 3 ระดับ ทั้ง Easy Medium และ Hard
1️⃣ Easy: Two Sum
2️⃣ Medium: Zigzag Conversion
3️⃣ Hard: Regular Expression Matching
หมายเหตุ
*ใช้ ChatGPT และ Gemini แบบ Free Version
*ใส่ prompt ที่เหมือนกันและใช้คำตอบแรกของทั้ง 2 ตัว

เปิดมาด้วยข้อแรก

Easy

1️⃣ Two Sum 
ระดับความยาก 🔥

Prompt 👉
Write Python code to solve this problem

Given an integer x, return true if x is a
palindrome
, and false otherwise.
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Constraints:
-231 <= x <= 231 – 1

คำตอบจาก ChatGPT

Copy code ได้จาก 👉 https://pastebin.com/WYZJNK8B 

คำตอบจาก Gemini

Copy code ได้จาก 👉https://pastebin.com/W8Tv66uR

ผลการตัดสิน

ผลการตัดสินข้อแรก 🔔

ข้อนี้ถือว่าผ่าน ได้คะแนนไปทั้งคู่ ✅✅
บอกเลยว่าเฉือนกันไปแค่นิดเดียว แล้วแต่ว่าใคร prefer run time หรือ memory ก็เลือกใช้กันได้ตามสะดวก

Medium

2️⃣ Zigzag Conversion
ระดับความยาก 🔥🔥

Prompt 👉
Write Python code to solve this problem
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: “PAHNAPLSIIGYIR”

Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);

Example 1:
Input: s = “PAYPALISHIRING”, numRows = 3
Output: “PAHNAPLSIIGYIR”

Example 2:
Input: s = “PAYPALISHIRING”, numRows = 4
Output: “PINALSIGYAHRPI”
Explanation:
P     I    N
A   L S  I G
Y A   H R
P     I

Example 3:
Input: s = “A”, numRows = 1
Output: “A”
Constraints:
1 <= s.length <= 1000
s consists of English letters (lower-case and upper-case), ‘,’ and ‘.’.
1 <= numRows <= 1000

คำตอบจาก ChatGPT

Copy code ได้จาก 👉 https://pastebin.com/HsTedPpR

คำตอบจาก Gemini

Copy code ได้จาก 👉 https://pastebin.com/euPjM9Gi

ผลการตัดสิน

ChatGPT ✅ ด้วย Runtime 28 ms และ Memory 11.85 MB
Gemini ✅ ผ่านไปด้วย Runtime 31 ms และ Memory 11.58 MB

ได้ไปอีกคนละ 1 แต้ม

Hard

3️⃣ Regular Expression Matching
ระดับความยาก 🔥🔥🔥

Prompt 👉
Write Python code to solve this problem
Given an input string s and a pattern p, implement regular expression matching with support for ‘.’ and ‘*’ where:
‘.’ Matches any single character.​​​​
‘*’ Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).

Example 1:
Input: s = “aa”, p = “a”
Output: false
Explanation: “a” does not match the entire string “aa”.

Example 2:
Input: s = “aa”, p = “a*”
Output: true
Explanation: ‘*’ means zero or more of the preceding element, ‘a’. Therefore, by repeating ‘a’ once, it becomes “aa”.

Example 3:
Input: s = “ab”, p = “.*”
Output: true
Explanation: “.*” means “zero or more (*) of any character (.)”.

Constraints:
1 <= s.length <= 20
1 <= p.length <= 20
s contains only lowercase English letters.
p contains only lowercase English letters, ‘.’, and ‘*’.
It is guaranteed for each appearance of the character ‘*’, there will be a previous valid character to match.

คำตอบจาก ChatGPT

Copy code ได้จาก 👉 https://pastebin.com/p0NjCfdV

Copy code ได้จาก 👉 https://pastebin.com/A88U40Sz

คำตอบจาก Gemini

ผลการตัดสิน

ChatGPT ✅ ด้วย Runtime 34 ms และ Memory 11.69 MB
Gemini ❌ ผ่านไปได้แค่ 289/356 testcases เท่านั้น

เพราะฉะนั้น ในการแข่งขันครั้งนี้ ChatGPT ชนะไปด้วยคะแนน 3 – 2 คะแนนนน เพื่อน ๆ ลองดูกันว่าถ้าเป็นคุณจะปรับโค้ดจาก Gemini ยังไง ให้ logic ถูกต้อง 

และสำหรับใครที่สนใจเรื่องการใช้ prompt ยังไงให้มีประสิทธิภาพ มาดูคอร์ส Getting Started with AI and ChatGPT เพิ่ม Productivity ลดเวลาการทำงานจำเจ ด้วย Insight ทุกอย่างที่ต้องรู้เกี่ยวกับ Generative AI 👉 ดูรายละเอียดเพิ่มเติม คลิก

More in:Data

Comments are closed.