Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

...but hey, no need to be strict.

Given a non-empty array of positive integers, determine if it is:

  1. Monotone strictly decreasing. This means that each entry is strictly less than the previous one.
  2. Monotone non-increasing, but not strictly decreasing. This means that each entry is less than or equal to the preceding, and the array does not fall in the above category.
  3. None of the above.

Note the following corner cases:

  • An array with a single number is monotone strictly decreasing (vacuously so).
  • An array with the same number repeated is monotone non-increasing, but not strictly decreasing.

Rules

You may provide a program or a function

Input can be taken in any reasonable format: array, list, string with numbers separated by spaces, ...

You can choose any three consistent outputs for the three categories respectively. For example, outputs can be numbers 0, 1, 2; or strings 1 1, 1 0, empty string.

Shortest code in bytes wins

Test cases

Monotone strictly decreasing:

7 5 4 3 1
42 41
5

Monotone non-increasing, but not strictly decreasing:

27 19 19 10 3
6 4 2 2 2
9 9 9 9

None of the above:

1 2 3 2
10 9 8 7 12
4 6 4 4 2
share|improve this question

Perl 6, 17 bytes

{[>](@_)+[>=] @_}
  • Monotone strictly decreasing: 2
  • Monotone non-increasing: 1
  • Other: 0

Expanded:

{            # bare block lambda with implicit parameter list 「@_」
  [>]( @_ )  # reduce using 「&infix:« > »」
  +
  [>=] @_    # reduce using 「&infix:« >= »」
}
share|improve this answer
    
Perl is magical – QPaysTaxes 1 hour ago

MATL, 10, 7 bytes

0hdX>ZS

Try it online! or verify all test cases!

3 bytes saved, thanks to @LuisMendo!

The outputs are

  • Strictly decreasing: -1

  • Non-increasing: 0

  • Other: 1

Explanation:

0           % Push a '0'
 h          % Join the 0 to the end of the input array.
  d         % Get consecutive differences
   X>       % Get the largest difference
     ZS     % Get its sign
            % Implicitly print it
share|improve this answer
1  
Can't you append a 0 instead of prepending the last plus 1? Something like 0hdX>ZS – Luis Mendo 6 hours ago
1  
@LuisMendo Ah, that's genius! Thankyou! – DrMcMoylex 6 hours ago

Mathematica, 22 bytes

Sign@*Max@*Differences

Unnamed function taking a list of numbers as input. Returns -1 if the list is strictly decreasing, 0 if it's nonincreasing but not strictly decreasing, and 1 if it's neither.

Pretty simple algorithm: take the differences of consecutive pairs, take the largest one, and take the sign of that largest one.

(I feel like there must exist some language in which this algorithm is 3 bytes....)

Regarding an array with a single entry: Differences yields an empty list; Max of an empty list gives -∞ (!); and Sign[-∞] evaluates to -1 (!!). So it actually works on this corner case. Gotta love Mathematica sometimes. (Indeed, the function also correctly labels an empty list as strictly decreasing.)

share|improve this answer
    
I see DrMcMoylex beat me by 7 minutes! :) – Greg Martin 5 hours ago
    
"I feel like there must exist some language in which this algorithm is 3 bytes" chat.stackexchange.com/transcript/message/33720906#33720906 :( – Martin Ender 40 mins ago

Common Lisp, 43 40 bytes

(defun f(x)`(,(apply'> x),(apply'>= x)))

This takes input as a Lisp list, and returns (T T), (NIL T) and (NIL NIL) to distinguish the 3 categories. Here it is running on the provided test cases:

CL-USER> (mapcar #'f '((7 5 4 3 1)
                       (42 41)
                       (5)
                       (27 19 19 10 3)
                       (6 4 2 2 2)
                       (9 9 9 9)
                       (1 2 3 2)
                       (10 9 8 7 12)
                       (4 6 4 4 2)))
((T T) (T T) (T T) (NIL T) (NIL T) (NIL T) (NIL NIL) (NIL NIL) (NIL NIL))
share|improve this answer

Jelly, 10 9 5 bytes

-Method found by DrMcMoylex, go give some credit!

;0IṠṀ

TryItOnline! or run all tests

Returns :-1=monotone strictly decreasing; 0=monotone non-increasing; 1=other.

How?

;0IṠṀ - Main link: list
;0    - concatenate with a zero
  I   - incremental differences
   Ṡ  - sign
    Ṁ - maximum
share|improve this answer

Haskell, 40 bytes

maximum.(zipWith compare<*>tail).(++[0])

Returns

  • GT for Monotone strictly decreasing
  • EQ for Monotone non-increasing
  • LT else

compare compares two numbers and returns GT (EQ, LT) if the first number is greater than (equal to, less than) the second number. zipWith compare<*>tail compares neighbor elements. maximum picks the greatest of the comparison results (where GT > EQ > LT). Before checking a dummy 0 is appended to the input list to make it work for singleton lists, too.

share|improve this answer

Racket, 44 bytes

(λ(x)(map(λ(p)(apply p`(,@x 0)))`(,>,>=)))

Invoked:

(map (λ(x)(map(λ(p)(apply p`(,@x 0)))`(,>,>=)))
 '((7 5 4 3 1)
   (42 41)
   (5)
   (27 19 19 10 3)
   (6 4 2 2 2)
   (9 9 9 9)
   (1 2 3 2)
   (10 9 8 7 12)
   (4 6 4 4 2)))

Result:

'((#t #t)
 (#t #t)
 (#t #t)
 (#f #t)
 (#f #t)
 (#f #t)
 (#f #f)
 (#f #f)
 (#f #f))
share|improve this answer
    
It's a shame Racket doesn't define the arity 1 case of > as true. Common Lisp gets that right, but fails to define the arity 0 case (which should also be true). – Omar 5 hours ago

JavaScript (ES6), 51 bytes

a=>a.some((e,i)=>e>a[i-1])+a.some((e,i)=>e>=a[i-1])

Returns 0 for strict decreasing, 1 for non-increasing, 2 otherwise.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.