एकरसता को कम करते हैं


33

... लेकिन अरे, सख्त होने की जरूरत नहीं।

कड़ाई से सकारात्मक पूर्णांक के एक गैर-खाली सरणी को देखते हुए, यह निर्धारित करें:

  1. मोनोटोन सख्ती से कम हो रहा है । इसका मतलब है कि प्रत्येक प्रविष्टि पिछले एक की तुलना में कड़ाई से कम है।
  2. गैर-बढ़ती मोनोटोन, लेकिन सख्ती से कम नहीं । इसका अर्थ है कि प्रत्येक प्रविष्टि पूर्ववर्ती से कम या बराबर है, और सरणी उपरोक्त श्रेणी में नहीं आती है।
  3. उपरोक्त में से कोई नहीं

निम्नलिखित कोने के मामलों पर ध्यान दें:

  • एक एकल संख्या के साथ एक सरणी मोनोटोन सख्ती से कम हो रही है (रिक्त रूप से ऐसा)।
  • एक ही संख्या के साथ एक सरणी दोहराया गैर-बढ़ती है, लेकिन सख्ती से कम नहीं है।

नियम

आप एक कार्यक्रम या एक समारोह प्रदान कर सकते हैं

इनपुट को किसी भी उचित प्रारूप में लिया जा सकता है: सरणी, सूची, स्ट्रिंग जो रिक्त स्थान द्वारा अलग किए गए हैं, ...

आप क्रमशः तीन श्रेणियों के लिए कोई तीन सुसंगत आउटपुट चुन सकते हैं। उदाहरण के लिए, आउटपुट संख्या हो सकता है 0, 1, 2; या तार 1 1, 1 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

क्या एक वैरेडिक फ़ंक्शन लिखना (जहां इनपुट मान किसी भी प्रकार के डेटा प्रकार में लिपटे नहीं हैं, लेकिन सभी फ़ंक्शन के लिए सीधे तर्क के रूप में पारित हो जाते हैं) "किसी भी उचित प्रारूप" के तहत आते हैं?
मार्टिन एंडर

@ मॉर्टिन हाँ, यह करता है!
लुईस मेन्डो

जवाबों:


9

जेली , 10 9 5 बाइट्स

-MMod DrMcMoylex द्वारा पाया , कुछ क्रेडिट दे दो!

;0IṠṀ

TryItOnline! या सभी परीक्षण चलाएं

रिटर्न: -1= मोनोटोन सख्ती से कम हो रहा है; 0= मोनोटोन गैर-बढ़ती; 1= अन्य।

कैसे?

;0IṠṀ - Main link: list
;0    - concatenate with a zero
  I   - incremental differences
   Ṡ  - sign
    Ṁ - maximum

क्या diacrtic Mकिसी भी 8bit कैरेक्टर मैप का हिस्सा है? आप यह नहीं कह सकते कि यह 5 बाइट्स है क्योंकि यह नहीं है। CP1252 के पास उदाहरण के लिए नहीं है।
यूरी पिनहोल

2
@EuriPinhollow जेली बाइट्स गिनने के लिए इस कस्टम कोड पेज का उपयोग करती है , जो byteइस पोस्ट के हेडर में शब्द से जुड़ा हुआ है ।
घातक

@ घातक: thx, मिल गया।
यूरी पिनहोल

22

पर्ल 6 , 17 बाइट्स

{[>](@_)+[>=] @_}
  • मोनोटोन सख्ती से कम हो रहा है: 2
  • मोनोटोन गैर-बढ़ती: 1
  • अन्य: 0

विस्तारित:

{            # bare block lambda with implicit parameter list 「@_」
  [>]( @_ )  # reduce using 「&infix:« > »」
  +
  [>=] @_    # reduce using 「&infix:« >= »」
}

4
पर्ल जादुई है
निधि मोनिका का मुकदमा

यह किसी भी प्रकार के साथ काम करने के लिए बढ़ाया जा सकता है अगर >इसके साथ afterऔर >=साथ स्विच किया गया था !beforesay {[after](@_)+[!before] @_}(<d c b a>) #=> 2
ब्रैड गिल्बर्ट

13

MATL , 10 , 7 बाइट्स

0hdX>ZS

इसे ऑनलाइन आज़माएं! या सभी परीक्षण मामलों की पुष्टि करें!

3 बाइट्स सहेजे गए, @LuisMendo की बदौलत!

आउटपुट हैं

  • सख्ती से कम हो रहा है: -1

  • गैर-बढ़ती: 0

  • अन्य: 1

