简体   繁体   中英

How to check if list of strings entered are in alphabetical order?

I have a program which takes a series of strings from user input and should print "Yes" if the strings entered are in alphabetical order and "No" if not. The program ends by the user entering an empty input. I can do this when I specify the number of inputs it should have, eg 2:

finished = False
while not finished:
    print("Please enter a string: ")
    s = input()
    x = input()
    if len(s) != 0: 
        if s < x:
            print("Yes")
        else:
            print("No")  
    else:
        finished = True

However I can't seem to get my code to work when there is an indefinite amount of strings that can be entered. My current working method is to append all the strings to a list and perform the checks there, but I'm not sure exactly how to write the if statement to check this:

lst = []
i = range(len(lst))

finished = False
while not finished:
    print("Please enter a string: ")
    s = input()
    lst.append(s)
    if len(s) != 0:
        if lst[i:] < lst[i: - 1]:
            print("Yes")
        else:
            print("No")
    else:
        finished = True

Is there an easy way to achieve this without deviating too far from the intended structure above?

lst is a list of strings, slicing syntax on that will get you a list of strings back. But you want a string instead. Since you're appending to the list, the latest string appended will be present in the [-1] index, and the previous one will be present in [-2] index.

Change lst[i:] < lst[i: - 1] to lst[-2] < lst[-1]

There's still another problem though, in the first iteration lst[-2] does not exist, because there is only one string that has been inputted, to get rid of this - take one input and append it to the list before the loop starts-

print("Please enter a string: ")
s = input()
lst.append(s)
finished = False
while not finished:
    # rest of your code

You can use the following to always compare the new item to the last item in the existing list. Therefore it always checks, if the next item is in order

new_input = input()
existing_list = ...

if sorted(existing_list[-1], new_input)[-1] == new_input
    existing_list.append(new_input)
    print("Yes")
else:
    print("No")

That way, you don't have to enter the value to the list before checking

I have made some changes to your code. There is no need for two inputs and a master list. The code is as below. Assumptions

  • Assumes that items in the list are separated by a single space
  • The difference between a capital case character and small case character is not important. If this is not true and ASCII ordering is important then remove ".lower()" from the third line.
while True:
    print("Please enter a string: ")
    s = input().lower() ## To avoid confusion between sorting ABb Abb and like strings
    if not s: ## If nothing is entered break out of loop
        break
    SplitString = s.split(" ") ##Get elements separated by "single" space into list
    SortedString = " ".join(sorted(SplitString)) ##Sort the list and then join into string
    if s == SortedString: ##If the sorted and joined string is same as original then the string was already sorted when entered
        print("Yes")
    else: ## Else it was not sorted when entered
        print("No")

The output is as below

Please enter a string: 
AAA AAB ABA ABC
Yes
Please enter a string: 
AAA AAB ABC ABA
No
Please enter a string: 
aaa aab aba abc
Yes
Please enter a string: 
aaa aab abc aba
No
Please enter a string: 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM