Countdown Letters Solver

Python 3 program written to solve the countdown letters game.


Contents


The Problem

The countdown letters game involves rearranging a selection of 9 letters into the longest possible word. Each letter in the selection can only be used once, however the selection can contain duplicate letters.

The selection must contain a minimum of 3 consonants and 4 vowels.


The Solution

My solution takes any string input, and will find any words from a given word list that can be made from the characters in that string.

It achieves this by looking through the entire word list, line by line, and for every word that is a valid anagram it appends it to a list.

The results are then sorted by length (descending), and printed.


Examples


Source Code


from collections import Counter

while (True):
	print("Please enter a string: ")
	letters = Counter(input())

	wordList = []

	with open("ukenglish.txt", encoding="Latin-1") as f:
		#Content_list is the list that contains the read lines.
		for line in f:
			line = line.rstrip("\n")
			valid = True
			
			lineCount = Counter(line)
			for x in lineCount:
				if (x in letters):
					if (letters[x] >= lineCount[x]):
						pass
					else:
						valid = False
				else:
					valid = False
			if (valid):
				wordList.append(line)
				
				#print(lineCount)
				#print(letters)
	#print(wordList)

	for i in range (0, len(wordList) - 1):
		for j in range (0, len(wordList) - i - 1):
			#print (wordList[j] + " " + wordList[j + 1])
			if (len(wordList[j]) < len(wordList[j + 1])):
				temp = wordList[j]
				wordList[j] = wordList[j + 1]
				wordList[j + 1] = temp
			
	#print(wordList)

	for word in wordList:
		print (str(len(word)) + ": " + word)

Source Code