两个 AI 编程工具 Clacky AI 和 Cursor AI,用了同样的提示词,没有修改任何设置,随机选四道题目,要求它们参考我写的其它语言的题解,写出 Ruby 版本的题解。大家欣赏下,看看喜欢哪个。
https://leetcoder.net/zh/leetcode/28-find-the-index-of-the-first-occurrence-in-a-string
# @param {String} haystack
# @param {String} needle
# @return {Integer}
def str_str(haystack, needle)
(0...haystack.length).each do |i|
j = 0
while i + j < haystack.length && haystack[i + j] == needle[j]
j += 1
return i if j == needle.length
end
end
-1
end
def str_str(haystack, needle)
for i in 0...haystack.length
j = 0
while i + j < haystack.length && haystack[i + j] == needle[j]
j += 1
if j == needle.length
return i
end
end
end
-1
end
本题结论:: Clacky AI 胜出。因为用.each
了,符合 Ruby 风格。
https://leetcoder.net/zh/leetcode/160-intersection-of-two-linked-lists
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end
# @param {ListNode} head_a
# @param {ListNode} head_b
# @return {ListNode}
def get_intersection_node(head_a, head_b)
node_count_a = 0
node_count_b = 0
node = head_a
while node
node_count_a += 1
node = node.next
end
node = head_b
while node
node_count_b += 1
node = node.next
end
bigger = head_a
smaller = head_b
if node_count_b > node_count_a
bigger = head_b
smaller = head_a
end
(node_count_b - node_count_a).abs.times do
bigger = bigger.next
end
while bigger && smaller
return bigger if bigger == smaller
bigger = bigger.next
smaller = smaller.next
end
nil
end
一,注释部分不同。Cursor AI 以下内容没有。
# @param {ListNode} head_a
# @param {ListNode} head_b
# @return {ListNode}
而没有是符合预期的,Clacky AI 总是创造性地加上参数和返回值注释。
二,Indents 不同,Clacky AI 用了 4 个空格做 Indents,这个不符合 Ruby 风格。
本题结论:平手,Clacky AI 略下风。
https://leetcoder.net/zh/leetcode/383-ransom-note
def can_construct(ransom_note, magazine)
char_to_count = Hash.new(0)
magazine.each_char { |c| char_to_count[c] += 1 }
ransom_note.each_char do |c|
char_to_count[c] -= 1
return false if char_to_count[c] < 0
end
true
end
(这里,我开始提醒 Clacky AI 要用 indents 2。希望下次会改正,果然,下一题改正了)
本题结论:平手。
https://leetcoder.net/zh/leetcode/59-spiral-matrix-ii
class Solution
def generate_matrix(n)
@matrix = Array.new(n) { Array.new(n) }
@increments = [[0, 1], [1, 0], [0, -1], [-1, 0]]
@increment_index = 0
i = 0
j = 0
num = 1
while num <= n * n
@matrix[i][j] = num
num += 1
increment = get_increment(i, j)
i += increment[0]
j += increment[1]
end
@matrix
end
private
def get_increment(i, j)
increment = @increments[@increment_index]
next_i = i + increment[0]
next_j = j + increment[1]
if next_i < 0 || next_i >= @matrix.size ||
next_j < 0 || next_j >= @matrix.size ||
@matrix[next_i][next_j]
@increment_index += 1
@increment_index %= @increments.size
end
@increments[@increment_index]
end
end
class Solution
def initialize
@matrix = nil
@increments = [[0, 1], [1, 0], [0, -1], [-1, 0]]
@increment_index = 0
end
def generate_matrix(n)
@matrix = Array.new(n) { Array.new(n, 0) }
i = 0
j = 0
num = 1
while num <= n * n
@matrix[i][j] = num
num += 1
increment = get_increment(i, j)
i += increment[0]
j += increment[1]
end
@matrix
end
private
def get_increment(i, j)
increment = @increments[@increment_index]
i += increment[0]
j += increment[1]
if i < 0 || i >= @matrix.length ||
j < 0 || j >= @matrix.length ||
@matrix[i][j] > 0
@increment_index += 1
@increment_index %= @increments.length
end
@increments[@increment_index]
end
end
区别:Cursor AI 把实例变量定义在 initialize
方法中,这导致多写了两行代码,另外还多写了一次 @matrix = nil
,这个不符合我的刷题喜好。
Clacky AI 定义了 local variables next_i
和next_j
,这是不符合我的预期的,因为我给出其它语言的例子了。但这个创造感觉还是不错的,可读性提高了。
本题结论:Clacky AI 胜出。
另外,Clacky AI 每一个题解都少输出了一行三个上斜点,后来我提醒它,它最后改正了。
本次测试,我发现 Clacky AI 创造性可能更强一点,比如我之前预期是用变量名 charactor
的,它认为不好,改成了c
了。
Cursor AI 更按部就班一点,因为我明确地告诉他们,依照我手写的其它语言的例子去做,但 Clacky AI 还是会进行再创造,不过几次再创造我愿意接纳了。
最终,我将选择 Clacky AI 写的题解,当然,依然是我的题解,因为逻辑上我要求它们参考我手写的其它语言的例子。
欢迎评论。