天啦噜太可怕了,之前我还嘲笑chatgpt不会小众语言来着。
chatgt(GPT-4)在接收2次prompt后,把过程式(的java代码)改成了函数式(的elixir代码),给出的Elixir代码可以不经修改直接AC。
chatgpt对文本的reasoning特别擅长(如翻译,总结全文,根据主题写文章)。
但是chatgpt事实上并不具备创新的能力,如果语料里【过程式代码转函数式代码的例子】足够多,它能完成这一任务我一点不觉得惊讶。
我理解是特别强大的检索整合能力,但是对于原创的新颖的算法题目,它也只能对着题目描述废话一堆给出错误的答案。
目录Problem URL
https://leetcode.cn/problems/split-two-strings-to-make-palindrome/description/
Code
defmodule Solution do
def check_palindrome_formation(a, b) do
check_concatenation(a, b) || check_concatenation(b, a)
end
def check_concatenation(a, b) do
n = byte_size(a)
check_concatenation_recursive(a, b, 0, n - 1)
end
defp check_concatenation_recursive(a, b, left, right) when left >= right do
true
end
defp check_concatenation_recursive(a, b, left, right) do
if binary_part(a, left, 1) == binary_part(b, right, 1) do
check_concatenation_recursive(a, b, left + 1, right - 1)
else
check_self_palindrome(a, left, right) || check_self_palindrome(b, left, right)
end
end
def check_self_palindrome(a, left, right) do
check_self_palindrome_recursive(a, left, right)
end
defp check_self_palindrome_recursive(a, left, right) when left >= right do
true
end
defp check_self_palindrome_recursive(a, left, right) do
if binary_part(a, left, 1) == binary_part(a, right, 1) do
check_self_palindrome_recursive(a, left + 1, right - 1)
else
false
end
end
end
Prompt
convert java code to elixir code, ``` format please
class Solution {
public boolean checkPalindromeFormation(String a, String b) {
return checkConcatenation(a, b) || checkConcatenation(b, a);
}
public boolean checkConcatenation(String a, String b) {
int n = a.length();
int left = 0, right = n - 1;
while (left < right && a.charAt(left) == b.charAt(right)) {
left++;
right--;
}
if (left >= right) {
return true;
}
return checkSelfPalindrome(a, left, right) || checkSelfPalindrome(b, left, right);
}
public boolean checkSelfPalindrome(String a, int left, int right) {
while (left < right && a.charAt(left) == a.charAt(right)) {
left++;
right--;
}
return left >= right;
}
}
Here's the equivalent Elixir code using pattern matching and recursion:
defmodule Solution do
def check_palindrome_formation(a, b) do
check_concatenation(a, b) || check_concatenation(b, a)
end
def check_concatenation(a, b) do
n = String.length(a)
check_concatenation_recursive(a, b, 0, n - 1)
end
defp check_concatenation_recursive(a, b, left, right) when left >= right do
true
end
defp check_concatenation_recursive(a, b, left, right) do
if String.at(a, left) == String.at(b, right) do
check_concatenation_recursive(a, b, left + 1, right - 1)
else
check_self_palindrome(a, left, right) || check_self_palindrome(b, left, right)
end
end
def check_self_palindrome(a, left, right) do
check_self_palindrome_recursive(a, left, right)
end
defp check_self_palindrome_recursive(a, left, right) when left >= right do
true
end
defp check_self_palindrome_recursive(a, left, right) do
if String.at(a, left) == String.at(a, right) do
check_self_palindrome_recursive(a, left + 1, right - 1)
else
false
end
end
end
This Elixir code follows the same logic as the Java code but uses Elixir's pattern matching and recursion capabilities.
Note that the `check_concatenation_recursive` and `check_self_palindrome_recursive` functions have been defined with
different patterns to handle the base case and recursive case separately.
yes, the result is true, but how to accelerate it, note that 1 <= a.length, b.length <= 10^5
To improve the performance of the Elixir code, you can leverage the `binary_part/3` function, which is more efficient for working with large binaries (strings in Elixir are binaries) than `String.at/2`. Here's the updated code with the optimized functions:
defmodule Solution do
def check_palindrome_formation(a, b) do
check_concatenation(a, b) || check_concatenation(b, a)
end
def check_concatenation(a, b) do
n = byte_size(a)
check_concatenation_recursive(a, b, 0, n - 1)
end
defp check_concatenation_recursive(a, b, left, right) when left >= right do
true
end
defp check_concatenation_recursive(a, b, left, right) do
if binary_part(a, left, 1) == binary_part(b, right, 1) do
check_concatenation_recursive(a, b, left + 1, right - 1)
else
check_self_palindrome(a, left, right) || check_self_palindrome(b, left, right)
end
end
def check_self_palindrome(a, left, right) do
check_self_palindrome_recursive(a, left, right)
end
defp check_self_palindrome_recursive(a, left, right) when left >= right do
true
end
defp check_self_palindrome_recursive(a, left, right) do
if binary_part(a, left, 1) == binary_part(a, right, 1) do
check_self_palindrome_recursive(a, left + 1, right - 1)
else
false
end
end
end
By using `binary_part/3` instead of `String.at/2`, the performance should be improved, especially when dealing with large strings. This is because `binary_part/3` provides direct access to the specified part of the binary without creating a new one, while `String.at/2` creates a new one for each character.