Am I an insignificant array?

40

5

An insignificant array is an array of positive integers, where the absolute differences between consecutive elements are all smaller than or equal to 1.

For example, the following array is insignificant:

[1, 2, 3, 4, 3, 4, 5, 5, 5, 4]

Because the corresponding (absolute) differences are:

[1, 1, 1, 1, 1, 1, 0, 0, 1]

Which are all smaller than or equal to 1.


Your task is to determine whether a given array of integers is insignificant.

  • You may assume that the array always contains at least two elements.
  • Standard input and output rules apply. You may take input (and output) in any reasonable format.
  • Default Loopholes are forbidden.
  • The truthy / falsy values have to be distinct and consistent.
  • This is , so shortest answer in bytes wins.

Test cases

Input -> Output

[1, 2, 3, 4, 3, 4, 5, 5, 5, 4] -> true
[1, 2, 3, 4, 5, 6, 7, 8, 9, 8] -> true
[3, 3, 3, 3, 3, 3, 3]          -> true
[3, 4, 4, 4, 3, 3, 3, 4, 4, 4] -> true
[1, 2, 3, 4]                   -> true 
[5, 4, 3, 2]                   -> true 
[1, 3, 5, 7, 9, 7, 5, 3, 1]    -> false
[1, 1, 1, 2, 3, 4, 5, 6, 19]   -> false
[3, 4, 5, 6, 7, 8, 7, 5]       -> false
[1, 2, 4, 10, 18, 10, 100]     -> false
[10, 20, 30, 30, 30]           -> false

I used the values true and false.

user70974

Posted 2017-09-20T15:37:14.223

Reputation:

Do the truthy/falsy values actually have to be truthy/falsy in our language of choice, or can we use any two distinct and consistent values? – Martin Ender – 2017-09-20T17:36:01.560

1@MartinEnder Any two distinct and consistent values. P.S Sorry for the late response – None – 2017-09-20T18:04:39.040

2The text says you'll be given an array of integers, but that only arrays of positive integers can be insignificant. Should we be prepared for an array of negative integers? – Mark S. – 2017-09-23T12:50:33.093

Answers

24

Jelly, 3 bytes

IỊẠ

Try it online!

How?

Just the perfect challenge for Jelly.

IỊẠ   Full program.

I     Increments; Get the difference between consecutive elements.
 Ị    Insignificant; return abs(number) ≤ 1.
  Ạ   All; returns 1 if all the elements are truthy, 0 otherwise.

Mr. Xcoder

Posted 2017-09-20T15:37:14.223

Reputation: 39 774

2P wouldn't work would it, because if all the differences were 1 it'd output 1, but if one of them was 0 it would output 0? And if one difference was 5 but one was 0 it would still do 0? – Tas – 2017-09-22T03:59:20.313

1What about the "positive integers" requirement? – 3D1T0R – 2017-09-23T03:06:20.960

19

JavaScript (ES7), 33 29 bytes

Saved 4 bytes thanks to @JohanKarlsson

a=>!a.some(v=>(a-(a=v))**2>1)

How?

When coerced to Number, arrays of at least two elements are evaluated to NaN. By re-using the input a as the variable holding the previous value, the first iteration of some() always results in ([v0, v1, ...] - a[0]) ** 2 = NaN, no matter the value of a[0]. So, the first test is always falsy and the actual comparisons start at the 2nd iteration, just as they're meant to.

Test cases

let f =

(a,p)=>!a.some(v=>(p-(p=v))**2>1)

console.log(f([1, 2, 3, 4, 3, 4, 5, 5, 5, 4])) // true
console.log(f([1, 2, 3, 4, 5, 6, 7, 8, 9, 8])) // true
console.log(f([3, 3, 3, 3, 3, 3, 3]         )) // true
console.log(f([3, 4, 4, 4, 3, 3, 3, 4, 4, 4])) // true
console.log(f([1, 2, 3, 4]                  )) // true
console.log(f([5, 4, 3, 2]                  )) // true
console.log(f([1, 3, 5, 7, 9, 7, 5, 3, 1]   )) // false
console.log(f([1, 1, 1, 2, 3, 4, 5, 6, 19]  )) // false
console.log(f([3, 4, 5, 6, 7, 8, 7, 5]      )) // false
console.log(f([1, 2, 4, 10, 18, 10, 100]    )) // false
console.log(f([10, 20, 30, 30, 30]          )) // false

