使用chatgt(GPT-4)将过程式(的java代码)改成函数式(的elixir代码)

科技资讯 投稿 6100 0 评论

使用chatgt(GPT-4)将过程式(的java代码)改成函数式(的elixir代码)

chatgt(GPT-4在接收2次prompt后,把过程式(的java代码改成了函数式(的elixir代码,给出的Elixir代码可以不经修改直接AC。

但是chatgpt事实上并不具备创新的能力,如果语料里【过程式代码转函数式代码的例子】足够多,它能完成这一任务我一点不觉得惊讶

特别强大的检索整合能力,但是对于原创的新颖的算法题目,它也只能对着题目描述废话一堆给出错误的答案。

目录
    Problem URL
  • Code
  • Prompt

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 

编程笔记 » 使用chatgt(GPT-4)将过程式(的java代码)改成函数式(的elixir代码)

赞同 (29) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