Arithmetic Progressions
Pyth, 30 bytes
?tJ{-VtQQ"NAAP"+hJ%"n%+d"-hQhJ
Test suite
To check whether it's a arithmetic procession, this uses a vectorized subtraction between each element and the previous, -VtQQ
. A ternary checks if there are multiple values in the result (?tJ{
) and prints NAAP
if so. Then, to get the +
or -
right, the mod-formating %+d
is used.
Haskell, 103 bytes
z=(tail>>=).zipWith
f l@(a:b:_:_:_)|and$z(==)$z(-)l=show(b-a)++'n':['+'|b-a<=a]++show(a+a-b)
f _="NAAP"
Usage example:
f [-6,8,22,36,50] -> "14n-20"
f [60,70,80,90] -> "10n+50"
f [2,3,4,6,7,8] -> "NAAP"
As always in Haskell, fancy output formatting (e.g. mixing numbers with strings) eats a lot of bytes (around 40). The program logic is quite compact:
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"
Japt, 60 52 51 bytes
V=N¤£X-NgY+1};W=Vg;Ve_¥W} ?W+'n+'+sU<W +(U-W :"NAAP
Try it online!
Input can be given with whichever separator you like, as that's how the interpreter is designed ;)
Ungolfed and explanation
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