Arnauld

Posted 2017-09-20T15:37:14.223

Reputation: 111 334

29 bytes: a=>!a.some(v=>(a-(a=v))**2>1) – Johan Karlsson – 2017-09-22T08:45:40.523

@JohanKarlsson Ah yes, the input is guaranteed to contain at least 2 elements, so that's safe. Thanks a lot! – Arnauld – 2017-09-22T11:47:16.040

11

Python 3, 40 bytes

f=lambda n,*t:t==()or-2<n-t[0]<=1==f(*t)

Try it online!

Dennis

Posted 2017-09-20T15:37:14.223

Reputation: 196 637

7

Mathematica, 24 bytes

Max@Abs@Differences@#<2&

Martin Ender

Posted 2017-09-20T15:37:14.223

Reputation: 184 808

#==Clip@#&@*Differences is one byte shorter. – Misha Lavrov – 2017-11-03T23:57:25.763

7

Python 2, 35 bytes

x=input()
while-2<x.pop(0)-x[0]<2:1

Exists with status code 1 for insignificant arrays, with status code 0 otherwise.

Try it online!

Dennis

Posted 2017-09-20T15:37:14.223

Reputation: 196 637

6

Husk, 4 bytes

ΛεẊ-

Try it online!

Explanation:

ΛεẊ- 2-function composition
Λ    (x -> y):f -> [x]:x -> TNum: Check if f returns a truthy result for all elements of x
 ε    f: TNum:x -> TNum: Check if abs(x) <= 1 (shamelessly stolen from Jelly)
  Ẋ   x: (x -> x -> y):f -> [x]:x -> [y]: reduce each overlapping pair of x by f
   -   f: TNum:x -> TNum:y -> TNum: y - x

Erik the Outgolfer

Posted 2017-09-20T15:37:14.223

Reputation: 38 134

6

Haskell, 34 33 bytes

all((<2).abs).(zipWith(-)=<<tail)

Try it online! -1 byte thanks to @user1472751


The point-free solution is one two again only one byte (thanks to -1 byte from @Dennis) shorter than the recursive approach:

f(a:b:r)=2>(a-b)^2&&f(b:r)
f _=1>0

Try it online!

Laikoni

Posted 2017-09-20T15:37:14.223

Reputation: 23 676

6

Octave, 21 bytes

@(x)all(diff(x).^2<2)

Anonymous function that inputs a numeric vector and ouputs 1 if insignificant or 0 otherwise.

Try it online!

Luis Mendo

Posted 2017-09-20T15:37:14.223

Reputation: 87 464

5

Perl 6, 25 bytes

{?(2>all(.[]Z-.skip)>-2)}

Try it online!

This should be pretty readable. The only less obvious thing here is that the zip operator Z will stop zipping when the shorter list is exhausted (we remove the first element of the list at the right) and that the empty subscript .[], so called Zen slice, gives the whole list. .skip returns the list without the first element.

Ramillies

Posted 2017-09-20T15:37:14.223

Reputation: 1 923

Are those two spaces really necessary? – Jonathan Frech – 2017-09-20T15:52:30.510

@JonathanFrech: The right one probably no. Also I just realized that the .rotate is not needed here. – Ramillies – 2017-09-20T15:53:18.847

Heck, even the left one could be removed. I really don't understand where the whitespace is required and where it is not... – Ramillies – 2017-09-20T15:54:57.273

You could write -2< instead of -1≤ and <2 instead of ≤1 to save four more bytes. – Sean – 2017-09-20T17:18:55.357

Er, I guess you actually have to reverse the comparisons 2>...>-2 to avoid interpreting the < in an erroneous way. – Sean – 2017-09-20T17:21:27.960

@Sean, thanks. I missed the fact that the array will contain only positive integers. – Ramillies – 2017-09-20T17:38:39.963

I recently discovered the following trick: .[]Z-.skip saves 2 bytes. – nwellnhof – 2017-09-20T20:03:28.637

@nwellnhof — wow! I mean: I was searching for a method that would do this, and didn't find it. (Apparently the docs need to get even better than they are now.) Many thanks! – Ramillies – 2017-09-20T21:08:25.177

