天啦噜太可怕了,之前我还嘲笑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.

使用chatgt(GPT-4)将过程式(的java代码)改成函数式(的elixir代码)的更多相关文章

  1. 快速将磁盘的MBR分区方式改成GPT分区方式

    1.按Shift + F10打开命令提示符. 2.diskpart 3.list disk(列出所有磁盘) 4.select disk 0(选择磁盘) 5.clean(格式化所选的磁盘) 7.conv ...

  2. 如何快速将磁盘的MBR分区方式改成GPT分区方式

    注:修改分区格式时此硬盘不能是在使用状态(简单说就是不能出现在盘符中),如果在使用中先在计算机的磁盘管理中删除卷. 由于MBR分区表模式的硬盘最大只支持2T的硬盘空间,而现在我们的硬盘越来越大,有时候 ...

  3. 001_关于选中的磁盘具有MBR分区表。在 EFI 系统上,Windows 只能安装到 GPT 磁盘。问题解决

    问题: 今天我的diy电脑重装系统时,遇到了一个棘手的问题.在选择安装分区的时候,提示有这样的错误. Windows 无法安装到这个磁盘.选中的磁盘具有MBR分区表.在 EFI 系统上,Windows ...

  4. 悟透JavaScript

    要理解JavaScript,你得首先放下对象和类的概念,回到数据和代码的本原.前面说过,编程世界只有数据和代码两种基本元素,而这两种元素又有着纠缠不清的关系.JavaScript就是把数据和代码都简化 ...

  5. 悟透JavaScript(理解JS面向对象的好文章)

    引子 编程世界里只存在两种基本元素,一个是数据,一个是代码.编程世界就是在数据和代码千丝万缕的纠缠中呈现出无限的生机和活力. 数据天生就是文静的,总想保持自己固有的本色:而代码却天生活泼,总想改变这个 ...

  6. C/C++:C++中static,extern和extern "C"关键字

    1. extern 变量 extern 表明该变量在别的地方已经定义过了,在这里要使用那个变量. 当extern不与"C"在一起修饰变量或函数时,如在头文件中: extern in ...

  7. Javascript设计模式详解

    Javascript常用的设计模式详解 阅读目录 一:理解工厂模式 二:理解单体模式 三:理解模块模式 四:理解代理模式 五:理解职责链模式 六:命令模式的理解: 七:模板方法模式 八:理解javas ...

  8. 《深度探索C++对象模型》读书笔记(一)

    前言 今年中下旬就要找工作了,我计划从现在就开始准备一些面试中会问到的基础知识,包括C++.操作系统.计算机网络.算法和数据结构等.C++就先从这本<深度探索C++对象模型>开始.不同于& ...

  9. 详解JS设计模式

    原文链接:www.cnblogs.com 一:理解工厂模式 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式. 简单的工厂模式可以理解为解决 ...

  10. 精练代码:一次Java函数式编程的重构之旅

    摘要:通过一次并发处理数据集的Java代码重构之旅,展示函数式编程如何使得代码更加精练. 难度:中级 基础知识 在开始之前,了解"高阶函数"和"泛型"这两个概念 ...

随机推荐

  1. 如何使用C++代码实现1-100之间的素数

    #include "pch.h" #include <iostream> using namespace std; int main() { cout << ...

  2. git常用命令查询手册

    默认已经连接到远程仓库的情况下 本地文件夹初始化成git仓库.提交本地仓库并添加注释 git init git add 文件1(文件夹1) 文件2(文件夹2)... git commit -m &qu ...

  3. vue + antV G6 实现流程图完整代码 (antv G6 流程图)

    效果如下: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  4. vue后台管理系统——商品管理模块

    电商后台管理系统的功能--商品管理模块 商品管理概述 商品管理模块用于维护电商平台的商品信息,包括商品的类型.参数.图片.详情等信息. 通过商品管理模块可以实现商品的添加.修改.展示和删除等功能. 1 ...

  5. pycharm的安装与使用

    官网下载最新版本,然后用激活码,激活,注意是专业版. 方法找到后更新在这里. 进入软件之后创建新的文件夹,可以自定义,建议自定义在系统盘以外, 1.新建文件略过 2.ctrl + 鼠标中键, 调节字体 ...

  6. uniapp 模拟器的使用

    uniapp 模拟器的使用 下载夜深模拟器 uniapp  - 工具-设置 adb路径  模拟器路径 模拟器端口号 :26944

  7. JetPack Compose 入门还得是官方

    官方写的真不错! 和那些所谓"教程"比真的简单高效不罗嗦! 所以还得是官方! 使用 Jetpack Compose 更快地打造更出色的应用 https://developer.an ...

  8. 6-8次PTA题目集总结

    一,前言 1,第六次作业(针对电信计费) 本次作业主要是对类的操作,题目已经给出类图,类图里面已经给了各种方法以及属性.此次作业涉及了座机的计费方式,市内省内省外怎么计费.难度偏难,主要是各类之间怎么 ...

  9. 执行kubectl api-resources 报错error: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request

    1.故障现象:error: unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the serve ...

  10. Shell脚本结构化-控制流

    脚本结构化命令 上一章给出的那些 shell 脚本里,shell 按照命令在脚本中出现的顺序依次进行处理.对顺序操作来说,这已经足够了,因为在这种操作环境下,你想要的就是所有的命令按照正确的顺序执行. ...