1

My assignment involves creating an algorithm that can tell if a list is ascending or not. If the list is ascending with no duplicates in the list, the output should return True otherwise if the list isn't ascending or has a duplicate, it should return False.

I'm having issues with figuring out how to return "True" or "False" instead of it being me using a string to print that out.

input:

def tr1(lis):

  if (len(lis)== len(set(lis)) and (sorted (lis) == lis)) :
    print ("True")
  else:
    print ("False")
tr1([1,2,3,4,4])

output:

False
snk
  • 91
  • 1
  • 8
  • You don't need to use `elif` when the second condition is the exact opposite of the first. Just use `else:` – Barmar Oct 31 '19 at 22:02
  • Possible duplicate of [Python Function Returning None](https://stackoverflow.com/questions/21471876/python-function-returning-none) – norok2 Oct 31 '19 at 22:07

3 Answers3

0

Generally to assign/return boolean values, instead of doing this:

my_result = False

if condition1 and condition2:
    my_result = True
else:
   my_result = False

You can simply do:

my_result = condition1 and condition2

Back to your question of creating sets.

You can create set from a list (or any iterable object) like below:

your_list = [1,2,3,4]

your_set = set(your_list)

if (len(your_set)==len(your_list):
    pass #All elements are unique

The primary property of a set is that it doesn't have duplicates. So if the length of your list and your set match then we can deduce that all elements in your list are unique.


You can check if all elements are in ascending order by first sorting the list and then matching if your sorted list is the same as your original list. If that's the case, then you can decude that all elements in your list are in ascending order.

your_list = [1,3,2,4]

sorted_list = sorted(your_list) #[1,2,3,4]

descending_sorted_list = sorted(your_list, reverse=True) #[4,3,2,1]

if (sorted_list == your_list):
    pass #They are in ascending order

Joining these two conditions with an AND operators validates both the conditions.

#This sets a boolean (True/False)
result = (sorted_list == your_list) and (len(your_set)==len(your_list))

Similarly, in a method context you can return it directly:

return (sorted_list == your_list) and (len(your_set)==len(your_list))
Rithin Chalumuri
  • 1,739
  • 7
  • 19
0

Detecting duplicates is handled in many other posts: make a set from the list and check their lengths; if there's a duplicate, the set will be shorter.

Printing a Boolean value requires thinking in Boolean data, just as you've learned to think in numerical and text data.

print(sorted(lis) == list)

That's the entire case. Put them together:

print(sorted(lis) == list and len(set(lis)) == len(lis) )
Prune
  • 76,765
  • 14
  • 60
  • 81
0

For increasing or decreasing test, you can use python lambda expressions:

For decreasing:

is_decreasing = all(l[i] <= l[i+1] for i in range(len(l)-1))

For increasing:

is_increasing = all(l[i] <= l[i+1] for i in range(len(l)-1))

is_decreasing and is_increasings are both boolean.

Mert Köklü
  • 2,183
  • 2
  • 16
  • 20