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, 2019 in Python by Harsha
2,330 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, 2019 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, 2019 in Python by Omkar
• 69,220 points
2,233 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, 2019 in Python by Neel
• 3,020 points
4,771 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,423 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,043 views
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,300 points
943 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, 2019 in Python by Anjali
1,120 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP