半回文之谜


23

回文是一个本身相反的词。

现在有些单词看起来像回文,但看起来却不然。例如,考虑单词 sheeshsheesh不是回文,因为它的反向是hseehs不同的,但是如果我们认为sh是单个字母,那么它的反向是sheesh。我们将这种词称为半回文。

具体来说,如果我们可以将单词拆分成一定数量的块,则该单词为半回文,这样当块的顺序颠倒时,便形成了原始单词。(因为sheesh这些块是sh e e sh),我们也将不要求任何块包含两个单词的字母(否则每个单词都是半回文)。例如rear,不是半回文,因为它r ea r具有一个块(ea),其中包含来自原始单词两边的字母。我们认为奇数长度的单词的中心字符不在单词的两侧,因此对于奇数长度的单词,中心字符必须始终位于其自己的块中。

您的任务是获取正整数列表,并确定它们是否为半回文。您的代码应输出两个一致的不相等值,如果输入是半回文,则应输出一个,否则将输出另一个。但是,代码的字节序列本身必须是半回文

答案将以字节计分,而字节数越少越好。

测试用例

[] -> True
[1] -> True
[2,1,2] -> True
[3,4,2,2,3,4] -> True
[3,5,1,3,5] -> True
[1,2,3,1] -> False
[1,2,3,3,4,1] -> False
[11,44,1,1] -> False
[1,3,2,4,1,2,3] -> False

程序生成更多的测试用例。


borrible指出,这些与广义Smarandache回文相似。因此,如果您想做进一步的阅读,那是一个开始的地方。


2
为什么使用字符串定义半回文但输入是整数数组?除了令人困惑之外,这意味着我们无法使用自己的程序来测试源代码。
BradC

@BradC Palindromes之类的词通常用词来解释,因为这样做比较容易。
暴民埃里克(Erik the Outgolfer)'18年

@BradC字符串倾向于引入奇怪的边缘情况,尤其是在字符与字节之间。我选择数字是因为它们更简单。我认为,出于解释目的,单词会更容易。
小麦巫师

2
这些回文类型在文献中被称为广义Smarandache回文。
糟糕的

1
@RosLuP是的,“真实”回文也是半回文,只需按原样对待每个字符/整数,而无需其他“分块”。
BradC

Answers:


6

视网膜0.8.285个 69字节

M`^(.+,)*(\d+,)?(?<-1>\1)*$(?(1)^)|M`^(.+,)*(\d+,)?(?<-1>\1)*$(?(1)^)

在线尝试!说明:

M`

选择匹配模式。实际上,对于单行程序,Retina默认为匹配模式,但是如果没有这些多余的字符,则代码的第二个副本将始终匹配。

^

比赛必须从头开始。

(.+,)*

捕获许多字符。每次运行都必须以逗号结尾。

(\d+,)?

(可选)匹配数字和逗号。

(?<-1>\1)*

(可选)以相反的顺序匹配所有捕获,并在匹配时弹出每个捕获。

$

比赛必须在最后结束。

(?(1)^)

回溯,除非所有捕获都弹出。它的工作方式是,如果我们有未弹出的捕获,则要求匹配项仍位于字符串的开头,这是不可能的。


5

果冻27 23字节

ṖUṁ@Ƒ€ṚẸHḢŒŒHḢŒṖUṁ@Ƒ€ṚẸ

对于半回文式,返回1,否则返回0

在线尝试!

怎么运行的

ṖUṁ@Ƒ€ṚẸHḢŒŒHḢŒṖUṁ@Ƒ€ṚẸ  Main link. Argument: A (array)

          Π             Invalid token. Everything to its left is ignored.
           ŒH            Halve; divide A into two halves similar lengths. The middle
                         element (if there is one) goes into the first half.
             Ḣ           Head; extract the first half.
              ŒṖ         Generate all partitions of the first half.
                U        Upend; reverse each chunk of each partition.
                         Let's call the result C.

                     Ṛ   Yield R, A reversed.
                   Ƒ€    Fixed each; for each array P in C, call the link to the left
                         with arguments P and R.
                         Return 1 if the result is P, 0 if not.
                 ṁ@          Mold swapped; replace the n integers of C, in reading
                             order, with the first n integers of R.
                     Ẹ   Exists; check if one of the calls returned 1.

4

Python 2中157个 153 147 143字节

-4个字节感谢tsh

s=lambda x,i=0:len(x)<2or[]<x[i:]and(x[-i:]==x[:i])&s(x[i:-i])|s(x,i+1)
s=lambda x,i=0:len(x)<2or[]<x[i:]and(x[-i:]==x[:i])&s(x[i:-i])|s(x,i+1)

在线尝试!


1
更改x==x[::-1]len(x)<2节省2 * 2字节;143字节
tsh

4

05AB1E59 47 43 41 字节

2äøø€.œ`âʒ`RQ}gĀIg_^q2äøø€.œ`âʒ`RQ}gĀIg_^

-12个字节感谢@Emigna

在线尝试验证所有测试用例

说明:

2ä               # Split the input into two parts
                 #  i.e. [3,4,2,0,2,3,4] → [[3,4,2,0],[2,3,4]]
  øø             # Zip twice without filler
                 # This will remove the middle item for odd-length inputs
                 #  i.e. [[3,4,2,0],[2,3,4]] → [[3,2],[4,3],[2,4]] → [[3,4,2],[2,3,4]]
    €.œ          #  Then take all possible partitions for each inner list
                 #   i.e. [[3,4,2],[2,3,4]]
                 #    → [[[[3],[4],[2]],[[3],[4,2]],[[3,4],[2]],[[3,4,2]]],
                 #       [[[2],[3],[4]],[[2],[3,4]],[[2,3],[4]],[[2,3,4]]]]
`                # Push both lists of partitions to the stack
 â               # Take the cartesian product (all possible combinations) of the partitions
                 #  i.e. [[[[3],[4],[2]],[[2],[3],[4]]],
                 #        [[[3],[4],[2]],[[2],[3,4]]],
                 #        ...,
                 #        [[[3,4,2]],[[2,3,4]]]]
  ʒ   }          # Filter this list of combinations by:
   `             #  Push both parts to the stack
    RQ           #  Check if the second list reversed, is equal to the first
                 #   i.e. [[3,4],[2]] and [[2],[3,4]] → 1 (truthy)
       gĀ        # After the filter, check if there are any combinations left
                 #  i.e. [[[[3,4],[2]],[[2],[3,4]]]] → 1 (truthy)
         Ig_     # Check if the length of the input was 0 (empty input-list edge-case)
                 #  i.e. [3,4,2,0,2,3,4] → 7 → 0 (falsey)
            ^    # Bitwise-XOR
                 #  i.e. 1 XOR 0 → 1 (truthy)
             q   # Stop the program (and then implicitly output the top of the stack)
2äøø€.œ`âʒ`RQ}gĀIg_^
                 # Everything after the `q` are no-ops to comply to the challenge rules

您可以使用带有øøε.œ}`的奇数长度列表来解决此问题,节省6个字节。您似乎还留下了30个未使用的字节...
Emigna '18

@Emigna的无人操作人员最终必须遵守挑战赛的受限制来源要求
Kamil Drakari

@KamilDrakari:哦,对。忘了那部分。好消息是,保存6字节将是12个字节:)
Emigna '18

@Emigna非常聪明的双拉链技巧。我对此部分不满意,但这要好得多!顺便说一句,由于Elixir重写了2字节命令,因此可以使用代替ε }。:)
Kevin Cruijssen

@KevinCruijssen:太酷了。我不知道
Emigna '18 -10-16

4

05AB1E,37 个字节

使用与Jonathan想出的大致相同的技术。

.œʒ€gηOZ;îå}εÂQ}ZĀqĀZ}QÂε}åî;ZOηg€ʒ.œ

在线尝试!


.œʒ€gηOZ;îå}εÂQ}ZĀqĀZ}QÂε}åî;ZOηg€ʒ.œ

完整程序。从STDIN接收列表,向STDOUT 输出10

