Progressioni aritmetiche


11

Il tuo compito è analizzare l'input e l'output della formula per l'ennesimo termine se si tratta di una sequenza aritmetica, altrimenti dovrebbe stampare "NAAP".


Ingresso

L'input (da STDIN) sarà composto da pochi numeri, tra 4 e 10 numeri in cui ciascun numero sarà compreso nell'intervallo tra -1000 e 1000 inclusi, separati da un delimitatore (uno spazio o una virgola o un punto e virgola [qualunque sia la tua preferenza]). Ecco alcuni esempi di input.

12,14,16,18       //valid
-3 4 5 1 -2 -4    //valid
45;35;-35         //invalid (only three numbers are present instead of the minimum of 4 numbers)
2,32;21,321       //invalid (it uses two different delimiters: `,` and `;`)

Produzione

Il programma dovrebbe prima verificare se l'ingresso è una progressione aritmetica o meno.

Arithmetic Progressions (AP) in breve: ogni AP avrà una differenza comune. Questa è la differenza tra $ n $ e $ {n-1} $ th termini (sostanzialmente $ a (n + 1) - a (n) $ dove aè la funzione per il sequnce). Questa differenza rimane invariata per qualsiasi valore di $ n $ in un AP. Se non v'è alcuna differenza comune, allora è non una sequenza aritmetica. Per calcolare il valore del n-esimo termine, utilizzare questa formula $ a (n) = a (1) + (n-1) d $ dove $ a (1) $ è il primo termine e $ d $ è il comune differenza.

Se non si tratta di una progressione aritmetica, il programma dovrebbe stampare il messaggio di errore "NAAP" (abbreviazione di "Not An Arithmetic Progression").

Se si tratta di una progressione aritmetica, il programma dovrebbe stampare su STDOUT l'ennesimo termine semplificato della sequenza.

Esempio:

> 1,3,5,7,9
2n-1

Spiegazione: Questo è un AP perché esiste una differenza comune ($ 3 - 1 = 2 $). Quindi usi la formula $ a (n) = a (1) + (n-1) d $

an=a1+(n1)d

an=1+(n1)2

an=1+2n2

an=2n1

Pertanto l'output è 2n-1(notare l'assenza di spazi)


Le scappatoie standard non sono consentite per impostazione predefinita.

