For example, the initial list L=["by","article","circle","for","line","dance","effort"], and I want to take out the["article","circle","for","line"] as the result. Because the first alpha in L is (b,a,c,f,l,d,e) and the consequence of (a,c,f,l)is the longer than (d,e), so the result is ["article","circle","for","line"]. How to fix my code? I'm new so I'm so confusing with sorted() and nested loop.
def find_longest_consisting(test_case):
if not test_case:
return []
result = []
for i in range(len(test_case)):
for j in range(i + len(result) + 1):
substring = test_case[i:j]
if len(substring) != (j - i):
break
if sorted(substring) == list(substring):
result = substring
return result
test_cases = ["by","article","circle","for","line","dance","effort"]
test_result = find_longest_consisting(test_cases)
print(test_result)