Ordina una sequenza concatenata


17

Considera una sequenza basata sulle relazioni di ricorrenza f(n) = f(n-1)+f(n-2), a partire da f(1) = x1, f(2) = x2. Per x1 = 2, x2 = 1, la sequenza inizia in questo modo:

2  1  3  4  7  11  18  29  47  76  123  199  322  521  843

Concatenarlo in una stringa darà:

213471118294776123199322521843

Ora, dividi questo elenco nei numeri più piccoli possibili che danno y(n) > y(n-1). Inizia con il primo numero, quindi il secondo ecc. Il primo numero di output dovrebbe essere sempre una singola cifra. Riempi l'ultimo numero con il numero richiesto di zeri.

2 13 47 111 829 4776 12319 93225 218430

Otterrai due numeri, (x1, x2)come input, in qualsiasi formato conveniente e la sfida è quella di produrre l'elenco ordinato.

Regole:

  • Funzione e programmi sono OK
  • La sequenza iniziale deve avere esattamente 15 numeri (l'ultimo numero è f(15)).
  • x1e x2sono non negativi (zero è possibile).
  • L'output può essere in qualsiasi formato conveniente
  • Il vettore di output ydeve essere creato in modo tale y2 > y1.
    • Prima il più piccolo possibile y1, poi il più piccolo possibile y2, quindi y3e così via.
  • Se x1 = x2 = 0quindi emette 15 zeri (sullo stesso formato dell'altro output, ovvero no 000000000000000).

Esempi :

Input: 1 1
Output: 1  12  35  81  321  345  589  1442  3337 7610

Input: 3 2
Output: 3  25  71  219  315  0811 3121  23435 55898 145300
                             |
                             Optional leading zero 
Input: 0 0
Output: 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

Vince il codice più breve in byte. Se possibile, includere un collegamento a un interprete online.


Cosa intendi esattamente con "i numeri più piccoli possibili"? Media più piccola? Il più piccolo massimo? Qualcos'altro?
Isaacg,

@isaacg Così come l'ennesimo numero è maggiore di (n-1) th.
nicael,

1
Per chiarire la mia domanda, quale sarebbe la giusta divisione di 5467? 54 67? 5 46 70?
Isaacg,


3
La cosa 0 sembra un'eccezione piuttosto fastidiosa e inutile.
Martin Ender,

Risposte:


1

Pyth, 56 byte

LsgM.:sMb2?sQsM.WyHX_1Z`0u?yGX_1GHaGHjkhM.u,eNsN14QYmZ15

Suite di test

Spiegazione:

Innanzitutto, controlliamo se l'input è preciso 0, 0. In tal caso, stampa 15 zeri.

Altrimenti, produciamo la sequenza, con jkhM.u,eNsN14Q. Questo è simile all'algoritmo Pyth standard per la sequenza di Fibonacci.

Successivamente, riduciamo su questa stringa. L'accumulatore è un elenco di stringhe, che rappresenta ciascun numero nella sequenza divisa. Ad ogni passo della riduzione, prendiamo il carattere successivo e controlliamo se l'accumulatore è in ordine, usando la funzione helper y, definita con LsgM.:sMb2, che è veritiera se l'ingresso è fuori servizio. Se è in ordine, aggiungiamo il carattere successivo all'elenco come il suo numero. In caso contrario, aggiungiamo il carattere successivo alla fine dell'ultima stringa. Questo si ottiene con u?yGX_1GHaGH ... Y.

Successivamente, eseguiamo un ciclo while funzionale. Il ciclo continua fino a quando l'elenco in esecuzione non è in ordine, riutilizzando la funzione di supporto. Ad ogni passaggio, a 0viene aggiunto alla fine dell'ultima stringa nell'elenco. Questo si ottiene con .WyHX_1Z`0.

Infine, le stringhe vengono convertite in numeri interi, con sMe stampate.


Pyth, 51 byte

LsgM.:sMb2?sQsM.WyHX_1Z`0hf!yT_./jkhM.u,eNsN14QmZ15

Credo che funzioni, ma è troppo lento per testarlo: è una soluzione di forza bruta per dividere la corda.


Apporterò alcuni miglioramenti alla Xfunzione, ma il codice sopra funziona nella versione di Pyth che era più recente quando la domanda è stata pubblicata.


5

JavaScript ES6, 127 135

(a,b)=>eval("for(n=r=[],v=13,o=a+n+b;v--;a=b,b=t)o+=t=b+a;for(d of o+'0'.repeat(99))(n+=d)>+v&&(r.push(v=n),n='');+v?r:[...o]")

Test

F=(a,b)=>eval("for(n=r=[],v=13,o=a+n+b;v--;a=b,b=t)o+=t=b+a;for(d of o+'0'.repeat(99))(n+=d)>+v&&(r.push(v=n),n='');+v?r:[...o]")

// less golfed

U=(a,b)=>{
  for(n=r=[], o=a+n+b, v=13; v--; a=b, b=t)
    o+= t= b+a;
  for(d of o+'0'.repeat(99))
    if ((n+=d) > +v)
      r.push(v=n), n='';
  return +v ? r : [...o]
}

function test(){
  var i = I.value.match(/\d+/g)
  O.textContent = i.length > 1 ? F(+i[0],+i[1]) : ''
}
test()
A,B : <input id=I value='0 1' oninput='test()'>
<pre id=O></pre>


C'è un errore per x1 = 0, x2> 0, ad es. Input "0 1".
terremoto del

@flornquake fixed. Il conteggio dei byte rimane lo stesso, avendo ridotto un po 'il codice di riempimento zero
edc65

2

JavaScript ES6, 187 180 187 184 182 179 175 172 165 160 155 154 154 byte

(a,b)=>eval('d=""+a+b;for(i=-12,j=1;++i<99;)i<2?(c=b,d+=b=a+b,a=c,r=a?[d[0]]:"0,".repeat(15)):(f=+d.slice(j,i))>r[r.length-1]?(r.push(f),j=++i-1):d+=0;r')

Ottengo risultati simili quando lo eseguo per 1,1e 3,2test casi. 0,0ha preso 26 byte in eccesso ...

De-golf + convertito in ES5 + demo:

function s(a, b) {
  d = "" + a + b;
  for (i = -12, j = 1; ++i < 99;)
    i < 2 ?
      (c = b, d += b = a + b, a = c, r = a ? [d[0]] : "0,".repeat(15))
    : (f = +d.slice(j, i)) > r[r.length - 1] ?
      (r.push(f), j = ++i - 1)
      : d += 0;
  return r
}
document.write(
   s(1,1)+"<br>"+
   s(3,2)+"<br>"+
   s(0,0)
)


Perché produce più numeri? E non dovrebbe essere facile da risolvere? Il requisito è n <= 15.
Stewie Griffin,

@Stewie Ma hey, il primo produce 12 e il secondo 11. Questo è più piccolo di 15.
nicael

La sequenza iniziale f(n) = f(n-1)+f(n-2)ha un valore massimo di esattamente 15. Il numero di valori di output è determinato in base all'algoritmo, nient'altro.
Stewie Griffin,

@Stewie ok, quindi devono essere esattamente 15, giusto? Quindi, per n <= 15 vuoi dire che i numeri di input sono inferiori a 15?
nicael,

Il numero di valori nella sequenza iniziale è 15. I valori iniziali f(1)=x1e f(2)=x2possono essere superiori a 15. Il numero di valori di output viene determinato in base ai valori di input. Perché 3 2sarà 10.
Stewie Griffin,

1

JavaScript (ES6), 162 byte

(a,b)=>(k=[...Array(15).keys(y="")],p=-1,z=k.map(_=>0),a|b?[...k.map(f=n=>n--?n?f(n)+f(n-1):b:a).join``,...z].map(d=>+(y+=d)>p?(p=y,y=" ",p):"").join``:z.join` `)

Spiegazione

(a,b)=>(
  k=[...Array(15).keys(y="")],     // k = array of numbers 0 to 14, initialise y
  p=-1,                            // initialise p to -1 so that 0 is greater than p
  z=k.map(_=>0),                   // z = array of 15 zeroes
  a|b?[                            // if a and b are not 0
      ...k.map                     // for range 0 to 14
      (f=n=>n--?n?f(n)+f(n-1):b:a) // recursive sequence function (0 indexed)
      .join``,                     // join result of f(0) to f(14) as a string
      ...z                         // append zeroes for padding
    ].map(d=>                      // for each digit of concatenated result
      +(y+=d)                      // append the digit to the current number y
      >p?(                         // if the current number is greater than the previous p
        p=y,                       // set previous to the current number
        y=" ",                     // reset y (with space as a separator)
        p                          // output the current number (with space at the start)
      ):""                         // else add nothing to the output
    )
    .join``                        // return the output as a string
  :z.join` `                       // return a bunch of zeroes if a and b are 0
)

Test


1

Mathematica, 192 byte

f[{0,0}]:=0~Table~15
f@l_:=(t=0;q={};If[#>0,q~Join~{10^⌈Log10[t/#]⌉#},q]&[Last@#]&@FoldList[If[#>t,AppendTo[q,t=#];0,#]&[10#+#2]&,0,Flatten@IntegerDigits@SequenceFoldList[#+#2&,l,Range@13]])

Casi test:

f[{2, 1}]
(* {2, 13, 47, 111, 829, 4776, 12319, 93225, 218430} *)
f[{3, 2}]
(* {3, 25, 71, 219, 315, 811, 3121, 23435, 55898, 145300} *)
f[{0, 0}]
(* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} *)

Le lunghezze dei nomi delle funzioni mi stanno uccidendo.


1

Haskell, 165 159 152 142 141 byte

w=take 15
x#y=x:scanl(+)y(x#y)
0%0=w[0,0..]
x%y=g(-1)(w(x#y)++0%0>>=show)(-1)
g _""_=[]
g b l@(h:t)a|b>a=b:g 0l b|1<2=g(max 0b*10+read[h])t a

Esempio di utilizzo: 3 % 2-> [3,25,71,219,315,811,3121,23435,55898,145300].

Demo online (con un mainwrapper).

Come funziona:

w=take 15
x#y=x:scanl(+)y(x#y)              -- fibonacci sequence generator for x and y

0%0=w[0,0..]                      -- special case 0%0
x%y=g(-1)(w(x#y)++0%0>>=show)(-1) -- calculate fib sequence, add some extra 0 and
                                  -- flatten all digits into a single string.
                                  -- start calculating the resulting sequence

g _""_=[]                         -- if we don't have digits left, stop.
                                  -- the final 0 in the second parameter is ignored.
g b l@(h:t)a
  |b>a=b:g 0l b                   -- if the current number is greater than the
                                  -- previous one, take it and start over.
  |1<2=g(max 0b*10+read[h])t a    -- otherwise add the next digit and retry.
                                  -- The "max" fixes the initial call with -1.

0

PowerShell, 167 166 byte

param($x,$w)if($w-lt($x-eq0)){"0`n"*15;exit}[char[]]("$x"+-join(0..13|%{$w;$w=$x+($x=$w)}))|%{$z+="$_";if(+$z-gt$y){($y=$z);$z=""}};if($z){while(+$z-lt$y){$z+="0"}$z}

Ho salvato un byte eliminando la $svariabile e alimentando direttamente il loop di output.

Ungolf e commentato:

param($x,$w)           # Take input parameters as x and w
if($w-lt($x-eq0)){     # If x=0, ($x-eq0)=1, so $w-lt1 implies w=0 as well
  "0`n"*15             # Print out 15 0's separated by newlines
  exit                 # And exit program
}                      # otherwise ...
[char[]](              # Construct the sequence string as a char-array
"$x"+-join(            # Starting with x and concatenated with a joined array
  0..13|%{             # Loop
    $w                 # Add on w
    $w=$x+($x=$w)      # Recalculate for next loop iteration
  }
))|%{                  # Feed our sequence as a char-array into a loop
  $z+="$_"             # z is our output number, starts with the first digit
  if(+$z-gt$y){        # If z is bigger than y (initialized to 0)
    ($y=$z)            # Set y equal to z and print it
    $z=""              # Reset z to nothing to start building the next number
  }
}
if($z){                # If there is remaining digits, we need to pad zeroes
  while(+$z-lt$y){     # Until z is bigger than y
    $z+="0"            # Tack on a zero
  }
  $z                   # Print the final number
}

0

Perl 6 , 107 byte

{$_=@=(|@_,*+*...*)[^15].join.comb;.sum??[.shift,{last if !@$_;until (my$a~=.shift//0)>$^b {};$a}...*]!!$_} # 107

Uso:

# give it a lexical name for ease of use
my &code = {...}

# use 「eager」 because the anonymous block returns a lazy array
# and 「say」 doesn't ask it to generate the values
say eager code 2, 1;
# [2 13 47 111 829 4776 12319 93225 218430]
say eager code 1, 1;
# [1 12 35 81 321 345 589 1442 3337 7610]
say eager code 3, 2;
# [3 25 71 219 315 0811 3121 23435 55898 145300]
say eager code 0, 0;
# [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
say eager code 0, 1;
# [0 1 12 35 81 321 345 589 1442 3337 7000]

Spiegazione

crea una sequenza simile a Fibonacci, iniziando con gli argomenti ( @_) slipped ( |) in

|@_,*+*...*

prende i primi 15 elementi di quella sequenza

(…)[^15]

combina quello in una singola stringa ( .join), lo divide in una sequenza di singoli caratteri ( .comb) e lo memorizza nello scalare "predefinito" ( $_) dopo aver forzato la sequenza in un array mutabile, memorizzandolo prima in un array anonimo ( @)

$_=@=(…)[^15].join.comb;

trova la somma dei valori nello scalare predefinito e se quello è zero restituisce lo scalare predefinito, che conterrà una matrice di 15 zeri

.sum??  !!$_

se la somma non è zero, crea un elenco spostando prima il primo elemento nello scalare predefinito

.shift,  

seguito generando il resto dei valori, verificandolo con quello precedente ( $^b)
se lo scalare predefinito si esaurisce, utilizzare 0 invece ( //0)

…,{  ;until (my$a~=.shift//0)>$^b {};$a}...*

interrompersi quando non sono rimasti elementi nello scalare predefinito

…,{last if !@$_;  }...*

perché deve esserci uno spazio until (my$a...? Non (è un delimitatore speciale?
gatto

@cat che sarebbe una chiamata alla subroutine denominata until, che non esiste.
Brad Gilbert b2gills
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.