python code to find if an array is monotonic

0 votes
I'm trying to write a python code to find if an array is monotonic. Can you help me out with it? Thank you.
Jun 17 in Python by Harsha
27 views

1 answer to this question.

0 votes

Hey Harsha, try out this code:

def monotonic(a):
    n = len(a)
    return (all(a[i]<=a[i+1] for i in range(n-1)) or
            all(a[i]>=a[i+1] for i in range(n-1)))
a = [6, 5, 4, 3, 2]
print(monotonic(a))

output:

True

Process finished with exit code 0
answered Jun 17 by Yesha

Related Questions In Python

+4 votes
7 answers
0 votes
1 answer
0 votes
1 answer

How to find if setuptools is installed in python?

You can use python interpreter to check ...READ MORE

answered Jan 29 in Python by Omkar
• 67,380 points
28 views
0 votes
1 answer

Is it possible to find the source code of built-in functions of Python?

Since Python is open source you can ...READ MORE

answered Jul 17 in Python by Neel
• 2,930 points
21 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 215 views
+4 votes
6 answers
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,160 points
57 views
0 votes
1 answer

Is it possible to create an array with all values as zero in python?

You can use  np.zeros(4,3) This will create a 4 ...READ MORE

answered May 24 in Python by Anjali
41 views