Given a list with string elements, the task is to write a Python program to return True if all list elements are sorted. The point of caution here is to keep in mind we are comparing characters of a string list element with other characters of the same list element and not string elements with each other.
Examples:
Input : test_list = [“dent”, “flop”, “most”, “cent”] Output : True Explanation : All characters are sorted.
Input : test_list = [“dent”, “flop”, “mist”, “cent”] Output : False Explanation : m > i in mist, hence unordered. So, False is returned.
Method 1 : Using loop and sorted()
In this, we iterate for each String and test if all the Strings are ordered using sorted(), if any string is not sorted/ordered, the result is flagged off.
Example:
# initializing list test_list = ["dent", "flop", "most", "cent"] # printing original list print("The original list is : " + str(test_list)) res = True for ele in test_list: # check for ordered string if ele != ''.join(sorted(ele)): res = False break # printing result print("Are all strings ordered ? : " + str(res))
Output:
The original list is : [‘dent’, ‘flop’, ‘most’, ‘cent’] Are all strings ordered ? : True
Method 2 : Using all() and sorted()
In this, loop is avoided and all the strings to be sorted is checked using all(), which returns True if all the elements return true for certain condition.
Example:
# initializing list test_list = ["dent", "flop", "most", "cent"] # printing original list print("The original list is : " + str(test_list)) # using all() to check all elements to be sorted res = all(ele == ''.join(sorted(ele)) for ele in test_list) # printing result print("Are all strings ordered ? : " + str(res))
Output:
The original list is : [‘dent’, ‘flop’, ‘most’, ‘cent’] Are all strings ordered ? : True
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.