While it doesn't change the length, you could use so … instead of ?(…) – Brad Gilbert b2gills – 2017-09-21T00:03:08.430

@BradGilbertb2gills: That would be probably nicer visually, but that's it. I just tend to use ? everywhere because (I think) it's at worst the same length as so. – Ramillies – 2017-09-21T09:53:42.113

5

Pyth, 6 bytes

._MI.+

Verify all the test cases.


Pyth, 8 bytes

.A<R2aVt

Try it online!

Explanation

._MI.+   Full program.

    .+   Deltas.
   I     Is invariant under...
._M      Mapping with Sign. 0 if n == 0, -1 if n < 0, 1 if n > 0.

.A<R2aVt    Full program.

      Vt    Vectorize function, applied on the input zipped with the tail of the input.
     a      Absolute difference.
  <R2       For each, check if it is smaller than 2.
.A          All.

Mr. Xcoder

Posted 2017-09-20T15:37:14.223

Reputation: 39 774

I have no idea why I thought I# instead of M. – Steven H. – 2017-09-20T18:48:35.817

5

Japt, 6 bytes

äa e<2

Try it online!

Explanation

ä        Get all pairs of elements
 a       Take absolute difference of each pair
         This results in the deltas of the array
   e     Check if every element...
    <2   Is less than 2

Justin Mariner

Posted 2017-09-20T15:37:14.223

Reputation: 4 746

5

Proton, 41 bytes

a=>all(-2<a[i]-a[i+1]<2for i:0..len(a)-1)

Try it online!

-16 bytes thanks to Mr. Xcoder
-2 bytes
-6 bytes thanks to Mr. Xcoder

HyperNeutrino

Posted 2017-09-20T15:37:14.223

Reputation: 26 575

49 bytes (-16 bytes) – Mr. Xcoder – 2017-09-20T16:06:58.630

@Mr.Xcoder I think the space in <2 for might be omittable. – Jonathan Frech – 2017-09-20T16:08:25.630

@JonathanFrech There is no space there? – Mr. Xcoder – 2017-09-20T16:08:42.217

@Mr.Xcoder oh yeah I don't know what I was thinking with all those crazy function chainings. Thanks! :D – HyperNeutrino – 2017-09-20T16:24:50.233

5

C# (.NET Core), 51 45 44 + 18 bytes

-1 byte thanks to Jeppe Stig Nielsen

a=>a.Zip(a.Skip(1),(x,y)=>x-y).All(x=>x*x<4)

Byte count also includes:

using System.Linq;

Try it online!

Explanation:

a =>                      // Take an array of integers as input
    a.Zip(                // Combine each element with corresponding one from:
        a.Skip(1),        //     the input array without first element
        (x, y) => x - y   //     get their difference
    )
    .All(x => x * x < 4)  // Check if all differences are less than 2
                          // (We only care about 0 and 1, and so happens that when they're squared, it works like Abs! Magic!)

Grzegorz Puławski

Posted 2017-09-20T15:37:14.223

Reputation: 781

3Little improvement: a=>a.Zip(a.Skip(1),(x,y)=>x-y).All(x=>x*x<4), it avoids the negation !. – Jeppe Stig Nielsen – 2017-09-20T19:42:44.793

@JeppeStigNielsen awesome, thank you! – Grzegorz Puławski – 2017-09-20T19:48:44.890

5

R, 30 26 bytes

cat(all(diff(scan())^2<2))

Try it online!

user2390246

Posted 2017-09-20T15:37:14.223

Reputation: 1 391

3I think function(a)all(diff(a)^2<2) saves 3 bytes. – BLT – 2017-09-20T18:34:54.107

you could take the input from the console: all(diff(scan())^2<2) – flodel – 2017-09-21T01:51:24.410

@BLT good point! – user2390246 – 2017-09-21T08:05:51.320

1

@flodel There was some recent discussion regarding the need to explicitly print the output when using scan, but it still saves a byte!

– user2390246 – 2017-09-21T08:11:08.283

4

05AB1E, 5 bytes

¥Ä2‹P

Try it online!

Explanation

¥        # calculate deltas
 Ä       # absolute values
  2‹     # smaller than 2
    P    # product

Emigna

Posted 2017-09-20T15:37:14.223

Reputation: 50 798

@Okx: I'm afraid not. It won't work for [5,2] for example. – Emigna – 2017-09-20T17:28:37.577

