-
Notifications
You must be signed in to change notification settings - Fork 302
/
lexico.py
49 lines (38 loc) · 1.05 KB
/
lexico.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
lexico.py
Donald Knuth, Art of Computer Programming, Volume 4 Facsimile 0
Exercise #30
Each letter of the word "first" appears in correct lexicographic order.
Find the first and last such words in the SGB words.
"""
from get_words import get_words
def in_sorted_order(word):
chars = list(word)
if(str(chars)==str(sorted(chars))):
return True
else:
return False
if __name__=="__main__":
words = get_words()
words = sorted(words)
count = 0
print("-"*40)
print("ALL lexicographically sorted words:")
for word in words:
if(in_sorted_order(word)):
print(word)
count += 1
print("{0:d} total.".format(count))
print("-"*40)
for word in words:
if(in_sorted_order(word)):
print("First lexicographically sorted word:")
print(word)
break
words.reverse()
print("-"*40)
for word in words:
if(in_sorted_order(word)):
print("Last lexicographically sorted word:")
print(word)
break