Skip to content

Instantly share code, notes, and snippets.

@asdkant
Created October 10, 2018 14:37
Show Gist options
  • Save asdkant/9019014f26899b23ff2658082516da5d to your computer and use it in GitHub Desktop.
Save asdkant/9019014f26899b23ff2658082516da5d to your computer and use it in GitHub Desktop.
Solution to the "what is the longest word you can write with 7 segments displays" problem from Tom Scott's video
import sys, re
# first argument: the words file
# second argument: the banned characters
wordsfile = sys.argv[1]
banned = sys.argv[2]
# print("words file: " + wordsfile)
# print("banned: " + banned)
# print("")
with open(wordsfile) as f:
lines = [line.rstrip('\n') for line in f.readlines()]
p = re.compile('[^'+banned+']*',re.IGNORECASE)
not_banned = list(filter(lambda x: p.fullmatch(x), lines))
mlen = max(map(len,not_banned))
result = [w for w in not_banned if len(w) == mlen]
for w in result:
print(w)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment