三个“ R”:反向,重新排序,重复


31

在用数字涂鸦时,我发现可以从数字列表中生成一个有趣的排列。如果您重复相同的排列足够多次,您将始终回到原始数组。让我们使用以下列表:

[1, 2, 3, 4, 5]

举个例子

  1. 反转数组。现在我们的数组是

    [5, 4, 3, 2, 1]
    
  2. 重新排序(交换)每对。我们的列表有2对:[5, 4][3, 2]。不幸的是,我们不能将它们组合1为一对,所以我们将其留给自己。交换每对后,新数组为:

    [4, 5, 2, 3, 1]
    
  3. 重复步骤1和2,直到返回到原始数组。以下是以下4个步骤:

    Step 2:
    Start:          [4, 5, 2, 3, 1]
    Reversed:       [1, 3, 2, 5, 4]
    Pairs Swapped:  [3, 1, 5, 2, 4]
    
    Step 3:
    Start:          [3, 1, 5, 2, 4]
    Reversed:       [4, 2, 5, 1, 3]
    Pairs Swapped:  [2, 4, 1, 5, 3]
    
    Step 4:
    Start:          [2, 4, 1, 5, 3]
    Reversed:       [3, 5, 1, 4, 2]
    Pairs Swapped:  [5, 3, 4, 1, 2]
    
    Step 5:
    Start:          [5, 3, 4, 1, 2]
    Reversed:       [2, 1, 4, 3, 5]
    Pairs Swapped:  [1, 2, 3, 4, 5]
    
    # No more steps needed because we are back to the original array
    

    如果列表的长度n为奇数,则将总是恰好需要n步才能返回到原始数组。如果n为偶数,则总是需要2个步骤才能返回到原始数组,除非 n为2,否则将需要1个步骤(因为反转和交换是同一回事)。

今天的任务(您应该选择接受)是将这组步骤可视化以显示任意长度的列表。您必须编写一个程序或函数,该程序或函数将单个正整数n作为输入,并对list进行这组步骤[1, n]。您必须沿途输出每个中间步骤,这意味着要打印每个步骤,还是将它们全部作为步骤列表返回。只要很清楚您正在生成每个步骤,我对输出格式就不会很挑剔。这意味着(例如)以下任何一项:

  • 将每个步骤作为列表输出到STDOUT

  • 返回列表列表

  • 返回每个步骤的字符串表示形式列表

  • 返回/输出矩阵

是可以接受的。

您还必须输出原始数组,无论是在数组末尾还是开头都取决于您。(从技术上讲,两者都是正确的)

您将需要处理2的边缘情况,而不是2而是1步,因此请确保您的解决方案使用2的输入(并且1是另一种可能的边缘情况)。

像往常一样,这是,因此存在标准漏洞,并尝试使您的解决方案比您选择的语言短于其他任何语言(或者,如果您感到不适,甚至尝试击败通常比您短的另一种语言)挑战)。

测试IO

1: 
[1]


2: 
[1, 2]


3: 
[2, 3, 1]
[3, 1, 2]
[1, 2, 3]


4: 
[3, 4, 1, 2]
[1, 2, 3, 4]


5: 
[4, 5, 2, 3, 1]
[3, 1, 5, 2, 4]
[2, 4, 1, 5, 3]
[5, 3, 4, 1, 2]
[1, 2, 3, 4, 5]


7: 
[6, 7, 4, 5, 2, 3, 1]
[3, 1, 5, 2, 7, 4, 6]
[4, 6, 2, 7, 1, 5, 3]
[5, 3, 7, 1, 6, 2, 4]
[2, 4, 1, 6, 3, 7, 5]
[7, 5, 6, 3, 4, 1, 2]
[1, 2, 3, 4, 5, 6, 7]


