1

How do I check for unsorted string within the list? If it is unsorted the function called need to return true

list([['3C', 'AS', '2H']])
list([['0C', 'JH', 'QS', 'KH', '9D']])
dspencer
  • 4,297
  • 4
  • 22
  • 43
Bryan Ng
  • 21
  • 2

2 Answers2

0

You mean like this?

a = ['3C', 'AS', '2H']
b = ['0C', 'JH', 'QS', 'KH', '9D']

def sorted_or_not(lis):
    if lis != sorted(lis):
        return True
    else:
        return False

print(sorted_or_not(a))
print(sorted_or_not(b))

Output:

True
True
Red
  • 26,798
  • 7
  • 36
  • 58
0

You might want to consider rephrasing your question. I'm not sure what you mean But one guess I have is that you want to check to see if a list has been sorted using the sorted() method:

ls = ['3C', 'AS', '2H']
ls2 = ['A', 'B', 'C']

def is_sorted(listr):
    if listr == sorted(listr):
        return True
    else:
        return False

print(is_sorted(ls))
print(is_sorted(ls2))

Basically the same as the other answer (but they beat me to it!)

d6stringer
  • 71
  • 1
  • 10