.œʒ        }

筛选保留满足以下条件的分区:

   €gηOZ;îå

这种情况:每个(€g)的长度存储在一个列表中,η然后将其前缀()相加(O),从而得出长度列表的累积总和。然后,该列表最大值的最高一半被压入堆栈-但是也将原始列表也保留在其上(Z;î),如果原始列表å以累积总和的形式出现(),则该函数将返回true。

εÂQ}

对于每一个,比较(Q一个一个反转,这是堆栈通过在分别推Â。返回0 s和1 s 的列表。

ZĀq

最大。如果有任何事实,则为1,否则为0。结束执行。随后的一切都将被完全忽略。


3

Python 2中275个 251 205字节

-24字节,感谢@KevinCruijssen

-44字节感谢@PostLeftGhostHunter

@KevinCruijssen多了-2个字节

def s(x):
 l=len(x)
 if l<2:return 1>0
 for i in range(1,l/2+1):
	if x[l-i:]==x[:i]:return s(x[i:l-i])
def s(x):
 l=len(x)
 if l<2:return 1>0
 for i in range(1,l/2+1):
	if x[l-i:]==x[:i]:return s(x[i:l-i])

对于半回文返回True,否则返回

在线尝试!


1
或只返回1
Jo King

为什么s(x)定义了两次?
Y Wit博士,

因为他们说算作回文...但是可以定义一个同名函数???
RosLuP

@RosLuP是的,可以。第二个只覆盖了第一个
乔金

3

果冻 33  32 字节

-1感谢总干事埃里克(Erik the Outgolfer)
也感谢丹尼斯(Dennis)的错误修复,并希望改变Jelly中的实现细节。

ẸƇŒḂƇƊ$ƊĊHṀċÄẈṖŒŒṖẈÄċṀHĊƊ$ƊƇŒḂƇẸ

半回文产生1,其他产生0

O(2n)

或查看测试套件

唯一的块是ŒḂS({3 和4 } VS {29 &30 }字节),只是以允许代码来解析。

怎么样?

所有工作均在右侧-“主链接”进行:

ŒṖẈÄċṀHĊƊ$ƊƇŒḂƇẸ - Main Link: list
ŒṖ               - all partitions
           Ƈ     - filter keep those for which this is truthy (i.e. non-zero):
          Ɗ      -   last three links as a monad:
  Ẉ              -     length of each
         $       -     last two links as a monad:
   Ä             -       cumulative addition
        Ɗ        -       last three links as a monad:
     Ṁ           -         maximum
      H          -         halve
       Ċ         -         ceiling
    ċ            -     count
              Ƈ  - filter keep those for which this is truthy:
            ŒḂ   -   is palindrome?
               Ẹ - any?

3

Perl 6的87 79个字节

-8字节,以及Jo King的一些技巧

$!={/\s/&&/^(.+)\s[(.+)\s]*$0$/&&$1.$!}#$!={/\s/&&/^(.+)\s[(.+)\s]*$0$/&&$1.$!}

在线尝试!

tsh的JavaScript答案端口。返回两个不同的Regex对象。




1

C(gcc)(X86),216字节

p(L,a,n)int*a;{return n?(memcmp(a,a+L-n,n*4)|p(L-2*n,a+n,L/2-n))&&p(L,a,n-1):1<L;}
#define p(L,a)p(L,a,L/2)//p(L,a,n)int*a;{return n?(memcmp(a,a+L-n,n*4)|p(L-2*n,a+n,L/2-n))&&p(L,a,n-1):1<L;}
#define p(L,a)p(L,a,L/2)

在线尝试!

p(L,a,n)如果alength 数组L是一个半回文,则返回0 ,否则返回1。假设>n已经检查了所有length 的前缀,则将length的前缀与length n的后缀进行比较np(L,a)是入口点。

不幸的是,更有趣的解决方案更长:

224字节

(f(L,a,n))//#define p(L,a)(n=L/2,
int*a,n;
{return n?(memcmp(a,a+L-n,n*4)|f(L-2*n,a+n,L/2-n))&&f(L,a,n-1):1<L;}//{return n?(memcmp(a,a+L-n,n*4)|f(L-2*n,a+n,L/2-n))&&f(L,a,n-1):1<L;}
int*a,n;
#define p(L,a)(n=L/2,f(L,a,n))//(

在线尝试!

取消高尔夫:

(f(L,a,n)) //#define p(L,a)(n=L/2,
int*a,n;
{
  return n 
    ? (memcmp(a, a+L-n, n*4) | f(L-2*n, a+n, L/2-n)) &&
      f(L,a,n-1)
    : 1 < L;
} // { ... } 
int*a,n;
#define p(L,a)(n=L/2,f(L,a,n)) //(

1

Japt,66字节


@¯X eUsXn}a1 "
ʧV?UÊ<2:ßUéV sVÑ
@¯X eUsXn}a1 "
ʧV?UÊ<2:ßUéV sVÑ

Japt口译员

这个版本有了很大的改进,实际上已经击败了大多数实用语言。由于以前的方法存在错误,因此现在可以对整数数组进行操作。

说明:

@        }a1         Find the first number n > 0 such that...
 ¯X                   the first n elements
     UsXn             and the last n elements
    e                 are the same

"
ʧV?UÊ<2:ßUéV sVÑ    String literal to make it a Semi-palindrome
@¯X eUsXn}a1 "

ʧV?                 If n >= length of input...
    UÊ<2              return true if the length is less than 2
        :            Otherwise...
          UéV         move n elements from the end of the input to the start
              sVÑ     remove the first 2*n elements
         ß            and repeat on the remaining elements

0

PHP 237字节

function f($a){for($x=2>$c=count($a);++$i<=$c/2;)$x|=($s=array_slice)($a,0,$i)==$s($a,-$i)&f($s($a,$i,-$i));return$x;}#function f($a){for($x=2>$c=count($a);++$i<=$c/2;)$x|=($s=array_slice)($a,0,$i)==$s($a,-$i)&f($s($a,$i,-$i));return$x;}

递归函数,返回true(对于包含少于两个元素的输入),1对于真,则
0返回falsy。 在线尝试(包含故障)。

实际代码长度为118个字节;通过代码重复创建的半回文。

为了获得更好的性能,更换&&&并插入!$x&&之前++$i


0

Scala,252个字节

def^(s:Seq[Int]):Int={val l=s.size;if(l>1)(1 to l/2).map(i=>if(s.take(i)==s.takeRight(i))^(s.slice(i,l-i))else 0).max else 1}//def^(s:Seq[Int]):Int={val l=s.size;if(l>1)(1 to l/2).map(i=>if(s.take(i)==s.takeRight(i))^(s.slice(i,l-i))else 0).max else 1}

在线尝试!

PS。显然,为了满足源代码也是半回文的要求,解决方案要长两倍。

PPS。不是候选代码,而是使用模式匹配的纯功能解决方案:

  def f(s:Seq[Int], i:Int=1):Int = {
    (s, i) match {
      case (Nil ,_) => 1
      case (Seq(_), _) => 1
      case (l, _) if l.take(i) == l.takeRight(i) => f(l.slice(i,l.size-i), 1)
      case (l, j) if j < l.size/2 => f(l, i+1)
      case (_, _) => 0
    }
  }

挑战还要求您的代码也必须是半回文式。这是挑战中的最大乐趣。
小麦巫师

@PostLeftGhostHunter,我在注释中添加了原始源代码以满足要求。顺便说一句,使源代码成为半回文的乐趣是什么?如果我没看错,那么在没有此要求的情况下,该线程中的每个解决方案都会缩短两倍。您知道不是那样的解决方案吗?
Y Wit博士,

0

Perl 6,81个字节

($!={/../&&/^(.+)(.*)$0$/&&$1.$!})o&chrs#($!={/../&&/^(.+)(.*)$0$/&&$1.$!})o&chrs

在线尝试!

返回/../True的正则表达式/^(.+)(.*)$0$/和False 的正则表达式。与nwellnhof的答案类似,但会事先将列表转换为字符串。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.