9: 
[8, 9, 6, 7, 4, 5, 2, 3, 1]
[3, 1, 5, 2, 7, 4, 9, 6, 8]
[6, 8, 4, 9, 2, 7, 1, 5, 3]
[5, 3, 7, 1, 9, 2, 8, 4, 6]
[4, 6, 2, 8, 1, 9, 3, 7, 5]
[7, 5, 9, 3, 8, 1, 6, 2, 4]
[2, 4, 1, 6, 3, 8, 5, 9, 7]
[9, 7, 8, 5, 6, 3, 4, 1, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

从好的方面来说,这是一个巨大的测试案例:

27: 
[26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 1]
[3, 1, 5, 2, 7, 4, 9, 6, 11, 8, 13, 10, 15, 12, 17, 14, 19, 16, 21, 18, 23, 20, 25, 22, 27, 24, 26]
[24, 26, 22, 27, 20, 25, 18, 23, 16, 21, 14, 19, 12, 17, 10, 15, 8, 13, 6, 11, 4, 9, 2, 7, 1, 5, 3]
[5, 3, 7, 1, 9, 2, 11, 4, 13, 6, 15, 8, 17, 10, 19, 12, 21, 14, 23, 16, 25, 18, 27, 20, 26, 22, 24]
[22, 24, 20, 26, 18, 27, 16, 25, 14, 23, 12, 21, 10, 19, 8, 17, 6, 15, 4, 13, 2, 11, 1, 9, 3, 7, 5]
[7, 5, 9, 3, 11, 1, 13, 2, 15, 4, 17, 6, 19, 8, 21, 10, 23, 12, 25, 14, 27, 16, 26, 18, 24, 20, 22]
[20, 22, 18, 24, 16, 26, 14, 27, 12, 25, 10, 23, 8, 21, 6, 19, 4, 17, 2, 15, 1, 13, 3, 11, 5, 9, 7]
[9, 7, 11, 5, 13, 3, 15, 1, 17, 2, 19, 4, 21, 6, 23, 8, 25, 10, 27, 12, 26, 14, 24, 16, 22, 18, 20]
[18, 20, 16, 22, 14, 24, 12, 26, 10, 27, 8, 25, 6, 23, 4, 21, 2, 19, 1, 17, 3, 15, 5, 13, 7, 11, 9]
[11, 9, 13, 7, 15, 5, 17, 3, 19, 1, 21, 2, 23, 4, 25, 6, 27, 8, 26, 10, 24, 12, 22, 14, 20, 16, 18]
[16, 18, 14, 20, 12, 22, 10, 24, 8, 26, 6, 27, 4, 25, 2, 23, 1, 21, 3, 19, 5, 17, 7, 15, 9, 13, 11]
[13, 11, 15, 9, 17, 7, 19, 5, 21, 3, 23, 1, 25, 2, 27, 4, 26, 6, 24, 8, 22, 10, 20, 12, 18, 14, 16]
[14, 16, 12, 18, 10, 20, 8, 22, 6, 24, 4, 26, 2, 27, 1, 25, 3, 23, 5, 21, 7, 19, 9, 17, 11, 15, 13]
[15, 13, 17, 11, 19, 9, 21, 7, 23, 5, 25, 3, 27, 1, 26, 2, 24, 4, 22, 6, 20, 8, 18, 10, 16, 12, 14]
[12, 14, 10, 16, 8, 18, 6, 20, 4, 22, 2, 24, 1, 26, 3, 27, 5, 25, 7, 23, 9, 21, 11, 19, 13, 17, 15]
[17, 15, 19, 13, 21, 11, 23, 9, 25, 7, 27, 5, 26, 3, 24, 1, 22, 2, 20, 4, 18, 6, 16, 8, 14, 10, 12]
[10, 12, 8, 14, 6, 16, 4, 18, 2, 20, 1, 22, 3, 24, 5, 26, 7, 27, 9, 25, 11, 23, 13, 21, 15, 19, 17]
[19, 17, 21, 15, 23, 13, 25, 11, 27, 9, 26, 7, 24, 5, 22, 3, 20, 1, 18, 2, 16, 4, 14, 6, 12, 8, 10]
[8, 10, 6, 12, 4, 14, 2, 16, 1, 18, 3, 20, 5, 22, 7, 24, 9, 26, 11, 27, 13, 25, 15, 23, 17, 21, 19]
[21, 19, 23, 17, 25, 15, 27, 13, 26, 11, 24, 9, 22, 7, 20, 5, 18, 3, 16, 1, 14, 2, 12, 4, 10, 6, 8]
[6, 8, 4, 10, 2, 12, 1, 14, 3, 16, 5, 18, 7, 20, 9, 22, 11, 24, 13, 26, 15, 27, 17, 25, 19, 23, 21]
[23, 21, 25, 19, 27, 17, 26, 15, 24, 13, 22, 11, 20, 9, 18, 7, 16, 5, 14, 3, 12, 1, 10, 2, 8, 4, 6]
[4, 6, 2, 8, 1, 10, 3, 12, 5, 14, 7, 16, 9, 18, 11, 20, 13, 22, 15, 24, 17, 26, 19, 27, 21, 25, 23]
[25, 23, 27, 21, 26, 19, 24, 17, 22, 15, 20, 13, 18, 11, 16, 9, 14, 7, 12, 5, 10, 3, 8, 1, 6, 2, 4]
[2, 4, 1, 6, 3, 8, 5, 10, 7, 12, 9, 14, 11, 16, 13, 18, 15, 20, 17, 22, 19, 24, 21, 26, 23, 27, 25]
[27, 25, 26, 23, 24, 21, 22, 19, 20, 17, 18, 15, 16, 13, 14, 11, 12, 9, 10, 7, 8, 5, 6, 3, 4, 1, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]

打高尔夫球吧!


6
在前面生成原始范围可以吗?
HyperNeutrino

1
我认为示例的最后一行有错误。应该是1 2 3 4 5,不是1 2 4 3 5
Stewie Griffin

2
任何人都可以确认元素0在过程的开始和结束时永远只有1吗?
罗伯托·格雷厄姆

1
@RobertoGraham我有一个python脚本,可以验证array[0]直到开始的过程的开始和结束,该脚本的值都为1 n = 999。从模式上看,似乎对于每个奇数n,第一个元素一直1, n-1, 3, n - 3, 5, n - 5, 7...上升到n - 2, 3, n, 1,它将始终走n步。我看不出任何模式都会随着n的增加而改变。
DJMcMayhem

3
如果我们想证明当n为奇数时周期为n,则可能更容易跟踪元素1的位置:它遵循该路径,并且很容易通过归纳法表明在偶数位置x处的元素在一步之后移动到nx,并且位于奇数位置x的元素移至n-x + 2。因此,如果n = 2k + 1,那么在第2k步之后,第1步将在2k处,而在下一步骤中,第n-2k = 1步。1, n, 2, n-2, 4, n-4, 6, n-6, 8, n-8, ...
Misha Lavrov

Answers:


16

TI-Basic(83系列),58 57 54字节(104个字符)

:seq(I,I,1,Ans→A
:Ans→B
:Repeat prod(Ans=ᶫB
:ᶫB→C
:int(⁻Ans/2→D
:SortD(ᶫC,ᶫA
:SortD(ᶫD,ᶫA
:Pause ᶫA
:End

说明

接受输入Ans(例如,写入5:prgmNAME以使用大小为5的列表)。

创建给定大小的三个辅助列表(ᶫB在每个步骤中从中重新创建):ᶫB = ᶫC = {1,2,3,4,5,...}ᶫD = {-1,-1,-2,-2,-3,...}。在每个步骤中,对进行排序ᶫCᶫD以降序排列,对进行相同的排列ᶫA。在情况下ᶫC,这是相反ᶫA的情况,在情况下ᶫD,这是交换相邻对的原因,因为TI-Basic使用了真正愚蠢的选择排序实现SortD(可以对其进行尽可能多的相同元素重新排序。当再次ᶫA等于ᶫB时,我们停止。

不,严重的是,它们的内置排序算法是我对TI-Basic解释器的第二大抱怨。(我最大的抱怨是很多嵌套循环会降低解释器的速度,因为循环数据存储在堆栈中,但是堆栈是从错误的一端增长的,因此每次推入一个元素时计算器都必须移动整个堆栈或弹出。)但这一次很方便。


-1个字节:Pause存储要打印到的值,该值Ans比引用要短ᶫA

-3个字节:输入 Ans


选择排序真棒!
Riking


5

05AB1E13 11字节

LIGÂ2ôí˜})Ù

在线尝试!

说明

L             # range [1 ... input]
 IG           # input-1 times do:
   Â          # bifurcate
    2ô        # split into pieces of 2
      í       # reverse each
       ˜      # flatten
        }     # end loop
         )    # wrap stack in a list
          Ù   # remove duplicates

4

JavaScript(ES6),89 85

编辑保存的4个字节thx @JustinMariner

利用以下事实:任何元素在正确的位置,所有元素都在正确的位置。

n=>{for(l=[];n;l[n-1]=n--);while(alert(l=l.reverse().map((x,i)=>l[i^1]||x)),l[0]-1);}

少打高尔夫球

n => {
  for(l=[], i=0; i<n; l[i] = ++i);
  while( alert(l=l.reverse().map( (x,i) => l[i^1] || x)),
         l[0]-1);
}

测试

var F=
n=>{for(l=[];n;l[n-1]=n--);while(alert(l=l.reverse().map((x,i)=>l[i^1]||x)),l[0]-1);}

alert=x=>console.log(x+'') // to avoid popup stress

function update() {
  F(+I.value);
} 

update()
<input type=number id=I value=1 min=1 oninput='update()'>


我认为您可以将建立范围的循环缩短至for(l=[];n;l[n-1]=n--);在线尝试!
贾斯汀·马里纳

@JustinMariner向后哇,太棒了!谢谢
edc65

3

Mathematica,142个字节

(h=Range@#;W={};For[i=1,i<=#,i++,s=Reverse@h;AppendTo[W,h=Join[f=Flatten[Reverse/@Partition[s,{2}]],s~Complement~f]];s=h];DeleteDuplicates@W)&

3

JavaScript(ES6),79个字节

f=(n,a=[...Array(n)],b=a.map((x,i)=>n-((x?x-1:i)^1)||1))=>b[0]>1?b+`
`+f(n,b):b

输出每个步骤的列表。

注意,我们不需要初始化数组就可以使球滚动。如果未初始化(x未定义),则可以使用数组的索引(参数i)执行第一步:

b=a.map((x,i)=>n-((x?x-1:i)^1)||1)

测试用例:


3

R,109 95 94 79 74 62字节

如果代码不会在实际解决方案之上抛出警告(如果警告n为1,则没有警告,如果n为偶数,n则为3警告,如果n为奇数,则为警告),那么由于vector,以下内容的工作方式与之前的解决方案相似回收:

n=scan();m=1:n;w=0:n+2*1:0;while(print(m<-rev(m)[w[w<=n]])-1)n

在线尝试!

再次感谢@Giuseppe额外节省了12个字节!

先前的无警告解决方案,占用94个字节:

n=scan();m=s=1:n;while(any(print(m<-rev(m)[c(if(n>1)2:1+rep(seq(0,n-2,2),e=2),n[n%%2])])!=s))n

在线尝试!

原始解决方案为109字节

n=scan();m=s=1:n;repeat{cat(m<-rev(m)[c(t(embed(s,min(n,2))[!!s[-n]%%2,]),n[n%%2])],"\n");if(all(m==s))break}

在线尝试!


1
88个字节 - print返回其参数,因此我们可以在这里使用它。我认为我从未见过encode;这是一种整洁的索引方式!
朱塞佩

谢谢!尽管我需要将其延长一点,因为到目前为止,如果n = 1则无法使用。
plannapus

哦,我没有注意到......替换2embedmin(n,2)
朱塞佩

1
您可以放n而不是{}while循环,因为n它什么也没做。:)
Giuseppe

1
令人印象深刻的进步!!!0:n+2*1:01+0:n+c(1,-1)-4字节相同。any(print(...) != s)相当于any(print(...)-s)-1个字节。AAAND如果我们能证明,m[1]==1只有在算法结束,那么我们就可以放下any,所以我们得到while(print(...)-1),我们可以删除s,这样我们就得到62个字节,n=scan();m=1:n;w=0:n+2*1:0;while(print(m<-rev(m)[w[w<=n]])-1)n
朱塞佩

3

Japt20 18 15 12字节

õ
£=ò2n)ÔcÃâ

试试看-R仅出于可视化目的标记)

ETHproductions节省了1个字节。

               :Implicit input of integer U
õ              :Range [1,U]
\n             :Reassign to U
£              :Map
  ò            :  Partitions
   2           :    Of length 2
    n          :    Starting from the end
     )         :  End partition
      Ô        :  Reverse
       c       :  Flatten
 =             :  Reassign to U
        Ã      :End map
         â     :Deduplicate

