# 以字典顺序排序生成一组前置-追加排列

14

• 从数字开始`1`

• 用于从每个数字`2``n`，放置这个号码到该序列的开头或结尾（或前置追加它，顺序因此得名）。

``````1
21     [beginning]
213    [end]
2134   [end]
``````

``````1234  [RRR]
2134  [LRR]
3124  [RLR]
3214  [LLR]
4123  [RRL]
4213  [LRL]
4312  [RLL]
4321  [LLL]
``````

xnor

3

Optimizer

1

2
FryAmTheEggman在您编辑答案前21分钟发布了答案。
Joe Z.

2
@Optimizer我不太认为这是最怪异的方法-FryAmTheEggman的答案是19字节长，比您早21分钟。这是最早发布的最短答案。
Joe Z.

10

# CJam，22 20 19 17个字节

``````]]l~{)f+_1fm>|}/p
``````

``````]]                   "Put [[]] onto stack. What we will do with this array of array is";
"that in each iteration below, we will first append the next";
"number to all present arrays, then copy all the arrays and";
"move the last element to first in the copy";
l~                 "Read input number. Lets call it N";
{         }/     "Run this code block N times ranging from 0 to N - 1";
)f+             "Since the number on stack starts from 0, add 1 to it and append";
"it to all arrays in the array of array beginning with [[]]";
_1fm>        "Copy the array of array and move last element from all arrays";
"to their beginning";
|       "Take set union of the two arrays, thus joining them and eliminating";
"duplicates. Since we started with and empty array and started adding";
"numbers from 1 instead of 2, [1] would have appeared twice if we had";
"simply done a concat";
p    "Print the array of arrays";
``````

``````]]l~ed{)edf+ed_ed1fm>ed|ed}/edp
``````

``````[[[]] 3]                                 "]]l~"            "Empty array of array and input";
[[[]] 1]                                 "{)"              "First iteration, increment 0";
[[[1]]]                                  "{)f+"            "Append it to all sub arrays";
[[[1]] [[1]]]                            "{)f+_"           "Copy the final array of array";
[[[1]] [[1]]]                            "{)f+_1fm>"       "shift last element of each";
"sub array to the beginning";
[[[1]]]                                  "{)f+_1fm>|}"     "Take set based union";
[[[1]] 2]                                "{)"              "2nd iteration. Repeat";
[[[1 2]]]                                "{)f+"
[[[1 2]] [[1 2]]]                        "{)f+_";
[[[1 2]] [[2 1]]]                        "{)f+_1fm>";
[[[1 2] [2 1]]]                          "{)f+_1fm>|}";
[[[1 2] [2 1]] 3]                        "{)";
[[[1 2 3] [2 1 3]]]                      "{)f+"
[[[1 2 3] [2 1 3]] [[1 2 3] [2 1 3]]]    "{)f+_";
[[[1 2 3] [2 1 3]] [[3 1 2] [3 2 1]]]    "{)f+_1fm>";
[[[1 2 3] [2 1 3] [3 1 2] [3 2 1]]]      "{)f+_1fm>|}";
[[[1 2 3] [2 1 3] [3 1 2] [3 2 1]]]      "{)f+_1fm>|}/";
``````

6

``````f 1=[[1]]
f n=(\x->map(++[n])x++map(n:)x)\$f\$n-1
``````

1

nimi 2015年

1
@nimi，但顺序错误r

@proudhaskeller：亲爱的，没有足够仔细地阅读规范。我试图对其进行修复，并发现了四种略有不同的方式，它们的长度与@alephalpha的版本相同，因此我无法提供改进。`f n=[x++[n]|x<-f\$n-1]++[n:x|x<-f\$n-1]``f n=map(++[n])(f\$n-1)++[n:x|x<-f\$n-1]``f n=map(++[n])(f\$n-1)++map(n:)(f\$n-1)``f n=(++[n])#n++(n:)#n;p#i=map p\$f\$i-1`
NIMI

