40
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Martin Ender Sep 20 '17 at 17:36
  • 1
    \$\begingroup\$ @MartinEnder Any two distinct and consistent values. P.S Sorry for the late response \$\endgroup\$ – user70974 Sep 20 '17 at 18:04
  • 2
    \$\begingroup\$ The 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? \$\endgroup\$ – Mark S. Sep 23 '17 at 12:50

48 Answers 48

1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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]
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ [:*/2>2|@-/\] should work for 6? fewer bytes. \$\endgroup\$ – cole Nov 1 '17 at 4:27
  • \$\begingroup\$ @cole Thanks, this is much better than mine! \$\endgroup\$ – Galen Ivanov Nov 1 '17 at 8:49
1
\$\begingroup\$

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!

\$\endgroup\$
  • 2
    \$\begingroup\$ ->a{b,=a;a.all?{|x|(b-(b=x))**2<2}} is 35 bytes. \$\endgroup\$ – MegaTom Sep 21 '17 at 18:55
  • \$\begingroup\$ @MegaTom right you are! \$\endgroup\$ – Snack Sep 21 '17 at 18:59
  • 1
    \$\begingroup\$ ->b,*a{a.all?{|x|(b-(b=x))**2<2}} is 33 bytes. \$\endgroup\$ – Jordan Nov 1 '17 at 20:27
  • \$\begingroup\$ @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. \$\endgroup\$ – Snack Nov 1 '17 at 22:29
  • \$\begingroup\$ No array, just pass the input as individual arguments. \$\endgroup\$ – Jordan Nov 1 '17 at 22:35
0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ abs(a[i]-a[i+1])<2 -> -2<a[i]-a[-~i]<2. \$\endgroup\$ – Jonathan Frech Sep 20 '17 at 15:47
0
\$\begingroup\$

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

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I don't think -p is needed to be a +1. Does it? \$\endgroup\$ – Stan Strum Sep 20 '17 at 20:12
0
\$\begingroup\$

Clojure, 36 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 53 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 49 bytes

lambda l:all(abs(j-i)<=1 for i,j in zip(l,l[1:]))
\$\endgroup\$
  • \$\begingroup\$ Unfortunately, this doesn't work for arrays like [1, 1, 3]. \$\endgroup\$ – Dennis Sep 21 '17 at 17:07
  • 1
    \$\begingroup\$ Great catch on that edge case!! Updated with another answer. @Dennis \$\endgroup\$ – ospahiu Sep 21 '17 at 18:47
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Scheme - 89 bytes

(define (f n) (if (not (empty? n)) (and (<= (abs (- (cadr n) (car n))) 1) (f (cdr n))) 1)
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ If Jelly uses Unicode characters and just count 1 byte, why wouldn't also count just 1 byte? \$\endgroup\$ – LS_ᴅᴇᴠ Oct 31 '17 at 9:11
  • \$\begingroup\$ @LS_ᴅᴇᴠ If by "Unicode" you mean UTF-8, most Jelly programs don't use UTF-8. \$\endgroup\$ – Jordan Nov 1 '17 at 20:30
  • \$\begingroup\$ @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. \$\endgroup\$ – numbermaniac Nov 2 '17 at 1:07
  • \$\begingroup\$ @Jordan Yes, you are right, I read something more about Jelly and get it! \$\endgroup\$ – LS_ᴅᴇᴠ Nov 2 '17 at 7:26
0
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

F#, 56 bytes

let f a=Seq.forall(fun(x,y)->abs(x-y)<2)(Seq.pairwise a)
\$\endgroup\$
0
\$\begingroup\$

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]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy