Python Program to check whether Characters of all string elements are in lexical order or not


Lexical order refers to the order of characters or strings based on their dictionary or alphabetical order. In lexical order, characters are arranged in the same way they would be in a dictionary. The comparison is done based on the numerical values of the characters in their respective character sets such as ASCII or Unicode.

In lexical order, characters are compared based on their ASCII or Unicode values from left to right. The character with a lower ASCII or Unicode value comes before the one with a higher value. For example, in ASCII order, 'a' comes before 'b', 'b' comes before 'c', and so on.

When comparing strings, lexical order is determined by comparing the corresponding characters of the strings from left to right. If the first character of one string is greater than the corresponding character of the other string, it is considered greater in lexical order. If the first characters are the same, the second characters are compared, and so on until a difference is found or one string ends.

There are several ways to check whether characters of all string elements are in lexical order or not.

Using all() function

The all() function is a built-in Python function that returns True if all elements in an iterable are considered True, and otherwise returns False. It takes an iterable as its argument and evaluates the truthiness of each element in the iterable.

The following are the key points which resembles the working of the all() function.

  • It takes an iterable as its argument, such as a list, tuple, set, or any other iterable object.

  • It iterates over each element in the iterable.

  • If all elements are considered True in a Boolean context, the function returns True.

  • If any element is considered False in a Boolean context, the function returns False.

  • If the iterable is empty, the function returns True since there are no elements to evaluate.

Example

In this approach, we use a list comprehension and the all() function to iterate through the list of strings. The all() function returns True only if all the elements in the iterable are True. Inside the list comprehension, we compare each string with the next string using the <= operator, which checks for lexical order. If any comparison is False, the all() function will return False.

def check_lexical_order(strings):
   return all(strings[i] <= strings[i+1] for i in range(len(strings)-1))
words = ['apple', 'banana', 'cherry', 'date']
result = check_lexical_order(words)
print(result)

Output

True

Using the sorted() function

The sorted() function is a built-in Python function that returns a new sorted list from the elements of an iterable. It takes an iterable as its argument and returns a new list containing the elements of the iterable in ascending order.

The following are the key points how the sorted() function works.

  • It takes an iterable as its first argument, such as a list, tuple, set, or any other iterable object.

  • It creates a new list by iterating over the elements of the iterable.

  • It compares the elements using their default ordering or a custom key function if provided.

  • It returns the new list with the elements sorted in ascending order.

Example

In this approach, we use the sorted() function to create a new list, sorted_strings, which contains the strings sorted in lexical order. Then, we compare this sorted list with the original list of strings using the == operator. If the two lists are equal, it means that the characters of all string elements are in lexical order.

def check_lexical_order(strings):
   sorted_strings = sorted(strings)
   return sorted_strings == strings
words = ['apple', 'banana', 'cherry', 'date']
result = check_lexical_order(words)
print(result)

Output

True

Updated on: 02-Aug-2023

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements