天啦噜太可怕了,之前我还嘲笑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. docker搭建phpswoole实现http服务

    一.创建Dockerfile FROM phpswoole/swoole # COPY ./www/ /var/www/ 二.同级目录下创建docker-compose.yml services: p ...

  2. MarkDown学习笔记1

    # MarkDown学习(一级标题)## 标题(二级)### 三级标题#### 四级标题## 字体*Hello,World!斜体***Hello,World!加粗*****Hello,World!加粗 ...

  3. 第三章:用python实现常用的用户分层模型(RFM模型)

    文章目录 项目背景 读取数据 数据分析 分析 Recent 分析 Frequency 分析 Mount RFM模型 分位数分层 自定义分层 定义客户标签 数据可视化 结论 源码地址 本文可以学习到以下 ...

  4. redis - 常用方法封装总结

    package com.citydo.utils; import org.springframework.data.redis.connection.DataType; import org.spri ...

  5. axios基本配置

    点击查看代码 <!-- axios基础用法 --> <script> /** * axios:一款基于promise设计模式封装的ajax库(JQ中的ajax就是最普通的aja ...

  6. lua按某些键排序的方法

    function sort(list, ...) local opts = {...}; local len = #opts; return table.sort(list, function(a, ...

  7. CMD输出文本文件内容的type指令(替代Linux的cat指令)

    :: 本脚本的所有指令本身不输出到屏幕 @echo off :: 打印文本文件内容并匹配关键字,结果不输出到屏幕 type result.txt | find "error" &g ...

  8. jmeter将接口返回写入log

    1.添加BeanShell 后置处理程序 2. //拿到响应值 String json = prev.getResponseDataAsString(); log.info("Value为: ...

  9. Jmeter九、jmeter中的函数和beanshell

    beanshell  松散类型的脚本语言 可在里面自定义函数

  10. switch组件的使用

    正常情况下,path和component是一一对应的关系 switch可以提高路由匹配效率(单一匹配)