天啦噜太可怕了,之前我还嘲笑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. 【shell脚本实战】脚本#35 分析每个用户使用的磁盘空间大小

    #!/bin/bash #设置 一个变量代表临界值,看看是否能超过这个值,单位是MB MAXDISKUSAGE=20000 #cut -d: -f1,3 /etc/passwd | awk -F: ' ...

  2. css如何实现(animation)跑马灯效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Typora怎么让左边的标题折叠

    点击文件选择偏好设置->在选择外观->选中侧边栏的大纲视图允许折叠和展开 效果

  4. ZSTUOJ刷题12:Problem B.--深入浅出学算法007-统计求和

    Problem B: 深入浅出学算法007-统计求和 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 6782  Solved: 3693 Descript ...

  5. chap3第三小组总结

      本周我们第三小组在张庆老师的带领下,走向编程的新一扇大门--分支结构.   我们第三小组是线下聚在一起学习,这样可以使我们的学习效率大大提高,我们在线下学习可以让我们的小组长更方便的指导我们的学习 ...

  6. mysql零基础-1

    数据库概述 为什么要使用数据库 持久化 DB:数据库 DBMS:数据库管理系统 SQL:结构化查询语言 数据库与数据库管理系统关系 数据库管理系统(DBMS)可以管理多个数据库,一般开发人员会针对每一 ...

  7. element-ui跨行

    1 <template> 2 <el-table :data="scheduleList" :span-method="objectSpanMethod ...

  8. Vue.sync修饰符与this.$emit('update:xxx', newXXX)

    Vue通过prop进行双向数据绑定.子组件数据变化,一般只能通过 this.$emit(func, val) 回调父组件函数来传值给父组件. Vue2.3版本引入sync,作为一个事件绑定语法糖,当子 ...

  9. UE5农场项目小功能(1)-砍树

    砍树功能效果的实现 ​ 在UE中想做一个砍树的功能,B站上没找到什么教程,最后在油管上找到个视频并跟着实现了,一共设计到三个蓝图和一个什么也没写的蓝图接口,下面介绍下步骤. 1.角色蓝图的部分 人物这 ...

  10. 01背包&完全背包二维写法的对比,进而理解一维优化后的正逆序

    01背包题解 完全背包题解 二维写法时两种背包问题核心代码的区别: 可以看出,01背包用的是上一层的数据,完全背包用的是当前层的数据 所以优化为一维时, 01背包需逆序 for (int i = 1; ...