4

Ohm v2, 4 bytes

Δ2<Å

Try it online!

How?

Δ2<Å   ~ Full program.

Δ      ~ Absolute deltas.
 2<    ~ Is smaller than 2?
   Å   ~ Check if all elements are truthy.

Mr. Xcoder

Posted 2017-09-20T15:37:14.223

Reputation: 39 774

3

PowerShell, 62 bytes

param($a)$l=$a[0];($a|?{$_-$l-in1..-1;$l=$_}).count-eq$a.count

Try it online!

PowerShell doesn't have a .map or .some or similar command, so here we're individually checking each delta.

We take input $a and set $l equal to the first element. Then we loop through $a and take out each element where |?{...} the difference $_-$l is -in the range 1,0,-1. We then set $l equal to the current element. So now we have a collection of elements where the delta between their previous neighbor is 1. We take the .count of that and check whether it's -equal to the .count of the array as a whole. If it is, then every delta is 1 or less, so it's an insignificant array. That Boolean result is left on the pipeline, and output is implicit.

AdmBorkBork

Posted 2017-09-20T15:37:14.223

Reputation: 41 581

You can save 1 byte by getting rid of the param and doing $l=($a=$args)[0] – briantist – 2017-09-21T20:49:47.590

@briantist That doesn't work, though. For example. This is because it's setting $l to be the whole input array in your suggestion.

– AdmBorkBork – 2017-09-21T20:54:26.670

I think it just requires changing the way you give arguments in TIO (each element needs to be specified separately). The way you have it now, the first element of $args is itself the whole array. Example

– briantist – 2017-09-21T20:57:35.923

That feels cheaty... – AdmBorkBork – 2017-09-21T21:06:28.020

I think that's actually the correct way to use $args. If you called a script or function with a series of arguments separated as spaces, it would come in as separate elements in $args, and for TIO that's how to emulate that. I've personally used it that way many times before, but to each their own :) – briantist – 2017-09-21T21:10:00.653

2

Python 3, 45 bytes

lambda k:all(-2<x-y<2for x,y in zip(k,k[1:]))

Try it online! or Try the test suite.

Thanks to Jonathan Frech for -2 bytes.

Mr. Xcoder

Posted 2017-09-20T15:37:14.223

Reputation: 39 774

abs(x-y)<2 -> -2<x-y<2. – Jonathan Frech – 2017-09-20T15:46:41.500

2

Java (OpenJDK 8), 78 bytes

a->{int p=0,i=1;for(;i<a.length;p+=Math.abs(a[i]-a[i++-1])>1?1:0);return p<1;}

Try it online!

Roberto Graham

Posted 2017-09-20T15:37:14.223

Reputation: 1 305

2

MATL, 6 5 bytes

d|2<A

-1 byte thanks to Giuseppe

Try it online! or Verify all test-cases

Cinaski

Posted 2017-09-20T15:37:14.223

Reputation: 1 588

I think per meta consensus you can use d|2< instead, as an array with a zero value is falsey in MATL.

– Giuseppe – 2017-09-20T15:56:57.663

1Or d|2<A for something closer to your original answer. – Giuseppe – 2017-09-20T15:59:01.413

1@Giuseppe No they can't: *The truthy / falsy values have to be distinct and consistent.* – Mr. Xcoder – 2017-09-20T15:59:03.277

@Mr.Xcoder "an array of all 1s for truthy" and "an array containing at least one zero for falsey" isn't distinct and consistent? – Giuseppe – 2017-09-20T16:01:38.697

@Giuseppe They are distinct, but inconsistent. – Mr. Xcoder – 2017-09-20T16:02:23.637

@Giuseppe I'll go for d|2<A, thanks! – Cinaski – 2017-09-20T16:06:26.577

2@Giuseppe "an array of all 1s for truthy" and "an array containing at least one zero for falsey" isn't distinct and consistent? - No, that is not acceptable, because they are inconsistent. – None – 2017-09-20T16:13:44.533

2

C, 61 56 bytes

Thanks to @scottinet for saving five bytes!

r;f(a,n)int*a;{for(r=1;--n;r=(*a-*++a)/2?0:r);return r;}

Try it online!

C (gcc), 47 bytes

r;f(a,n)int*a;{for(r=1;--n;r=(*a-*++a)/2?0:r);}

