Skip to content

Instantly share code, notes, and snippets.

@Moderrek
Created January 1, 2024 21:05
Show Gist options
  • Save Moderrek/8cda5dd535aaaa6fbd45aa4ec67f5ca5 to your computer and use it in GitHub Desktop.
Save Moderrek/8cda5dd535aaaa6fbd45aa4ec67f5ca5 to your computer and use it in GitHub Desktop.
Finds longest word possible to write on 7 segment display with txt dictionary.
import urllib.request
dictionaries: dict[str, str] = {
"pl": "https://raw.githubusercontent.com/sigo/polish-dictionary/master/dist/pl.txt",
"en": "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt",
}
_7seg_possible_chars: str = "0123456789abcdefhijlnoprstuy"
def pick_dict(choice: str) -> str:
if choice in dictionaries:
return dictionaries[choice]
return choice
def get_lower_words(url: str) -> list[str]:
with urllib.request.urlopen(url) as stream:
return stream.read().decode("UTF-8").lower().split('\n')
def find_longest(words: list[str], allowed_chars: str = _7seg_possible_chars) -> list[str]:
longest: list[str] = []
for word in words:
if len(longest) > 0:
if len(longest[0]) > len(word):
continue
valid = True
for char in word:
if char not in _7seg_possible_chars:
valid = False
break
if valid:
if len(longest) > 0 and len(longest[0]) == len(word):
longest.append(word)
else:
longest.clear()
longest.append(word)
return longest
def main():
chosen = input("Select a dictionary or enter a URL (EN/PL/URL) > ").lower()
word_dict = pick_dict(chosen)
print(f"Downloading {word_dict}...")
words = get_lower_words(word_dict)
print("Successfully downloaded!")
print(f"Found {len(words)} words!")
print("Checking longest word on 7-segment display...")
longest = find_longest(words)
print("These are found:")
for i, v in enumerate(longest):
print(f" {i + 1}. {v}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment