<em>Bad Code</em>

Longest Seven-Segment Word | <em>Bad Code</em> [4]

What It Does

A program that finds the longest word in the English language that can be represented by a seven-segment display. Watch Tom Scott’s video for more information – this program was made as a response. It turns out there is only one longest word that meets the criteria…

# words.txt from GitHub file linked at the end of Tom's video 
words = open("words.txt","r")
english_words = words.readlines()
words.close()

longest = [""]
allowed = []
not_allowed = ["g","k","m","q","v","w","x","z"]

for i in range(0,len(english_words)):
    test = english_words[i]
    english_words[i] = test
    if any (c in not_allowed for c in test):
        continue
    else:
        allowed.append(test)
        if len(test) > len(longest[0]):
            longest = []
            longest.append(test)
        elif len(test) == len(longest[0]):
            longest.append(test)
        else:
            continue

print(longest)

Output:

[‘dichlorodiphenyltrichloroethane\n’]

Leave a Reply

Your email address will not be published. Required fields are marked *