Try it online!

Steadybox

Posted 2017-09-20T15:37:14.223

Reputation: 15 798

And if it is allowed / if you feel like it, you may save 9 more bytes by storing the result in r instead of returning it. :-)

– scottinet – 2017-09-20T20:05:15.783

@scottinet I considered that, but it's not valid C even though it happens to work with gcc. It's allowed, though, so I guess I'll just include it as an alternate version. – Steadybox – 2017-09-20T20:08:57.047

I'm puzzled as to why using the global r to store the result and printing it in main is not valid C? – scottinet – 2017-09-20T20:16:49.773

2@scottinet Assigning a variable at the end of a function puts that value in the function's return adress, making it feel like it is returning the value. However, this behaviour is not part of the C specifications, thereby not guarenteed to work. It can also break with certain optimizing compiler flags. – Jonathan Frech – 2017-09-20T20:25:55.990

Indeed, but that is not what I did in my proposal. I also modified the main function to make it print r (a global variable) explicitly instead of a returned value that do not exist anymore. That's why I wondered if that was allowed by rules. – scottinet – 2017-09-20T20:28:48.857

2@scottinet Ah, I am sorry. I think that would not be allowed as you cannot simply assign variables in your solution per agreed upon rule. As an example, using globally defined variables instead of function arguments would not be allowed either. Your task is to write a fully functional program / function. – Jonathan Frech – 2017-09-20T20:33:10.533

I am not sure, but is this valid?

– Jonathan Frech – 2017-09-20T20:45:16.650

1@JonathanFrech languages are defined by their implementation here, so if you have a compiler which produces consistent results then the answer is valid, even if formally UB. – Quentin – 2017-09-21T11:19:10.867

2

anyfix, 9 bytes

I€A€2<»/&

Try it online!

I€A€2<»/&  Main Link
I          Deltas
 €         For each element
  A        Take its absolute value
   €  »    For each element
    2<     Is it less than two?
       /   Reduce over
        &  Logical AND

This is mostly a port of the 05AB1E solution except terrible because anyfix doesn't have autovectorization and other cool things

HyperNeutrino

Posted 2017-09-20T15:37:14.223

Reputation: 26 575

2

TI-Basic, 6 7 bytes

