43
\$\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\$
3
  • \$\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\$ Sep 20, 2017 at 17:36
  • 1
    \$\begingroup\$ @MartinEnder Any two distinct and consistent values. P.S Sorry for the late response \$\endgroup\$
    – user70974
    Sep 20, 2017 at 18:04
  • 3
    \$\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, 2017 at 12:50

53 Answers 53

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

K (ngn/k), 15 13 bytes

-2 bytes from @ngn's improvements

&/2>#'!'1_-':

Try it online!

  • -': subtract each value in the (implicit) input from its predecessor
  • 1_ drop the first result
  • #'!' take the absolute value (literally, take the count of the range of each value; e.g. 1 -> ,0, -2 -> -2 -1)
  • &/2> are all the absolute values smaller than 2?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ here's an idea: abs:{#'!'x} \$\endgroup\$
    – ngn
    Mar 27, 2021 at 5:56
  • \$\begingroup\$ Makes the literal translation a bit more... nuanced, but saves some bytes! Thanks! \$\endgroup\$
    – coltim
    Mar 27, 2021 at 16:04
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ You could use currying: a=>u=>!a.some(e=>(e-=(u=e))>1|e<-1) \$\endgroup\$
    – Bálint
    Sep 24, 2017 at 10:30
1
\$\begingroup\$

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

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

Try it online!

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

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!")
}
\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

Common Lisp, 53 bytes

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

Try it online!

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

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

F#, 56 bytes

let f a=Seq.forall(fun(x,y)->abs(x-y)<2)(Seq.pairwise a)
\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

Desmos, 46 bytes

\left\{max(abs(n[2,...,n.length]-n))<2\right\}

View on Desmos!

This is a rare case where I think using \left\{\right\} is more efficient than not.

\$\endgroup\$
1
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\$
1
  • \$\begingroup\$ abs(a[i]-a[i+1])<2 -> -2<a[i]-a[-~i]<2. \$\endgroup\$ Sep 20, 2017 at 15:47
0
\$\begingroup\$

Clojure, 36 bytes

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

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

Python 3, 49 bytes

lambda l:all(abs(j-i)<=1 for i,j in zip(l,l[1:]))
\$\endgroup\$
2
  • \$\begingroup\$ Unfortunately, this doesn't work for arrays like [1, 1, 3]. \$\endgroup\$
    – Dennis
    Sep 21, 2017 at 17:07
  • 1
    \$\begingroup\$ Great catch on that edge case!! Updated with another answer. @Dennis \$\endgroup\$
    – ospahiu
    Sep 21, 2017 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\$
5
  • \$\begingroup\$ If Jelly uses Unicode characters and just count 1 byte, why wouldn't also count just 1 byte? \$\endgroup\$ Oct 31, 2017 at 9:11
  • \$\begingroup\$ @LS_ᴅᴇᴠ If by "Unicode" you mean UTF-8, most Jelly programs don't use UTF-8. \$\endgroup\$
    – Jordan
    Nov 1, 2017 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\$ Nov 2, 2017 at 1:07
  • \$\begingroup\$ @Jordan Yes, you are right, I read something more about Jelly and get it! \$\endgroup\$ Nov 2, 2017 at 7:26
  • \$\begingroup\$ It should be measured by how many bytes it takes on the calculator. Which if it's tokenized, could be much less than 26 bytes. \$\endgroup\$
    – Deadcode
    Apr 2, 2021 at 14:59
0
\$\begingroup\$

Pxem, Filename: 40 bytes + Content: 0 bytes = 40 bytes.

  • Filename (escaped): ._\001.-.c._.t.w\001.-._.c.m.-\001.x.d.a.t.c.aY.o
  • Content: empty

Usage

Input is from STDIN; output from STDOUT. First positive integer N for input represents number of items in list. Then integers c1, c2, ..., cN represents each item of the list. Each input has to be separated by one or more s.

An output of Y represents truthy, and no output falsey.

Input example

List [1, 2, 3, 4, 3, 4, 5, 5, 5, 4] is:

10
1 2 3 4 3 4 5 5 5 4

With comments

XX.z
.a._\001.-.cXX.z # push N; decrement it
.a._.tXX.z # push c1; heap<-c1
.a.w\001.-XX.z # while N!=0; do N--
  .a._.c.m.-\001.xXX.z # push ci; if abs(ci-heap); then
    .a.dXX.z # exit
  .a.aXX.z # fi
  .a.tXX.z # heap=ci
.a.c.aXX.z # done
.aY.oXX.z # putchar o
.a

Link

Try it online!

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

Regex (ECMAScript), 27 bytes

^(x(x*)(,(?=\2x?x?\b)|$))*$

Try it online!

Input is a list of positive unary numbers separated by ,. Each one is a string of xs whose length represents the number.

^
(
    x(x*)         # \2 = this element - 1
    # Assert that either the next element is in the range \2 to \2+2 inclusive,
    # or that we've reached the end and there are no more elements.
    (
        ,         # Skip to the next element
        (?=       # look ahead to the next element to make the following assertion:
            \2    # subtract \2 from it
            x?x?  # subtract any number from 0 to 2 from it
            \b    # assert that we matched the exact number
        )
    |
        $
    )
)*
$
\$\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\$
1
1
2

Your Answer

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