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

69 Answers 69

1 2
3
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
    Commented Sep 21, 2017 at 17:07
  • 1
    \$\begingroup\$ Great catch on that edge case!! Updated with another answer. @Dennis \$\endgroup\$
    – ospahiu
    Commented 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\$ Commented 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
    Commented 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\$ Commented Nov 2, 2017 at 1:07
  • \$\begingroup\$ @Jordan Yes, you are right, I read something more about Jelly and get it! \$\endgroup\$ Commented 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
    Commented 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\$

Factor, 28 bytes

[ [ - [-1,1]? ] monotonic? ]

Try it online!

monotonic? is a combinator that tests if a quotation returns true for every pair of adjacent elements in a sequence.

  • - Subtract.
  • [-1,1]? Is it between -1 and 1 inclusive? (This is just the name of a word, like most everything in Factor.)
\$\endgroup\$
0
\$\begingroup\$

Arturo, 36 bytes

$[a][n:a\0every? a'x[2>abs n-x n:x]]

Try it

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

Stax, 8 bytes

┘ε≡}◄ï╖Z

Run and debug it

Link includes all test cases.

Unpacked:

Stax, 10 bytes

:-{|a2<m:*

Run and debug it

:-          # pairwise differences/deltas
  {     m   # mapped by
   |a       # absolute value of n
     2<     # is less than 2
         :* # product of array (essentially all(a))
\$\endgroup\$
0
\$\begingroup\$

Kamilalisp, 31 bytes (APL385 SBCS)

$(∧?∘⌹‡⍡ 2 $(> 2)∘abs∘$(⌿⊙← -))

Attempt This Online!

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.