繰り返しまでの変換の数


12

整数のシーケンスを指定するか、より具体的には、0..N 変換の順列を次のように指定します。

  • output [x] = reverse(input [input [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変換するYYするX(または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 2か、3 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に変換されます

ボーナスタスク:数式を計算します。

オプションのルール

  • 言語の最初のインデックスが0ではなく1の場合、順列を使用できます1..N(例とテストケースのすべての整数に1を追加するだけです)。

$ f(a_ {0}、a_ {1}、a _ {...}} = a_ {0} ^ a_ {1} + ... $のような「閉じた式」のようなものですi} $は、指定されたシーケンスのi番目の要素です
mroman

このような「閉じた式」が存在しますか?
トッドシーウェル

すでに発生した順列が発生するまでのステップ数を返します(または出力します)。」これは、それに続くほぼすべてと矛盾しています。まず、戻り値0を不可能にします...
Peter Taylor

3番目の例は正しいですか?に3,0,1,2変換する必要があります2,3,0,1
FireCubez

繰り返す前の変換の数です。
mroman

回答:




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バイト

nwellnhofのおかげで-9バイト

{($_,{.[[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.~^:(<@!@#)

オンラインでお試しください!

どうやって

元の説明。私の最後の改善は、「すでに見た最初の要素のインデックス」を見つける部分を変更するだけです。現在では、「nub Sieve」を使用して、バイト数を減らしています。

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.}:)\が「すでに見られている最初の要素のインデックス」を見つけることに専念していることに注意してください。これを入手するには非常に長いように思えますが、それ以上ゴルフをすることはできませんでした。提案を歓迎します。




弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.