截至目前,我相信w ò mw可以是ò2n)w
ETHproductions's

好的,谢谢@ETHproductions。快要走进酒吧了,所以我会在早上仔细看一下。
毛茸茸的

2

外壳,9个字节

U¡ȯṁ↔C2↔ḣ

在线尝试!

            -- implicit input N                 |  3
         ḣ  -- list [1..N]                      | [1,2,3]
 ¡(     )   -- iterate the following function,  | [[1,2,3],[2,3,1],[3,1,2],[1,2,3],...
U           -- until the first repetition:      | [[1,2,3],[2,3,1],[3,1,2]]
       ↔    --   reverse                        |   [3,2,1]
     C2     --   cut into two                   |   [[3,2],[1]]
   ṁ↔       --   reverse each pair & flatten    |   [2,3,1]

2

Ruby64 57 52 50字节

->x{(s=*w=1..x).map{s=w.map{|y|s[-y^1]||s[0]}}|[]}

在线尝试!

怎么运行的:

首先创建范围,然后重复排列x次:使用负索引,但翻转最后一位,所以我们得到序列-2,-1,-4,-3 ...如果x为偶数,则结束好吧,否则,我们将在最后添加其余元素。最后一步:过滤掉重复的数组(因此我们涵盖了所有情况:x = 1,x = 2,奇数和偶数)


2

Haskell,75 74字节

g(a:b:c)=b:a:g c
g x=x
h=g.reverse
0!x|x<[2]=[x]|1<2=x:0!h x
p n=0!h[1..n]

在线尝试!

g进行成对交换,h一个步骤(反向+重新排序),!重复应用h(并收集中间结果),直到恢复顺序。注意:!采用额外但未使用的额外参数0只是为了使其成为中缀运算符。主要功能p启动它。

编辑:感谢@Angs一个字节。


2
0!x而不是f x节省一个字节- 在线尝试!
昂斯

1

爪哇8,215个 214字节

import java.util.*;n->{Stack a=new Stack(),t;int i=0;for(;i<n;a.add(++i));t=(Stack)a.clone();Collections x=null;for(i=0;i<1|!a.equals(t);System.out.println(t))for(x.reverse(t),i=0;i<n;i++)if(i<n-1)x.swap(t,i,++i);}

我尝试使用实际的数组而不是列表来打高尔夫球,但是反转和交换都将占用太多字节。也许它可以组合成一个循环(而不是先反转然后交换),但是我还没有把这个想出来(解决;计算出;弄明白。
当然,这肯定可以打更多。

说明:

在这里尝试。

import java.util.*;           // Required import for Stack and Collections

n->{                          // Method with integer parameter and no return-type
  Stack a=new Stack(),        //  Original List
        t;                    //  Copy-List
  int i=0;                    //  Index-integer, starting at 0
  for(;i<n;a.add(++i));       //  Fill the original list with the integers
  t=(Stack)a.clone();         //  Make a copy of the List
  Collections x=null;         //  Static `Collections` to reduce bytes
  for(i=0;                    //  Reset index `i` to 0
      i<1                     //  Loop (1) as long as `i` is 0 (the first iteration),
      |!a.equals(t);          //  or the input array is not equal to the copy
      System.out.println(t))  //    After every iteration: print the modified List
    for(x.reverse(t),         //   Reverse the copied List
        i=0;                  //   Reset `i` to 0
        i<n;                  //   Inner loop (2) over the List
        i++)                  //     After every iteration: increase `i` by 1 again
      if(i<n-1)               //    Unless it's the last item in the List:
        x.swap(t,i,++i);      //     Swap the items at indexes `i` and `i+1` 
                              //     (by increasing `i` by 1 first with `++i`)
                              //   End of inner loop (2) (implicit / single-line body)
                              //  End of loop (1) (implicit / single-line body)
}                             // End of method

1

的Java(OpenJDK的8) 257个 245 243 226 206 205字节

n->{int i=0,k,t[]=new int[n];while(i<n)t[i]=++i;do{for(i=0;i<n/2;t[i]=t[n+~i],t[n+~i++]=k)k=t[i];for(k=1;k<n;t[k]=t[--k],t[k]=i,k+=3)i=t[k];System.out.println(java.util.Arrays.toString(t));}while(t[0]>1);}

在线尝试!



您还可以通过删除,f和重新使用来节省两个字节i
凯文·克鲁伊森

很好,您已经对其进行了很多改进!您现在甚至比我的Java答案还低。:)您可以更改for(i=0;i<n/2;k=t[i],t[i]=t[n+~i],t[n+~i++]=k);for(i=0;i<n/2;t[i]=t[n+~i],t[n+~i++]=k)k=t[i];
克鲁伊森

1

MATL,17个字节

:`tP2ePXz!tG:-a]x

在线尝试!

说明

:       % Implicit input: n. Push [1 2 ... n]
`       % Do...while
  t     %   Duplicate
  P     %   Reverse
  2e    %   Reshape into a 2-row matrix. A final 0 is added if needed
  P     %   Reverse each column
  Xz    %   Nonzero entries (i.e. remove final 0 if present). Gives a column vector
  !     %   Transpose into a row
  t     %   Duplicate
  G:    %   Push [1 2 ... n] again
  -     %   Subtract element-wise
  a     %   Any. Gives true if there is at least a nonzero value
]       % End. Go to next iteration if top of the stack is true.  So the loop ends
        % when [1 2 ... n] has been found again
