42.9k views
0 votes
Associate true with the variable is_ascending if the list numbers is in ascending order (that is, if each element of the list is greater than or equal to the previous element in the list). otherwise, associate false with is_ascending

User Altandogan
by
8.4k points

2 Answers

5 votes
The "is_ascending" variable allows for the programmer to denote if the list in question is in ascending order. If the list is in ascending order, then the variable "is_ascending" is considered true, and marked with variable as such. If the list is in descending order, in no discernible order, etc., the is_ascending variable does not apply, and is therefore false in reference to that list.
User Mokosha
by
7.7k points
3 votes
is_ascending = True
p = None
for n in numbers:
....if p is not None and n < p:
........is_ascending = False
........break
....p = n

This would also work
is_ascending = True if numbers == sorted(numbers) else False

In a real world scenario I would probably do this.

def issorted(seq):
....if not seq: return True
....iseq = iter(seq)
....prev = next(iseq)
....for x in iseq:
........if x < prev:
............return False
....return True


The answer the is_ascending is False
User Dhunt
by
8.4k points