直到重复的转换次数


12

给定一个整数序列,或更具体地讲,是0..N 变换的排列,此序列如下:

  • 输出[x] =反向(输入[输入[x]])
  • 重复

例如:[2,1,0]变为[0,1,2]和反转为[2,1,0][0,2,1]变成[0,1,2]并反转[2,1,0]

例子1

In:   0 1 2
S#1:  2 1 0
S#2:  2 1 0
Output: 1

例子2

In:   2 1 0
S#1:  2 1 0
Output: 0

例子3

In:   3 0 1 2
S#1:  1 0 3 2
S#2:  3 2 1 0
S#3:  3 2 1 0
Output: 2

例子4

In:   3 0 2 1
S#1:  0 2 3 1
S#2:  2 1 3 0
S#3:  2 0 1 3
S#4:  3 0 2 1
Output: 3

您的任务是定义一个函数(或程序),该函数接受整数的排列0..N并返回(或输出)步数,直到发生排列为止。如果X将转换为,X则输出应为零;如果X将转换为YYX(或Y),则输出应为1。

Y -> Y: 0 steps
Y -> X -> X: 1 step
Y -> X -> Y: 1 step
A -> B -> C -> D -> C: 3 steps
A -> B -> C -> D -> A: 3 steps
A -> B -> C -> A: 2 steps
A -> B -> C -> C: 2 steps
A -> B -> C -> B: also 2 steps

测试用例:

4 3 0 1 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps 
4 3 2 1 0 -> 4 3 2 1 0: 0 steps
4 3 1 2 0 -> 4 1 3 2 0 -> 4 3 2 1 0 -> 4 3 2 1 0: 2 steps
1 2 3 0 4 -> 4 1 0 3 2 -> 0 3 4 1 2 -> 4 3 2 1 0 -> 4 3 2 1 0: 3 steps
5 1 2 3 0 4 -> 0 5 3 2 1 4 -> 1 5 3 2 4 0 -> 1 4 3 2 0 5 -> 
  5 1 3 2 0 4 -> 0 5 3 2 1 4: 4 steps

如果您的语言不支持“函数”,则可以假定该序列是由空格分隔的整数列表,例如单行0 1 23 1 0 2

有趣的事实:

  • 序列0,1,2,3,..,N将始终转换为N,...,3,2,1,0
  • 序列N,..,3,2,1,0将始终转换为N,..,3,2,1,0
  • 序列0,1,3,2,...,N + 1,N将始终转换为N,...,3,2,1,0

奖励任务:找出一个数学公式。

可选规则

  • 如果您的语言的第一个索引是1而不是0,则可以使用排列1..N(您可以在示例和测试用例中向每个整数添加一个)。

我的意思更像是一个“封闭式”,例如$ f(a_ {0},a_ {1},a _ {...}} = a_ {0} ^ a_ {1} + ... $其中$ a_ { i} $是给定序列中的第i个元素
mroman

您确定存在这样的“封闭公式”吗?
Todd Sewell

返回(或输出)直到已经发生置换为止的步骤数。 ”这与它后面的所有内容都不一致。首先,它使返回值0成为不可能...
Peter Taylor

第三个例子正确吗?我认为3,0,1,2应该转换为2,3,0,1
FireCubez

这是重复之前的转换次数。
mroman '18

Answers:


4

JavaScript(ES6),54个字节

a=>~(g=a=>g[a]||~-g(g[a]=a.map(i=>a[i]).reverse()))(a)

在线尝试!


这是什么[]做的功能?
mroman

函数是对象。因此,g[a]可以在其上使用该属性a
Arnauld

啊,我明白了。您g
通常


3

Pyth,10 9 8字节

tl.u@LN_

说明:

t               One less than
 l              the number of values achieved by
  .u            repeating the following lambda N until already seen value:
    @LN_N         composing permutation N with its reverse
         Q      starting with the input.

测试套件


3

Haskell,52个字节

([]#)
a#x|elem x a= -1|n<-x:a=1+n#reverse(map(x!!)x)

在线尝试!

a # x                -- function '#' takes a list of all permutations
                     -- seen so far (-> 'a') and the current p. (-> 'x')
  | elem x a = -1    -- if x has been seen before, return -1 
  | n<-x:a =         -- else let 'n' be the new list of seen p.s and return
    1 +              -- 1 plus
       n #           -- a recursive call of '#' with the 'n' and
        reverse ...  -- the new p.

([]#)                -- start with an empty list of seen p.s 

3

Perl 6的44 35个字节

-9字节归功于nwellnhof

{($_,{.[[R,] |$_]}...{%.{$_}++})-2}

在线尝试!

说明:

{                              }  # Anonymous code block
                  ...    # Create a sequence where:
  $_,  # The first element is the input list
     {.[[R,] |$_]} # Subsequent elements are the previous element reverse indexed into itself
                     {        }    # Until
                      %.{$_}       # The index of the listt in an anonymous hash is non-zero
                            ++     # Then post increment it
 (                            )-2  # Return the length of the sequence minus 2

2

J,33 27 26字节

-7多亏了起泡器

_1(+~:i.0:)|.@C.~^:(<@!@#)

在线尝试!

怎么样

原始的解释。我最后的改进只是更改了找到“我们已经看到的第一个元素的索引”的部分。现在,它使用“小块筛子”以更少的字节数完成操作。

1 <:@i.~ [: ({: e. }:)\ |.@C.~^:(<@!@#)
                        |.@C.~          NB. self-apply permutation and reverse
                              ^:        NB. this many times:
                                (<@!@#) NB. the box of the factorial of the
                                        NB. the list len.  this guarantees
                                        NB. we terminate, and the box means
                                        NB. we collect all the results
         [: ({: e. }:)\                 NB. apply this to those results:
                      \                 NB. for each prefix
             {: e. }:                   NB. is the last item contained in 
                                        NB. the list of previous items?
1 <:@i.~                                NB. in that result find:
1    i.~                                NB. the index of the first 1
  <:@                                   NB. and subtract 1

请注意,整个最后一句话1<:@i.~[:({:e.}:)\专用于查找“已经看到的第一个元素的索引”。这似乎很久了,但是我无法再打高尔夫了。欢迎提出建议。




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.