x       % Delete top of the stack (which is [1 2  ... n]). Implicit display

1

Stax,17 个字节

âΩÄ─g╫B♥C╛♠ƒ?|πcD

运行并调试

说明

RX~Wr2/{r+}Fc|uPcx=C      # Full program, unpacked, implicit input
RX~                       # Create range, save to X register, pop back to input stack
   W                      # Start while loop until truthy value
    r                     # reverse array
     2/                   # Split into groups of 2
      {r+}F               # Loop through each set and reverse each
           c              # Copy top value
            |u            # Convert to string representation of array
              P           # Pop top value off
               cx=        # Copy top value, get value of x register, compare to top value
                  C       # If top value is truthy, cancel block and end

令人惊讶的是它的工作速度与它一样快,在我不想再对浏览器进行临时配置之前,它已经进行了399次测试。


0

JavaScript(ES6),122个字节

f=(n,a=[...Array(n)].map((_,i)=>i+1),r=[],b=a.map((_,i)=>a[n+~(i^1)]||a[0]))=>b.some((e,i)=>e>b[i+1],r.push(b))?f(n,b,r):r

r.push(a)可以用来代替r.push(b)将原始排列放在最前面。


0

Haskell,97个字节

这感觉有点长:(

f n|x<-[1..n]=x:takeWhile(/=x)(tail$iterate((r=<<).g.r)x)
r=reverse
g[]=[]
g x=take 2x:g(drop 2x)

在线尝试!

解释/取消

-- starting with x, accumulate the results of repeatedly
-- applying the function permute
f n = x : takeWhile (/=x) (tail $ iterate permute x)
  where x = [1..n]
        -- reverse, cut2, reverse each pair & flatten
        permute = concatMap reverse . cut2 . reverse

-- recursively transform a list into groups of 2
cut2 [] = []
cut2 xs = take 2 xs : g (drop 2 xs)

0

堆叠,42字节

[~>[rev 2#<$revflatmap]periodsteps behead]

在线尝试!

使用periodsteps内置函数执行给定的转换。但是,此内置函数返回所有元素,其中包括输入范围作为其第一个和最后一个元素。因此,我们将列表斩首,返回除第一个元素以外的所有元素。


0

AWK,123字节

不是很严格,但这是我能想到的最好的方法。

{for(N=$1;N>$i=++i;);for(;n++<(i%2?i:i>2?2:1);){for(k=0;k++<i;)K[k]=(z=i-k+(k-1)%2*2)?$z:$1;for(k=0;k++<N;){$k=K[k]}print}}

在线尝试!


0

Python 2中165个 159 138 81字节

x=input()+1
b=a=range(1,x)
while b:b=[b[x-min(x,i+1^1)]for i in a];print b;b*=a<b

在线尝试!

-20个字节,感谢@ChasBrown。(遗憾的是,我对扩展切片语法提出了整个挑战)

哇!GolfStorm(-57字节)!感谢IanGödel,tsh和Jonathan Frech。


而不是list(reversed(a))尝试a[::-1]
Chas Brown

' '*[2-(x<3),x][x%2]
tsh



1
@tsh [b,0][b==a]- > b*(a!=b)
乔纳森·弗雷希

0

JavaScript,136个字节

(n)=>{for(a=[],i=0;i<n;a[i]=++i);for(j=0;j<(n&1?n:2);j++){a.reverse();for(i=0;i<a.length-1;i += 2)m=a[i],a[i]=a[i+1],a[i+1]=m;alert(a)}}
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.