Skip to content

Instantly share code, notes, and snippets.

@kyunghyuncho
Created November 7, 2024 20:19
Show Gist options
  • Save kyunghyuncho/e24e34eb8450f5d373f9591db0048161 to your computer and use it in GitHub Desktop.
Save kyunghyuncho/e24e34eb8450f5d373f9591db0048161 to your computer and use it in GitHub Desktop.
# download `https://github.com/dwyl/english-words/raw/refs/heads/master/words_alpha.txt`
# and save it as `words_alpha.txt` in the same directory as this notebook
import os
import requests
if not os.path.exists('words_alpha.txt'):
url = 'https://github.com/dwyl/english-words/raw/refs/heads/master/words_alpha.txt'
r = requests.get(url, allow_redirects=True)
open('words_alpha.txt', 'wb').write(r.content)
# read the words from the file
with open('words_alpha.txt') as f:
words = f.read().splitlines()
# this function counts the number of a given letter in a word
def count_letter(word, letter):
return word.count(letter)
# # let's test this function
# word = 'hello'
# letter = 'l'
# print(count_letter(word, letter)) # should print 2
# this function finds the words that have the most number of a given letter from a given word list
def find_most_letter(words, letter):
max_count = 0
max_words = []
for word in words:
count = count_letter(word, letter)
if count > max_count:
max_count = count
max_words = [word]
elif count == max_count:
max_words.append(word)
return max_words
# # let's test this function
# letter = 'l'
# print(find_most_letter(words[:100], letter))
# i want to find the words that have the most number of "h" in them.
# once the words are found, print them one line at a time with the number of "h" in them next to them.
while True:
letter = input("Enter a letter: ")
if letter.strip().lower() == "exit":
break
if not letter.isalpha():
print("Please enter a valid letter.")
continue
max_words = find_most_letter(words, letter)
for word in max_words:
print(word, count_letter(word, letter))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment