gpt4 book ai didi

reshape - 如何检查APL中的字符串是否被 reshape ?

转载 作者:行者123 更新时间:2023-12-01 23:08:20 28 4
gpt4 key购买 nike

如何检查字符串是否被 reshape ?示例:“aab”返回 0,因为“a”无法 reshape 为该字符串或任何其他更短的字符串。

另一个例子是“aabbaab”返回 1,因为“aabb”可以被 reshape 为该字符串。

很多例子都可以找到这个:

返回 1

101
abba
abcab
abacedabarab
abcdefedabc
!@#~€!

返回0

aA
~
[][][][]][-
abcac
aecec

是否有 lambda 函数可以完成此任务?

注意:如果您不熟悉 APL,请阅读 this

最佳答案

该任务本质上归结为检查我们是否可以循环给定字符串的某个子字符串,以便我们返回上述字符串。一个简单的解决方案如下:

{(⊂⍵)∊(≢⍵)⍴¨¯1↓,\⍵}

或者,默认:

(⊂∊≢⍴¨¯1↓,\)

让我们解压 dfn:

{(⊂⍵)∊(≢⍵)⍴¨¯1↓,\⍵}
,\⍵ prefixes of the input
¯1↓ ignore the last one
(≢⍵)⍴¨ reshape each of the prefixes so
that it has the same length as input

(⊂⍵)∊ check if input appears anywhere in the list.

前缀是以下操作:

      ,\ 'Hello!'
┌─┬──┬───┬────┬─────┬──────┐
│H│He│Hel│Hell│Hello│Hello!│
└─┴──┴───┴────┴─────┴──────┘

关于reshape - 如何检查APL中的字符串是否被 reshape ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70395690/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com