blob: b7aadfd3b0838942f13ba82092774237d345d92f (
plain)
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
|
#!/usr/bin/env python3
#
#
# get-in-line.py
#
# Ordered Letters
# Submitted by: E. Choroba
#
# Given a word, you can sort its letters alphabetically (case
# insensitive). For example, “beekeeper” becomes “beeeeekpr” and
# “dictionary” becomes “acdiinorty”.
#
# Write a script to find the longest English words that don’t change
# when their letters are sorted.
#
#
#
# © 2021 colin crain
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
dictFile = '/usr/share/dict/words'
def isSorted(word):
if len(word) < 3:
return False
for i in range(1, len(word)):
if word[i-1] > word[i]:
return False
return True
bag = {}
f = open(dictFile, "r")
for line in f:
line = line.rstrip('\r\n').lower()
if isSorted(line):
bag[line] = len(line)
f.close
maxLen = max( v for v in bag.values() )
longWords = [ w for w in bag if len(w) == maxLen ]
print("longest word length", maxLen, "letters", "\n")
for word in longWords:
print(word)
|