È possibile creare una funzione se lo si desidera (con l'array di numeri come parametro). In caso contrario, è necessario creare un programma completo che accetta l'input come una stringa o un array e ne genera l'output.

Casi test:

1.

1,3,5,7,9
2n-1

2.

1 3 12312 7 9
NAAP

3.

-6;8;22;36;50
14n-20

4.

5,1,-3,-7,-11,-15
-4n+9

5.

-5,-7,-9,-11,-13,-15
-2n-3

6.

3,3,3,3,3,3,3,3,3
0n+3

7.

-4,-5,-6,-7
-1n-3

Questo è quindi vince il codice più breve in byte! (scusa per il cattivo matematica-jax)

Eventuali suggerimenti sono benvenuti!


4
Probabilmente dovresti tenere il tuo post nella sandbox per più di un'ora ...
Mego

3
Un'ora è un tempo davvero breve. Non tutti controllano costantemente la sandbox. 24 ore è un buon minimo.
Mego

8
Siamo spiacenti, ma sebbene MathJax funzioni su Meta, non funziona sul sito PPCG principale ...
ETHproductions

1
È necessario aggiungere casi di test con sequenze decrescenti.
lirtosiast,

2
Sono 0,0,0,0e 3,1,-1,-3,-5progressioni aritmetiche? Se è così, penso che sarebbero buoni casi di test, dal momento che hanno rotto un metodo che stavo provando.
xnor

Risposte:


5

Pyth, 30 byte

?tJ{-VtQQ"NAAP"+hJ%"n%+d"-hQhJ

Suite di test

Per verificare se è una processione aritmetica, questo utilizza una sottrazione vectorized tra ciascun elemento e la precedente -VtQQ. Un ternario controlla se ci sono più valori nel risultato ( ?tJ{) e stampa in NAAPtal caso. Quindi, per ottenere il giusto +o -, %+dviene utilizzata la mod-formating .


3

Haskell, 103 byte

z=(tail>>=).zipWith
f l@(a:b:_:_:_)|and$z(==)$z(-)l=show(b-a)++'n':['+'|b-a<=a]++show(a+a-b)
f _="NAAP"

Esempio di utilizzo:

f [-6,8,22,36,50]   ->   "14n-20"
f [60,70,80,90]     ->   "10n+50"
f [2,3,4,6,7,8]     ->   "NAAP"

Come sempre in Haskell, la formattazione di output elaborata (ad es. Miscelazione di numeri con stringhe) consuma molti byte (circa 40). La logica del programma è piuttosto compatta:

f l@(a:b:_:_:_)           -- pattern match an input list with at least 4 elements,
                          -- call the whole list l, the first two elements a and b
z=(tail>>=).zipWith       -- the helper function z takes a function f and a list l
                          -- and applies f element wise to the tail of l and l

           z(-)l          -- make a list of neighbor differences
     z(==)                -- then compare these differences for equality
 and                      -- and see if only True values occur

       show ...           -- if so format output string

f _="NAAP"                -- in all other cases ( < 4 elements or False values)
                          -- return "NAAP"

2

TI-BASIC, 70 byte

Input X
ΔList(∟X->Y
If variance(Ans
Then
∟X(1)-min(Ans
Text(0,0,min(∟Y),"n",sub("+-",(Ans<0)+1,1),abs(Ans
Else
"NAAP

Per ovviare alla mancanza di conversione da numero a stringa di TI-BASIC, questo utilizza l'output sullo schermo del grafico ( Text() se la progressione è aritmetica, che concatena automaticamente gli argomenti insieme.

Ciò presuppone che i numeri negativi vengano immessi usando il carattere meno alto di TI-BASIC (che assomiglia un po 'a ), non il segno meno binario. Tuttavia, l'output utilizza il segno meno binario.


2

Japt , 60 52 51 byte

V=N¤£X-NgY+1};W=Vg;Ve_¥W} ?W+'n+'+sU<W +(U-W :"NAAP

Provalo online!

L'input può essere dato con qualsiasi separatore che ti piace, in questo modo è progettato l'interprete;)

Ungolfed e spiegazione

V=N¤  £    X-NgY+1};W=Vg;Ve_  ¥ W} ?W+'n+'+sU<W +(U-W :"NAAP
V=Ns2 mXYZ{X-NgY+1};W=Vg;VeZ{Z==W} ?W+'n+'+sU<W +(U-W :"NAAP

            // Implicit: N = list of inputs, U = first input
V=Ns2       // Set variable V to N, with the first 2 items sliced off,
mXYZ{       // with each item X and index Y mapped to:
X-NgY+1}    //  X minus the item at index Y+1 in N.
            // This results in a list of the differences (but the first item is NaN).
W=Vg;       // Set W to the first item in V (the multiplication part).
VeZ{Z==W}   // Check if every item in V is equal to W.
?W+'n+      // If true, return W + "n" +
'+sU<W      //  "+".slice(U<W) (this is "+" if U >= W, and "" otherwise)
+(U-W       //  + (U minus W [the addition part]).
:"NAAP      // Otherwise, return "NAAP".
            // Implicit: output last expression

1

Matlab, 103 byte

x=str2num(input('','s'));y=diff(x);if range(y) disp('NAAP'),else fprintf('%gn%+g\n',y(1),x(1)-y(1)),end

1

CJam, 38 byte

{:T2ew::-):U-"NAAP"UW*"n%+d"T0=U+e%+?}

Questa è una funzione anonima che accetta un array nello stack come input e lascia una stringa nello stack come output. Provalo online con un codice I / O aggiuntivo per il test.

Spiegazione:

:T      Save a copy of input in variable T for output generation.
2ew     Generate list of pairs of sequential elements.
::-     Reduce all pairs with subtraction operator.
)       Pop last value from list of differences.
:U      Save difference value in variable U for output generation.
-       Set difference. This will leave an empty list (falsy) if all values are the same.
"NAAP"  First value for ternary operator, for case where not all values are the same.
UW*     Start generating output for success case. Need to flip sign of difference saved
        in variable U, since it was 1st value minus 2nd, and we need the opposite.
"n%+d"  Push format string for printf operator. The + sign in the format specifies that
        the sign is always generated, saving us from needing different cases for the
        value being negative or positive.
T0=     Extract first value from original input saved in variable T.
U+      Add the difference (with the "wrong" sign) to it.
e%      "printf" operator.
+       Concatenate two parts of result.
?       Ternary operator for picking one of the two output cases.

1

JavaScript (ES6), 91 byte

x=>(s=x.split`,`,m=s[1]-s[0],a=s[0]-m,s.some((n,i)=>n!=m*i+m+a)?"NAAP":m+"n"+(a<0?a:"+"+a))

Spiegazione

x=>(
  s=x.split`,`,       // s = array of input numbers
  m=s[1]-s[0],        // m = the multiplication part of the formula
  a=s[0]-m,           // a = the addition part of the formula
  s.some((n,i)=>      // check if the rest of the numbers follow this sequence
    n!=m*i+m+a
  )?"NAAP":
  m+"n"+(a<0?a:"+"+a) // output the formula
)

Test

<input type="text" id="input" value="5,1,-3,-7,-11,-15" /><button onclick='result.innerHTML=(

x=>(s=x.split`,`,m=s[1]-s[0],a=s[0]-m,s.some((n,i)=>n!=m*i+m+a)?"NAAP":m+"n"+(a<0?a:"+"+a))

)(input.value)'>Go</button><pre id="result"></pre>


1

Perl 6, 123 102 101 byte

EDIT: non negare la differenza

EDIT: usa sub anonimi, operatori logici e interpolazione di stringhe. Grazie Brad Gilbert b2gills

sub{my@b=@_.rotor(2=>-1).map({[-] $_}).squish;$_=@_[0]+@b[0];@b.end&&"NAAP"||"@b[0]n{'+'x($_>=0)}$_"}

Programma test (legge da stdin):

my $f = <the code above>
$f(split(/<[;,]>/, slurp)).say

Spiegazione:

my @b =
  @_.rotor(2=>-1)  # sliding window of 2: (1,2,3,4) => ((1,2),(2,3),(3,4))
  .map({[-] $_})  # calculate difference (subtract all elements and negate)
  .squish;         # remove adjacent elements that are equal

@b.end        # @b.end is last index, @b.end = 0 means @b has only 1 element
&& "NAAP"     # true branch
|| "@b[0]n{'+'x($_>=0)}$_" # string for an+b, 
        # {'+'x($_>=0)} inserts a plus sign using the repetition operator x

In genere si utilizza uno dei moduli di espressione lambda in modo da poter rimuovere il file sub f. Inoltre, se utilizzato al @_posto di @ate, salverebbe diversi byte. {my@b=@_.rotor.... Inoltre non si dovrebbe mettere parentesi intorno alla condizione di un ifcomunicato, non si tratta di Perl 5. Se è stata modificata che ifper @b.end&&"NAAP"||$_=...voi sarebbe risparmiare un paio di byte. Puoi anche eliminare l'ultima ifistruzione se hai usato "@b[0]n{'+'x($_>=0)}$_"invece, salvando 4 byte.
Brad Gilbert b2gills

Non è necessario suball'inizio, senza che diventa un blocco anonimo. Anche solo per questo sai, non avrei pensato di usarlo .map({[-] $_}), probabilmente avrei usato quello ».map(*-*).flatche è più lungo, ora devo passare attraverso le mie voci per vedere se posso accorciarlo con il tuo trucco.
Brad Gilbert b2gills

1

Rubino, 95 78 76 byte

->s{i,j=s;puts s.reduce(:+)==s.size*(s[-1]+i)/2?"%dn%+d"%[v=j-i,i-v]:"NAAP"}

78 byte

->s{puts s.reduce(:+)==s.size*(s[-1]+i=s[0])/2?"%dn%+d"%[v=s[1]-i,i-v]:"NAAP"}

95 byte

->s{puts s.reduce(:+)==s.size*(s[0]+s[-1])/2?"#{v=s[1]-s[0]}n#{"+"if (i=s[0]-v)>0}#{i}":"NAAP"}

Ungolfed:

-> s {
  i,j=s
  puts s.reduce(:+)==s.size*(s[-1]+i)/2?"%dn%+d"%[v=j-i,i-v]:"NAAP"
}

Uso:

->s{i,j=s;puts s.reduce(:+)==s.size*(s[-1]+i)/2?"%dn%+d"%[v=j-i,i-v]:"NAAP"}[[-6,8,22,36,50]]

=> 14n-20

0

Python 3, 92 byte
Basato sulla mia risposta da Trova la sequenza

def m(t):s=str;a=t[0];b=t[1];q=b-a;return((s(q)+'n'+'+'*(0<a-q)+s(a-q)),'NAAP')[t[2]/b==b/a]

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.