-
-
Save anurag-7/dfc1a75cce67717e7e38573f087d7fe2 to your computer and use it in GitHub Desktop.
Finding the longest word which is still a word with a character removed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
# Dataset used https://github.com/dwyl/english-words/blob/master/words_dictionary.json | |
with open(r'path/to/words_dictionary.json') as f: | |
words = json.load(f) | |
longest_len = 0 | |
longest_word = "" | |
for word in words: | |
length = len(word) | |
if all(((word[:idx] + word[idx + 1:]) in words) for idx in range(length)) and length > longest_len: | |
longest_len = length | |
longest_word = word | |
print(word) | |
print("Word: ", longest_word) | |
print("Length: ", longest_len) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment