Numero di trasformazioni fino alla ripetizione


12

Data una sequenza di numeri interi o, per essere più specifici, una permutazione di 0..N trasformare questa sequenza come segue:

  • output [x] = reverse (input [input [x]])
  • ripetere

Ad esempio: [2,1,0]diventa [0,1,2]e è invertito [2,1,0]. [0,2,1]diventa [0,1,2]e si inverte [2,1,0].

Esempio 1

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

Esempio 2

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

Esempio 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

Esempio 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

Il tuo compito è definire una funzione (o programma) che accetta una permutazione di numeri interi 0..Ne restituisce (o genera) il numero di passi fino a quando si verifica una permutazione che è già avvenuta. Se si Xtrasforma in Xallora l'uscita dovrebbe essere zero, Se si Xtrasforma in Ye Yin X(o Y) allora l'uscita dovrebbe essere 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

Casi test:

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

Se la tua lingua non supporta le "funzioni", puoi presumere che la sequenza sia data come un elenco separato da spazi di numeri interi come 0 1 2o 3 1 0 2su una sola riga.

Fatti divertenti:

  • la sequenza 0,1,2,3, .., N si trasformerà sempre in N, ..., 3,2,1,0
  • la sequenza N, .., 3,2,1,0 si trasformerà sempre in N, .., 3,2,1,0
  • la sequenza 0,1,3,2, ..., N + 1, N si trasformerà sempre in N, ..., 3,2,1,0

Compito bonus : scopri una formula matematica.

Regole opzionali :

  • Se il primo indice della tua lingua è 1 anziché 0, puoi usare le permutazioni 1..N(puoi semplicemente aggiungerne uno a ogni numero intero nell'esempio e nelle prove).

Intendevo più come una "formula chiusa" come $ f (a_ {0}, a_ {1}, a _ {...}} = a_ {0} ^ a_ {1} + ... $ dove $ a_ { i} $ è l'i-esimo elemento nella sequenza indicata.
mroman,

Sei sicuro che esista una tale "formula chiusa"?
Todd Sewell,

" restituisce (o emette) il numero di passaggi fino a quando si verifica una permutazione che si è già verificata. " Ciò è incompatibile con quasi tutto ciò che lo segue. Per cominciare, rende impossibile un valore di ritorno di 0 ...
Peter Taylor,

Il terzo esempio è corretto? Vedo che 3,0,1,2dovrebbe trasformarsi in2,3,0,1
FireCubez il

È il numero di trasformazioni prima di una ripetizione.
mroman,

Risposte:


4

JavaScript (ES6), 54 byte

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

Provalo online!


Cosa fa []una funzione?
mroman,

Una funzione è un oggetto. Quindi, g[a]può essere utilizzato su di esso per accedere alla proprietà a.
Arnauld,

Ah capisco Stai usando gper memorizzare lo stato.
mroman,


3

Pyth, 10 9 8 byte

tl.u@LN_

Spiegazione:

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.

Suite di test .


3

Haskell, 52 byte

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

Provalo online!

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 byte

-9 byte grazie a nwellnhof

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

Provalo online!

Spiegazione:

{                              }  # 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 byte

-7 grazie al gorgogliatore

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

Provalo online!

Come

spiegazione originale. il mio ultimo miglioramento cambia solo il pezzo che trova "l'indice del primo elemento che abbiamo già visto". ora usa il "nub setaccio" per farlo in meno byte.

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

Nota l'intera frase finale 1<:@i.~[:({:e.}:)\è dedicata alla ricerca "dell'indice del primo elemento che è già stato visto". Questo sembra terribilmente lungo per ottenerlo, ma non sono stato in grado di giocarlo di più. Suggerimenti benvenuti.




Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.