Creating a program that displays true if three words are entered in the dictionary

I am trying to create a program that asks the user for three words and displays "True" if the words are entered in dictionary order. For example:

Enter first word: chicken Enter second word: fish Enter third word: zebra True 

Here is my code:

 first = (input('Enter first word: ')) second = (input('Enter second word: ')) third = (input('Enter third word: ')) s = ['a','b','c','d','e','f','g','h', 'i','j','k','l','m','n','o','p', 'q','r','s','t','u','v','w','x', 'y','z','A','B','C','D','E','F', 'G','H','I','J','K','L','M','N', 'O','P','Q','R','S','T','U','V', 'W','Z','Y','Z'] if s.find(first[0]) > s.find(second[0]) and s.find(second[0]) > s.find(third[0]): print(True) 
+7
python string order lexicographic
source share
3 answers

If you are working on a list of arbitrary lengths, I think sorted() , as the other answers indicate, is good for small lists (with small lines) when it comes to large lists and large lines and cases (and cases where the list can be randomly ordered), a faster way is to use the all() built-in function and the generator expression (this should be faster than sorted() ). Example -

 #Assuming list is called lst print(all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1))) 

Note that the above will eventually call str.lower() for each row (except the first and last) twice. If your lines are not very large, this should be fine.

If your lines are really very large compared to the length of the list, you can create another temporary list before doing all() , which stores all the lines in lower case. And then run the same logic on this list.

You can create your list (using user input) using list comprehension, Example -

 lst = [input("Enter word {}:".format(i)) for i in range(3)] #Change 3 to the number of elements you want to take input from user. 

Results of synchronization of the specified method vs sorted() (modified sorted() code for working without case) -

 In [5]: lst = ['{:0>7}'.format(i) for i in range(1000000)] In [6]: lst == sorted(lst,key=str.lower) Out[6]: True In [7]: %timeit lst == sorted(lst,key=str.lower) 1 loops, best of 3: 204 ms per loop In [8]: %timeit all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1)) 1 loops, best of 3: 439 ms per loop In [11]: lst = ['{:0>7}'.format(random.randint(1,10000)) for i in range(1000000)] In [12]: %timeit lst == sorted(lst,key=str.lower) 1 loops, best of 3: 1.08 s per loop In [13]: %timeit all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1)) The slowest run took 6.20 times longer than the fastest. This could mean that an intermediate result is being cached 100000 loops, best of 3: 2.89 ยตs per loop 

Result -

In cases that should return True (these are already sorted lists), using sorted() is much faster than all() , since sorted() works great for lists sorted for the most part.

For random cases, all() works better than sorted() due to a short circuit all() (it will short circuit when it sees the first False ).

In addition, there is the fact that sorted() will create a temporary (sorted list) in memory (for comparison), while all() will not require this (and this fact applies to the timings that we see above).


An earlier answer, which is direct (and applies only to this question), you can simply directly compare strings as such, you do not need another line / list for alphabets. Example -

 first = (input('Enter first word: ')) second = (input('Enter second word: ')) third = (input('Enter third word: ')) if first <= second <= third: print(True) 

Or, if you want to compare only the first characters (although I really doubt it) -

 if first[0] <= second[0] <= third[0]: print(True) 

To compare case insensitive strings, you can convert the entire string to lowercase before comparing. Example -

 if first.lower() <= second.lower() <= third.lower(): print(True) 

Or easier -

 print(first.lower() <= second.lower() <= third.lower()) 
+8
source share

Yes, lists do not have a find method. Although you do not even need to use lists. The operator <= (and also >= ) compares sequences lexicographically. In addition, Python supports coordinated comparisons. Here's how I write it:

 first = input('Enter first word: ') second = input('Enter second word: ') third = input('Enter third word: ') print(first <= second <= third) 

If there are more than three words, you must collect them into a list, sort and compare it with the original list. Example:

 words = input('Enter words (separated by whitespace): ').split() print(sorted(words) == words) # True if sorted() didn't re-order anything 

Both of these approaches will work well enough for a small number of words. If the word count is large, you should use a short circuit solution using the built-in all function and generator expression:

 prev_it, cur_it = iter(words), iter(words) # Consume first element next(cur_it) # Pairwise iteration print(all(prev <= cur for prev, cur in zip(prev_it, cur_it))) 

which is an effective generalization of the first solution.


If you want to make a case insensitive comparison, use str.lower (or str.casefold , in Python str.casefold ).

Example for the first code snippet:

 print(first.lower() <= second.lower() <= third.lower()) 

Example for list-based approaches:

 words = [s.lower() for s in input('Enter words (separated by whitespace): ').split()] 
+6
source share

Save the words to list , and then check it with sorted() . You can do this by ignoring case by specifying a key that looks at the lower case of each word for comparison:

 words = input("Enter three words, separated by spaces: ").split() print(words == sorted(words, key=str.lower)) 
+3
source share

All Articles