स्पष्टीकरण:

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

1
क्या आप 0पिछले प्लस को लागू करने के बजाय जोड़ नहीं सकते हैं 1? कुछ इस तरह0hdX>ZS
लुइस मेंडो

2
@LuisMendo आह, यह प्रतिभाशाली है! धन्यवाद!
DJMcMayhem

मदद नहीं करता है, लेकिन आप के लिए भी उपयोग कर सकते हैं: 0hdX>0/- आप और @LuisMendo के लिए प्रश्न: क्या यह इस तथ्य का लाभ उठाने के लिए संभव है कि केवल 1 char (विरोध X>) है, किसी तरह से अंतिम मूल्य का उपयोग करके?
डेनिस जहरुद्दीन

@ डेनिसजेरुद्दीन का उपयोग करने के बारे में भी मैंने सोचा था S, लेकिन मुझे इसे छोटा बनाने का कोई तरीका नहीं मिला ...
लुइस मेंडू

9

गणितज्ञ, 22 बाइट्स

Sign@*Max@*Differences

इनपुट के रूप में संख्याओं की सूची लेने का कार्य करें। रिटर्न -1सूची सख्ती से, कम हो रही है, तो है 0अगर यह nonincreasing है, लेकिन सख्ती से कम नहीं है, और 1यह न तो हो चुका है।

बहुत सरल एल्गोरिथ्म: लगातार जोड़े के मतभेदों को ले लो, सबसे बड़ा एक ले लो, और उस सबसे बड़े एक का संकेत ले लो।

(मुझे ऐसा लगता है कि कुछ भाषा मौजूद होनी चाहिए जिसमें यह एल्गोरिथ्म 3 बाइट्स है ....)

एक एकल प्रविष्टि के साथ एक सरणी के बारे में: Differencesएक खाली सूची देता है; Maxएक खाली सूची देता है -∞(!); और (!!) में Sign[-∞]मूल्यांकन करता है -1। तो यह वास्तव में इस कोने के मामले पर काम करता है। कभी-कभी गणितज्ञ को प्यार करते हैं। (वास्तव में, फ़ंक्शन सही ढंग से रिक्त सूची को सख्ती से कम करने के लिए लेबल करता है।)


मैं देख रहा हूँ DrMcMoylex ने मुझे 7 मिनट से हराया! :)
ग्रेग मार्टिन

2
"मुझे लगता है कि कुछ भाषा मौजूद होनी चाहिए, जिसमें यह एल्गोरिथ्म 3 बाइट्स है" chat.stackexchange.com/transcript/message/33720906#33720906 :(
मार्टिन

7

हास्केल, 40 38 37 बाइट्स

foldl min GT.(zipWith compare<*>tail)

रिटर्न

  • GT मोनोटोन के लिए सख्ती से कम हो रही है
  • EQ गैर-बढ़ती मोनोटोन के लिए
  • LT अन्य

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. foldl min GT reduces the list of the comparison results with the min function starting with GT (note: LT < EQ < GT).

Edit: @xnor found 2 3 bytes. Thanks!


Can you prepend a LT instead of appending 0?
xnor

@xnor: Yes, thanks, but it must be a GT, because we need the minimum of the list (I had maximum, which was wrong and a relict from an early version where I used =<< instead of <*>).
nimi

1
I see. Actually, how about foldl min GT?
xnor

6

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))

1
The exact same number of bytes as (defun f(x)(mapcar'apply'(> >=)`(,x,x))). Note that you can just write (lambda(x)...) to be shorter.
coredump

6

Python 2, 30 bytes

lambda l:max(map(cmp,l[1:],l))

-1 for strictly decreasing, 0 for weakly decreasing, +1 for non-decreasing

Using cmp to compare consecutive elements, and takes the maximum. This is done by removing the first element of one copy of the list, then mapping cmp . For example, l=[2,2,1] gives

l[1:]  2   1   None
l      2   2   1
cmp    0  -1   -1

which has max 0 because an equality exists.

The shorter list is automatically extended with None, which is less than all numbers and so harmless. This phantom element also insulates against taking the min of an empty list when the input has length 1.


Even with the very little Python I know I can appreciate how great this answer is
Luis Mendo

5

Brachylog, 7 bytes

>,1|>=,

Try it online!

This prints 1 for strictly decreasing, 0 for non-increasing and false. otherwise.

Explanation

  (?)>              Input is a strictly decreasing list
      ,1(.)         Output = 1
|                 Or
  (?)>=             Input is a non-increasing list
       ,(.)         Output is a free variable; gets automatically labeled as an integer at
                      the end of execution. Since its domain is [-inf, +inf], the first
                      value it takes is 0
                  Or
                    No other possibility, thus this main predicate is false.

Other 7 bytes solutions

>=!>,;1           Returns 0 for strictly decreasing, false. for non-increasing, 1 otherwise.

>=!>,1|           Returns 1 for strictly decreasing, false. for non-increasing, 0 otherwise.

4

R, 44 bytes

d=diff(scan());c(any(!d)&all(d<=0),all(d<0))

Reads input from stdin and prints the following depending on the input:

Output:

[1] FALSE TRUE: Monotone non-increasing

[1] TRUE FALSE: Monotone strictly decreasing

[1] FALSE FALSE: None of the above


d=diff(scan());ifelse(all(d<=0),!prod(d),2) is 1 byte shorter. It returns 0 if monotone strictly, 1 if monotone non-increasing and 2 if none of the above. Not sure if it is allowed to return nothing if none of the above, but then you could simplify further to d=diff(scan());if(all(d<=0))!prod(d).
JAD

Actually, d=diff(scan());if(all(d<=0))any(!d) is one byte better.
JAD

3

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.


3

05AB1E, 5 8 bytes

Bug fixed by Emigna, thanks! It uses the same method as DrMcMoylex's.

®¸ì¥Z0.S

®¸ì   Implicitly take input and appends -1 to it
¥     Yield deltas
 Z    Take the largest delta
  0.S Take its sign and implicitly display it

Try it online!

Output is:

-1 if strictly decreasing sequence
 0 if non-strictly decreasing sequence
 1 otherwise

1
®¸ì¥Z0.S would fix the single element issue.
Emigna

Nice, thanks! I think 0 at the beginning would also work since all numbers are positive (strictly by default I guess).
Osable

Yes 0 would work as well, but it's nice that it works for input containing 0 (even though by definition it wont) :)
Emigna

Fun fact: in French "positive" means positive or zero and you have to specify "strictly positive" to reach the same meaning as "positive" in English.
Osable

3

Ruby, 37 bytes

->l{[d=l==l.sort.reverse,d&&l|[]==l]}

Output:[true,true], [true,false] or [false,false]


2

Mathematica, 15 11 bytes

##>0|##>=0&

This is a variadic function, taking all input integers as separate arguments.

  • Strictly decreasing: True | True
  • Non-increasing: False | True
  • Neither: False | False

Note that | is not Or but Alternatives, which is part of pattern matching syntax, which explains why these expressions don't get evaluated to True, True, False, respectively.

The code itself is mostly an application of this tip. For example ##>0 is Greater[##, 0] but then ## expands to all the input values so we get something like Greater[5, 3, 2, 0], which itself means 5>3>2>0.


2

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))

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

2

C++14, 85 bytes

int f(int x){return 3;}int f(int x,int y,auto...p){return((x>=y)+2*(x>y))&f(y,p...);}

Returns 3 (0b11) for strict decreasing, 1 (0b01) for non-increasing and 0 otherwise.

Ungolfed:

int f(int x) {return 3;}
int f(int x,int y,auto...p){
  return ((x>=y)+2*(x>y)) & f(y,p...);
}

I thought this was a perfect problem for C++17's folding expressions:

int g(auto...x){return(x>...)+(x>=...);}

Unfortunately it does not chain the relational operators but does

((x1>x2)>x3)>x4)...

which is not was wanted.


2

Python 2, 61 74 bytes

+13 bytes for the single number input

x=map(str,input())
print[2,eval(">".join(x))+eval(">=".join(x))][len(x)>1]

Requires input in bracket list form like [3,2,1]. Returns 2 for strict decreasing, 1 for non-increasing and 0 otherwise.

Old solution:

print eval(">".join(x))+eval(">=".join(x))

2

Python 3, 81 52 bytes (Thanks to FryAmTheEggMan)

e=sorted
lambda a:(a==e(a)[::-1])+(e({*a})[::-1]==a)

Try it online !


sorted(s)[::-1] is shorter for reversing a sorted list. In Python 3 you can do {*a} to get a set of the elements of a. sorted returns a list so you don't have to cast the set to a list either. Also adding booleans is perfectly kosher! Finally you can submit an anonymous lambda, so you don't need f=. I get 52 bytes in the end. repl.it/E7eG/2
FryAmTheEggman

2

Befunge, 50 bytes

&: >~1+#^_v>:0`|
1\:^  @.$$<-@.2_
-: ^    >& ^   >

Try it online!

Accepts input as a sequence of int separated by spaces, and returns 0 if strictly decreasing, 1 if non-strictly decreasing, 2 otherwise.

Since reading befunge is kind of impossible if you don't know the language, this is the algorithm in pseudocode:

push(input())

while( getchar()!=EOF ){
  push(input())
  subtract()
  duplicate()
  if(pop()>0){
    subtract() //we are doing a-(a-b), so b is now on top
    duplicate()
  }
  else{
    if(pop()==0){
      push(1) //the sequence is not strictly decreasing
      swap()
      duplicate()
    }
    else{
      push(2) //the sequence has just increased
      output(pop)
      return
    }
  }
}
pop()
pop()
output(pop())

*in befunge memory is a stack which starts with an infinite amount of 0 on it. pop(), push(x), input() and output(x) are self-explainatory, the other pseudofunctions i used work like this:

function duplicate(){
  a=pop()
  push(a)
  push(a)
}

function subtract(){
  a=pop()
  b=pop()
  push(b-a)
}

function swap(){
  a=pop()
  b=pop()
  push(a)
  push(b)
}

Funge!


Previous version, just 41 bytes but invalid since it requires a 0 to terminate the input sequence (or using an interpreter like this)

&:  >&:|>:0`|
1\v@.$_<-@.2_
- >:v  >^   >

Try it online!


I'm afraid a trailing 0 doesn't count as a valid input format. I think it falls under the "preprocessed input" category. In fact, some answers append a 0 in the code (thus including that in the byte count). I would be acceptable if you could Can you replace the 0 by some non-numeric character? That would be acceptable
Luis Mendo

@LuisMendo Actually, with this interpreter (which is the one I used to develop the code) EOF does return 0, so there is no need to add anything to the input. I wasn't able to discover what the intended behaviour should be, so I don't know if this assumption is standard or not. A thing that I may have misinterpreted, though, is: can zeroes be part of the input sequence? If so, I would need to modify the code anyway.
Leo

No, zeros can't be part of the sequence (Given a non-empty array of positive integers_ I meant strictly positive integers). But some answers do use a 0 inserted by the code to deal with the case that the input has only one entry. That's one reason why I consider that including that 0 in the input is not valid. Anyway, if there is an interpreter that doesn't need it, you can use that interpreter to prove that your answer is valid without the 0. If the Try-it-online interpreter needs that 0, you can include it for demonstration purposes, with an appropriate explanation note
Luis Mendo

@JamesHolderness while on tryonline ~ works as it should, & has a strange behaviour on EOF, apparently repeating the last input forever. See here for an example
Leo

1
I edited the answer using James's approach, now input is terminated by EOF
Leo

2

J, 14 bytes

Monadic verb taking the list on the right, returning 1 for strictly decreasing, 0 for weakly decreasing, and _1 otherwise.

*@([:<./2-/\])

Takes the sign * of the minimum <./ of consecutive differences 2-/\ of the list. J doesn't swap the order of the differences when taking them so e.g. the sequence is strictly decreasing if these are all positive. Notably, <./ returns positive infinity on zero-element lists.

In use at the REPL:

   *@([:<./2-/\]) 3
1
   *@([:<./2-/\]) 3 2
1
   *@([:<./2-/\]) 3 2 2
0
   *@([:<./2-/\]) 3 2 2 3
_1

2

C, 68 67 Bytes

A function f, which is passed an array of ints (l) preceded by its length (n, also an int). Returns 3 if monotone strictly decreasing, 1 if monotone non-increasing, but not strictly decreasing, 0 otherwise.

f(int n,int*l){return n<2?3:((l[0]>l[1])*2|l[0]>=l[1])&f(n-1,l+1);}

Un-golfed slightly for readability:

int f_semiungolfed(int n, int* l) {
    return (n < 2) ? 3 : ((l[0] > l[1]) * 2 | l[0] >= l[1]) & f(n - 1, l + 1);
}

Rearranged and commented to show logic:

int f_ungolfed(int n, int* l) {
    int case1 = 0, case2 = 0, recursion = 0;
    if (n < 2) { // Analogous to the ternary conditional I used - n < 2 means we have a single-element/empty list
        return 3; // Handles the vacuous-truth scenario for single lists
    } else {
        case1 = l[0] > l[1]; // The first case - are the two numbers in the list strictly decreasing? (case1 is 1 if so)
        case2 = l[0] >= l[1]; // The second case - are the two numbers strictly non-increasing (case2 is 1 if so)
        recursion = f_ungolfed(n - 1, l + 1); // Recursively call ourselves on the "rest" of the list (that is, everything other than the first element). Consider that comparison is transitive, and that we already have a vacuous-truth scenario covered.
        case1 *= 2; // Shift case1's value over to the left by one bit by multiplying by 2. If case1 was 1 (0b01), it's now 2 (0b10) - otherwise it's still 0 (0b00)
        return (case1 | case2) & recursion; 
        // The bitwise OR operator (|) will combine any 1-bits from case1's value (either 0b10 or 0b00) or case2's value (either 0b01 or 0b00) into either 3, 2, 1, or 0 (0b11, 0b10, 0b01, or 0b00 respectively).
        // The bitwise AND operator (&) will combine only matching 1-bits from (case1|case2) and the return value of the recursive call - if recursion = 0b11 and case1|case2 = 0b01, then the return value will be 0b01.
    }
}

Test cases (courtesy IDEOne):

{7, 5, 4, 3, 1}: 3
{42, 41}: 3
{5}: 3
{27, 19, 19, 10, 3}: 1
{6, 4, 2, 2, 2}: 1
{9, 9, 9, 9}: 1
{1, 2, 3, 2}: 0
{10, 9, 8, 7, 12}: 0
{4, 6, 4, 4, 2}: 0

2

Retina, 41 bytes

\d+
$*
A`\b(1+) 1\1
S`\b$
\b(1+) \1\b.*|$

Try it online! (The first line enables a linefeed-separated test suite.)

  • Strictly decreasing: 2
  • Non-increasing: 3
  • Neither: 1

Explanation

\d+
$*

Converts the input unary.

A`\b(1+) 1\1

The regex here matches an increasing pair of consecutive numbers. If this is the case, the input can clearly not be non-increasing. The A denotes it as an "anti-grep" stage which means that the input line is discarded and replaced with the empty string if the regex matches.

S`\b$

This is a split stage which is used to append a linefeed to the input only if the input wasn't discarded. So we've got two possible outcomes so far: non-increasing inputs get a linefeed at the end and others are still empty.

\b(1+) \1\b.*|$

Finally, we count the number of matches of this regex. The regex either matches to identical numbers (and then everything to the end of the string to avoid multiple matches of this kind for inputs like 1 1 1 1), or the "end of the input". Let's go through the three types of inputs:

  • Strictly decreasing: the first part of the regex can't match because all values are unique, but the $ matches. Now $ isn't exactly "the end of the string". It can also match in front of a trailing linefeed. So we'll actually get two matches from it, one at the end of the input, and one after the linefeed we inserted.
  • Non-increasing: now the first part of the regex also provides a match, and we end up with three matches.
  • Neither: remember that we took care to turn the input into an empty string, so now $ matches only once.

1

Axiom, 114 bytes

m(c:List(INT)):INT==(i:=r:=1;repeat(~index?(i+1,c)=>break;c.i<c.(i+1)=>return 0;if c.i=c.(i+1)then r:=2;i:=i+1);r)

Ungolfed

-- if [a,b,..n] decrescente ritorna 1
--          non crescente   ritorna 2
--          altrimenti      ritorna 0  
m(c:List(INT)):INT==
   i:=r:=1
   repeat
      ~index?(i+1,c)=>break 
      c.i<c.(i+1)   =>return 0
      if c.i=c.(i+1) then r:=2
      i:=i+1
   r

Results

(x) -> m([3,1])=1, m([1,1])=2, m([])=1, m([1])=1, m([1,3])=0
   (x)  [1= 1,2= 2,1= 1,1= 1,0= 0] 

1
Forse dovresti tradurre i commenti all'inglese :-)
Luis Mendo

1

APL, 16 bytes

(a≡a[⍒a])×1+a≡∪a

Note: enter one element array as eg a←1⍴3 otherwise: a←4 3 2 1

Interpreting output:

2 Monotone strictly decreasing
1 Monotone non-increasing, but not strictly decreasing
0 None of the above

Idea: test for monotonicity by comparing original to sorted array, check for non-increasing by comparing to array with removed duplications.

(And I think it can be improved...)


Changed to one number. Bytes increased by 2...
Roman Susi

1

Haskell, 36 bytes

f l=[scanl1(min.(+x))l==l|x<-[0,-1]]

(+x) is because haskell mis-interprets (-x) as a value instead of a section. I wonder if the whole expression can be profitably made pointfree.


1

LabVIEW, 12 nodes, 18 wires ==> 48 bytes by convention

enter image description here

No functions hidden in the other case frames, just a single wire across.


1

Ceylon, 86 bytes

Object m(Integer+l)=>let(c=l.paired.map(([x,y])=>x<=>y))[if(!smaller in c)equal in c];

The function takes the input as its parameters, and returns a tuple of zero or one booleans – [false] for Monotone strictly decreasing, [true] for Monotone non-increasing, but not strictly decreasing, and [] for None of the above.

It can be used like this:

shared void run() {
    print("Monotone strictly decreasing:");
    print(m(7, 5, 4, 3, 1));
    print(m(42, 41));
    print(m(5));

    print("Monotone non-increasing, but not strictly decreasing:");
    print(m(27, 19, 19, 10, 3));
    print(m(6, 4, 2, 2, 2));
    print(m(9, 9, 9, 9));

    print("None of the above:");
    print(m(1, 2, 3, 2));
    print(m(10, 9, 8, 7, 12));
    print(m(4, 6, 4, 4, 2));
}

Output:

Monotone strictly decreasing:
[false]
[false]
[false]
Monotone non-increasing, but not strictly decreasing:
[true]
[true]
[true]
None of the above:
[]
[]
[]

An ungolfed and commented version:

// Let's decrease the monotony! 
//
// Question:  http://codegolf.stackexchange.com/q/101036/2338
// My Answer: http://codegolf.stackexchange.com/a/101309/2338


// Define a function which takes a non-empty list `l` of Integers)
// (which arrive as a tuple) and returns an Object (actually
// a `[Boolean*]`, but that is longer.) 
Object m(Integer+ l) =>
        // the let-clause declares a variable c, which is created by taking
        // pairs of consecutive elements in the input, and then comparing
        // them. This results in lists like those (for the example inputs):
        // { larger, larger, larger, larger }
        // { larger }
        // {}
        // { larger, equal, larger, larger }
        // { larger, larger, equal, equal }
        // { equal, equal, equal }
        // { smaller, smaller, larger }
        // { larger, larger, larger, smaller }
        // { smaller, larger, equal, larger }  
        let (c = l.paired.map( ([x,y]) => x<=>y) )
            // now we analyze c ...
            // If it contains `smaller`, we have an non-decreasing sequence.
            // We return `[]` in this case (an empty tuple).
            // Otherwise we check whether `equal` is in the list, returning
            // `[true]` (a non-strictly decreasing sequence) if so,
            // and `[false]` (a strictly decreasing sequence) otherwise.
            [if(!smaller in c) equal in c];

1

Clojure, 34 bytes

#(if(apply > %)1(if(apply >= %)2))

Very straight-forward, returns 1 for if strictly decreasing, 2 if non-increasin and nil otherwise.

Also tried avoiding apply with macro's ~@ but it is just longer at 43 chars (this results in [1 2 nil]):

(defmacro f[& i]`(if(> ~@i)1(if(>= ~@i)2)))

[(f 7 5 4 3 1)
 (f 27 19 19 10 3)
 (f 1 2 3 2)]

1

Pip, 8 bytes

O$>g$>=g

Full program. Takes the input list as command-line arguments. Outputs 11 for strictly decreasing, 01 for non-increasing, 00 for neither.

Try it online!

Explanation

This approach works because Pip's comparison operators, like Python's, chain together: 4>3>2 is true, rather than being (4>3)>2 (false) as in C. And the same behavior holds when the comparison operators are modified with the $ fold meta-operator.

          g is list of command-line args (implicit)
 $>g      Fold g on >
O         Output without newline
    $>=g  Fold g on >=
          Print (implicit)

1

Japt, 9 8 7 bytes

Outputs -1 for "monotone strictly decreasing", 0 for "monotone non-incresing" and 1 otherwise.

än rw g

Try it

1 byte saved thank to Oliver.


@Oliver, yes; otherwise it will default to ... Wait, what? Why does that work?! än mg rw returns the wrong results without the J but that's not the case with än rw g. Weird.
Shaggy

1

R, 34 bytes

function(x)max(sign(diff(c(x,0))))

Try it online!

Ports DJ's MATL answer.

R, 43 bytes

function(x)all(diff(x)<0)+all(x==cummin(x))

Try it online!

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

all(x==cummin(x)) is TRUE (converts to 1 when used in arithmetic) if and only if f is non-increasing, including the strict case.

all(diff(x)<0) is TRUE only when f is strictly decreasing.

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.