For a research paper, I am simulating rounds of the Channel 4 Program Countdown's letters round. Essentially, a board of 9 random letters are put up, and players must try and find words made up of the letters given of as high length as they can.
I'm currently using the code:
for word in sowpods:
testboard = list(board)
count = 0
for letter in word:
if letter in testboard:
testboard.remove(letter)
count += 1
if len(word) == count:
length.append(len(word))
if len(length) == 0:
return 0
return max(length)
to create a list of dictionary words (a SOWPODS .txt file imported into the kernel) that can be created from the board, and then returning the longest word possible for statistical analysis (this is part of a function that simulates a round of Countdown). But, from what I can imagine is due to the repeated for loops, if statements and list edits, this is extremely slow when scaled up to larger samples of 500 or so boards. Is there a more efficient way to check if a word can be created from the board?
I tried using sets; my original test was:
set(word) <= set(board)
but this method ignored repeats, so for example if the board was [a, e, h, s, l,..) it would count "Hassle" as a possible word despite only one S being available. Ideally I'd love a datatype halfway between a list and a set, where order doesn't matter but number of the same element does, but that doesn't seem to exist.