prod(2>abs(ΔList(Ans

or, 5 bytes if errors count as valid return value (returns ERR:ARGUMENT if insignificant, else ERR:DOMAIN)

augment(sin⁻¹(ΔList(Ans

Oki

Posted 2017-09-20T15:37:14.223

Reputation: 311

1This should probably have abs(ΔList(Ans, or else drops by more than 1 (such as in {5,3,1} or in the test case {3,4,5,6,7,8,7,5}) don't get detected. – Misha Lavrov – 2017-09-24T01:19:55.210

@MishaLavrov thanks, you're right! – Oki – 2017-09-24T20:45:07.350

2

Clojure, 35 bytes

#(every? #{-1 0 1}(map -(rest %)%))

How neat is that?

NikoNyrh

Posted 2017-09-20T15:37:14.223

Reputation: 2 361

1

JavaScript (ES6), 37 36 bytes

(a,u)=>!a.some(e=>(e-=(u=e))>1|e<-1)

Edit: Saved 1 byte by stealing @Arnauld's trick.

Neil

Posted 2017-09-20T15:37:14.223

Reputation: 95 035

You could use currying: a=>u=>!a.some(e=>(e-=(u=e))>1|e<-1) – Bálint – 2017-09-24T10:30:50.017

1

Pyth, 7 bytes

._I#I.+

Test Suite

Returns true/false.

Explanation:

     .+ Deltas, returns differences between consecutive values.
._      Signum, returns the sign of a number (1, 0, or -1).  Note that this should
             be equal to the input for insignificant arrays.
  I     Tests if it is equal to the input...
   #    For each in the input, and filter out those that aren't...
    I   And make sure none have been filtered out.

Steven H.

Posted 2017-09-20T15:37:14.223

Reputation: 2 841

1

Java (OpenJDK 8), 60 bytes

a->{int r=1,p=a[0];for(int i:a)r|=(r=p-(p=i))*r;return r<2;}

Try it online!

  • 5 bytes thanks to @Nevay!

Olivier Grégoire

Posted 2017-09-20T15:37:14.223

Reputation: 10 647

1You can use r in the loop to calculate (p-n) only once, >>1 can be /2, or removed if you use | instead of +: a->{int r=1,p=a[0];for(int i:a)r|=(r=p-(p=i))*r;return r<2;} (60 bytes). – Nevay – 2017-09-20T20:12:22.830

Cheers @Nevay, thank you! Perfect golfing, as usual ;-) – Olivier Grégoire – 2017-09-20T20:29:53.920

can you explain me how does it work? thank you! – blurstream – 2017-09-22T09:01:02.850

1

Ruby, 36 35 33 bytes

->b,*a{a.all?{|x|(b-(b=x))**2<2}}

Takes the input array as individual arguments.

-1 byte thanks to MegaTom, -2 bytes thanks to Jordan.

Try it online!

Snack

Posted 2017-09-20T15:37:14.223

Reputation: 251

2->a{b,=a;a.all?{|x|(b-(b=x))**2<2}} is 35 bytes. – MegaTom – 2017-09-21T18:55:31.483

@MegaTom right you are! – Snack – 2017-09-21T18:59:44.723

1->b,*a{a.all?{|x|(b-(b=x))**2<2}} is 33 bytes. – Jordan – 2017-11-01T20:27:01.157

@Jordan Not sure if that works - how are you passing the array into it? Passing the array like in the TIO just gives true for all test cases. – Snack – 2017-11-01T22:29:27.737

No array, just pass the input as individual arguments. – Jordan – 2017-11-01T22:35:49.813

@Jordan nice, didn't know about that one. Thanks! – Snack – 2017-11-03T15:09:14.053

1

Mathematica, 34 bytes

Differences@#~MatchQ~{(1|0|-1)..}&

Explanation

                                 & (* Function *)
Differences                        (* which takes the consecutive differences*)
           @#                      (* of the input list *)
             ~MatchQ~              (* and returns whether it matches *)
                     {(1|0|-1)..}  (* a list consisting of one or more 1s, 0s, or -1s *)

ngenisis

Posted 2017-09-20T15:37:14.223

Reputation: 4 600

1

Swift 4, 52 bytes

{!zip($0.dropFirst(),$0).map(-).contains{1<abs($0)}}

Test suite:

let isInsignificant: (_ array: [Int]) -> Bool = {!zip($0.dropFirst(),$0).map(-).contains{1<abs($0)}}

let testcases: [(input: [Int], expected: Bool)] = [
    (input: [1, 2, 3, 4, 3, 4, 5, 5, 5, 4], expected: true),
    (input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 8], expected: true),
    (input: [3, 3, 3, 3, 3, 3, 3],          expected: true),
    (input: [3, 4, 4, 4, 3, 3, 3, 4, 4, 4], expected: true),
    (input: [1, 2, 3, 4],                   expected: true ),
    (input: [5, 4, 3, 2],                   expected: true ),
    (input: [1, 3, 5, 7, 9, 7, 5, 3, 1],    expected: false),
    (input: [1, 1, 1, 2, 3, 4, 5, 6, 19],   expected: false),
    (input: [3, 4, 5, 6, 7, 8, 7, 5],       expected: false),
    (input: [1, 2, 4, 10, 18, 10, 100],     expected: false),
    (input: [10, 20, 30, 30, 30],           expected: false),
]


for (caseNumber, testcase) in testcases.enumerated() {
    let actual = isInsignificant(testcase.input)
    assert(actual == testcase.expected,
        "Testcase #\(caseNumber) \(testcase.input) failed. Got \(actual), but expected \(testcase.expected)!")
    print("Testcase #\(caseNumber) passed!")
}

Alexander - Reinstate Monica

Posted 2017-09-20T15:37:14.223

Reputation: 481

1

APL, 13 bytes

{×/(|2-/⍵)<2}

First APL answer \o/

Note: I am a bot owned by Hyper Neutrino. I exist mainly for chat testing.

Explanation

{×/(|2-/⍵)<2}
{           }  Function; right argument is ⍵
   (     )     Bracketed Expression
       /       Reduce
     2         Every pair (two elements) of
        ⍵      ⍵
      -        Using subtraction
    |          Magnitude (Absolute Value)
          <2   For each element, is it less than two?
  /            Reduce over
 ×             Multiplication (Product) (All)

NeutrinoBot

Posted 2017-09-20T15:37:14.223

Reputation: 59

111 bytes as tacit - ∧/2>(|2-/⊢) – Uriel – 2017-10-30T21:52:57.290

1

Jq 1.5, 42 41 bytes

all(keys[1:][]as$i|.[$i]-.[$i-1]|.*.;.<2)

Explained

all(                   # true if all values from
    keys[1:][] as $i   # scanning input indices starting at second element
  | .[$i]-.[$i-1]      # calculate differences between successive elements
  | .*.                # square the difference
; . < 2                # are < 2
)

Sample Run

$ jq -Mc 'all(keys[1:][]as$i|.[$i]-.[$i-1]|.*.;.<2)' data.json
true
true
true
true
true
true
false
false
false
false
false

$ echo -n 'all(keys[1:][]as$i|.[$i]-.[$i-1]|.*.;.<2)' | wc -c
  41

jq170727

Posted 2017-09-20T15:37:14.223

Reputation: 411

1

Excel VBA, 55 Bytes

Anonymous VBE immediate window function that takes input from range [1:1] and outputs to the VBE immediate window

[2:2]="=IfError(If(B1=0,0,ABS(B1-A1)),0)":?[Max(2:2)>1]

Taylor Scott

Posted 2017-09-20T15:37:14.223

Reputation: 6 709

1

J, 20 19 bytes

i=.[:*/2>[:|2&(-/\)

I defined it as a verb, which adds 3 bytes to the total length.

Explanation

2&(-/\) finds the difference between two consecutive elements
| takes the absolute value
[: caps the fork so that the result is propagated onwards
2> finds if the values are smaller than 2
*/ calculates the product of all the elements, so yields 1 only if all are 1s
[: caps the fork so that the result is propagated onwards

Example:

   i 1 3 5 7 9 7 5 3 1  
0

Try it online

Galen Ivanov

Posted 2017-09-20T15:37:14.223

Reputation: 13 815

[:*/2>2|@-/\] should work for 6? fewer bytes. – cole – 2017-11-01T04:27:06.610

@cole Thanks, this is much better than mine! – Galen Ivanov – 2017-11-01T08:49:54.047

0

Python 3, 54 bytes

2 bytes thanks to Jonathan Frech.

lambda a:all(-2<a[i]-a[i+1]<2for i in range(len(a)-1))

Try it online!

Leaky Nun

Posted 2017-09-20T15:37:14.223

Reputation: 45 011

abs(a[i]-a[i+1])<2 -> -2<a[i]-a[-~i]<2. – Jonathan Frech – 2017-09-20T15:47:33.290

0

Perl 5, 38 + 2 (-ap) = 40 bytes

$_&&=abs$i-$F[-1]<2while($i=pop@F)&&@F

Try it online!

Xcali

Posted 2017-09-20T15:37:14.223

Reputation: 7 671

I don't think -p is needed to be a +1. Does it? – Stan Strum – 2017-09-20T20:12:14.710

0

Clojure, 36 bytes

#(every? #{-1 0 1}(map - %(rest %)))

Try it online!

MattPutnam

Posted 2017-09-20T15:37:14.223

Reputation: 521

0

8th, 66 49 55 bytes

Code

1 >r ( n:- abs dup 1 > if rdrop 0 >r then ) a:y drop r>

SED (Stack Effect Diagram) is: a -- f

Explanation

This code leaves 1 on TOS if array is insignificant, otherwise it leaves 0. Here the ungolfed version:

1 >r                                           \ suppose array is insignificant
( n:- n:abs dup 1 n:> if rdrop 0 >r then ) a:y \ compute corresponding (absolute) differences and check if difference is > 1
drop r>                                        \ clean stack and put result on TOS

We could immediately exit from loop with break when array is significant, but in this case we waste 6 bytes.

Chaos Manor

Posted 2017-09-20T15:37:14.223

Reputation: 521

0

RProgN 2, 8 bytes

{-â2<}á*

Explained

{-â2<}á*
{    }á     # Fold the input by the function
 -          # Subtract, get the difference.
  â         # Absolute
   2<       # Is less than 2.
       *    # Get the product, 1 for truthy, 0 for falsey.

Try it online!

ATaco

Posted 2017-09-20T15:37:14.223

Reputation: 7 898

0

Common Lisp, 53 bytes

(lambda(a)(every(lambda(x y)(< -2(- x y)2))a(cdr a)))

Try it online!

Renzo

Posted 2017-09-20T15:37:14.223

Reputation: 2 260

0

Python 3, 49 bytes

lambda l:all(abs(j-i)<=1 for i,j in zip(l,l[1:]))

ospahiu

Posted 2017-09-20T15:37:14.223

Reputation: 231

Unfortunately, this doesn't work for arrays like [1, 1, 3]. – Dennis – 2017-09-21T17:07:09.320

1Great catch on that edge case!! Updated with another answer. @Dennis – ospahiu – 2017-09-21T18:47:21.980

0

Pyth, 15 12 bytes

Works pretty simply. T or 10 as a boolean is True

V.+Q=&T<N2)T

Explanation:

V.+Q        In the difference map of the input...
    =&T<N2) T = T && the current item is less than 2, and close the loop
T           Print J

Test Suite

Stan Strum

Posted 2017-09-20T15:37:14.223

Reputation: 436

0

Scheme - 89 bytes

(define (f n) (if (not (empty? n)) (and (<= (abs (- (cadr n) (car n))) 1) (f (cdr n))) 1)

GenRincewind

Posted 2017-09-20T15:37:14.223

Reputation: 1

0

Casio-Basic, 28 bytes

judge(max(abs(⊿list(n)))<2

It's nice that the fx-CP400 has a built-in for cumulative differences. Not so nice that you need to use judge to force it to check whether the resulting value is less than 2.

27 bytes for the function ( is two bytes), +1 to enter n in the argument list.

numbermaniac

Posted 2017-09-20T15:37:14.223

Reputation: 639

If Jelly uses Unicode characters and just count 1 byte, why wouldn't also count just 1 byte? – LS_ᴅᴇᴠ – 2017-10-31T09:11:48.490

@LS_ᴅᴇᴠ If by "Unicode" you mean UTF-8, most Jelly programs don't use UTF-8.

– Jordan – 2017-11-01T20:30:24.657

@LS_ᴅᴇᴠ Jelly uses its own code page, as linked in the comment above, which means that every single character used in the language counts as one byte, even if it's two in Unicode. On the Casio ClassPad, that character has a character code above 256, which makes it a two-byte character. – numbermaniac – 2017-11-02T01:07:25.347

@Jordan Yes, you are right, I read something more about Jelly and get it! – LS_ᴅᴇᴠ – 2017-11-02T07:26:40.320

0

Pushy, 13 bytes

$2d-|v.;Og2<#

Try it online!

                 \ Implicit: Input on stack
$      ;         \ While there are items left on stack:
 2d-|            \   Get the absolute difference of the last two integers
     v           \   Send this to the second stack
      .          \   Pop the last item 

        O        \ Go to the second stack (list of differences)
         g       \ Sort ascendingly (largest item last)
          2<#    \ Check if it is smaller than 2 (print 1/0 accordingly)

FlipTack

Posted 2017-09-20T15:37:14.223

Reputation: 13 242

0

PHP, 85 bytes

function a($p){$r=1;foreach($p as$k=>$q)if($p[$k+1]&&abs($p[$k+1]-$q)>1)$r=0;echo$r;}

Try it online!

It can probably be golfed some more, but it's a start.

Jo.

Posted 2017-09-20T15:37:14.223

Reputation: 974

0

F#, 56 bytes

let f a=Seq.forall(fun(x,y)->abs(x-y)<2)(Seq.pairwise a)

Jason Handby

Posted 2017-09-20T15:37:14.223

Reputation: 31

0

Add++, 16 bytes

L,vbUÑ_€|2ª>

Try it online!

How it works

L,		; Create a lambda function
		; Example argument: 	    ['[1 2 3 4 3 4 5 5 5 4]']
	vbU	; Evaluate as list; STACK = [1 2 3 4 3 4 5 5 5 4]
	Ñ_	; Deltas;	    STACK = [1 1 1 -1 1 1 0 0 -1]
	€|	; Absolute values;  STACK = [1 1 1 1 1 1 0 0 1]
	2ª>	; All less than 2;  STACK = [1]

caird coinheringaahing

Posted 2017-09-20T15:37:14.223

Reputation: 13 702