5

# 蟒蛇2，68

``f=lambda n:[[1]]*(n<2)or[x*b+[n]+x*-b for b in[1,-1]for x in f(n-1)]``

“布尔” `b`编码是放在`[n]`开头还是结尾。实际上，我们`x`在表达式中移动列表的其余部分`x*b+[n]+x*-b`。把`b`作为`-1``1`通过否定允许采用翻转，因为乘以一个列表`-1`是空列表。

4

# 珀斯，19岁

``````usCm,+dH+HdGr2hQ]]1
``````

``````[[1]]
[([1,2], [2,1])]
``````

``````([1,2], [2,1])
[([1,2,3],[3,1,2]),([2,1,3],[3,2,1])]
([1,2,3],[2,1,3],[3,1,2],[3,2,1])
``````

2

# Mathematica，57 54 49字节

``````f@1={{1}};f@n_:=#@n/@f[n-1]&/@Append~Join~Prepend
``````

``````f[4]
``````

{{1、2、3、4}，{2、1、3、4}，{3、1、2、4}，{3、2、1、4}，{4、1、2、3} ，{4，2，1，3}，{4，3，1，2}，{4，3，2，1}}

2

# J，26个字节

``````   0|:<:((,,.,~)1+#)@[&0,.@1:

(0|:<:((,,.,~)1+#)@[&0,.@1:) 3
1 2 3
2 1 3
3 1 2
3 2 1
``````

FUZxxl 2015年

1

# Pyth， 343331 29

``````L?]]1<b2smm++*kdb*k_dy-b1,1_1
``````

``````L                                  define a function y with argument b that returns
?*]]1<b2                          [[1]] if b < 2 else
s                         sum(
m                        map(lambda d:
m                       map(lambda k:
++*kdb*k_d             k*d + [b] + k*-d
y-b1         , y(b - 1))
,1_1)    , (1, -1))
``````

FryAmTheEggman 2015年

FryAmTheEggman 2015年

@FryAmTheEggman您可能实际上想将其添加为自己的答案。太棒了
PurkkaKoodari'3

FryAmTheEggman 2015年

1

# 纯净重击，103

``````a=1..1
for i in {2..9} {a..u};{
((++c<\$1))||break
a={\${a// /,}}
a=`eval echo \$a\$i \$i\$a`
}
echo \${a%%.*}
``````

1

# JavaScript（ES6）73 80

@Optimizer的漂亮解决方案的JavaScript实现。

``R=(n,i=1,r=[[1]])=>++i>n?r:r.map(e=>r.push([i,...e])+e.push(i))&&R(n,i,r)``

``F=n=>(i=>{for(r=[[1]];++i<=n;)r.map(e=>r.push([i,...e])+e.push(i))})(1)||r``

``R(4)``

[[1、2、3、4]，[2、1、3、4]，[3、1、2、4]，[3、2、1、4]，[4、1、2、3] ，[4、2、1、3]，[4、3、1、2]，[4、3、2、1]]

0

``````public static void main(String[] args) {
listPrependAppend(4);
}

private static void listPrependAppend(int n) {
int total = (int) Math.pow(2, n - 1);
int ps;
boolean append;
String sequence;
String pattern;

for (int num = 0; num < total; num++) {
sequence = "";
pattern = "";
append = false;
ps = num;
for (int pos = 1; pos < n + 1; pos++) {
sequence = append ? (pos + sequence) : (sequence + pos);
append = (ps & 0x01) == 0x01;
ps = ps >> 1;
if (pos < n) {
pattern += append ? "L" : "R";
}
}
System.out.format("%s\t[%s]%n", sequence, pattern);
}
}
``````

2

Joe Z.

1
@BrettRyan您可以通过删除不必要的空格并使用一字符变量名称来使代码更短。您也可以替代`false`由类似`5<4`
ProgramFOX

1

Brett Ryan