python - Find max length word from arbitrary letters -
I have 10 arbitrary letters and need to check the maximum length of the word file
- < Li>
- The first idea that came was using the set: [10 characters] but It also repeats that the character and I do not know how to avoid
-
I tried to learn dragon recently but RE Before and maybe RE is not required and can be solved without it
- "Using this:" the itater looks inappropriate, but it may be easy to do (With whom I am not familiar)
I have started learning RE a while ago, and are unable to find the appropriate pattern
I think the solution is also known for novice programmers / scripts, but do not thank me
I'm guessing that one of the scrabble tiles Magistrate is to discover possible terms given something, so that a letter be repeated several times because it is repeated in the original list.
This trick is to test each character of each word in your word file efficiently against its source letter set for each character, if found in the test set, then test it Remove from and proceed; Otherwise, the word is not a match, and go on to the next word.
Python is a good function to test a set of conditions based on the elements in a sequence all
all
added the feature This will be "short-circuit", that is, no further testing is done when an item condition fails. So if your initial letter of your candidate is 'Z', and there is no 'Z' in your source papers, then there is no point in examining any more letters in the candidate's words.
This shot was simple in my first written:
matches = [] word for the word list: testet = set (letters) if all (in word c In testet: match.append (word) Unfortunately, the bug here is that if there is a 'm' in the source letters, then a word with several 'm is incorrectly matched because every' m ' M 'in different source' reset, so I needed to remove every letter because it was matched. I have taken advantage of the fact that set.remove (item)
does not give any, which Python considers as a boolean false
, And expanded my generator expression used to call all
. For every word, if it is found in the testet, then I want to delete it from the testet (some pseudo-code, not valid python):
all (in testet c And from the "Remove from Testet c" in the word c)
since the set. The revolve returns one, I can change the bit quoted with "no testet.remove (c)", and now I have a valid Python expression:
all (in c Not the testet and the term for testet.remove (c)
Now we just need to wrap it in the loop, which checks each word in the list (checking each word Be sure to create a new test set first, because our all
test is now a disastrous test ):
For the word in the word list: testet = set (letters) if all (in testet c and not for word testet.remove (c)): matches.append ( Word)
Sort fairs with the end time of the final step. We can pass an important function to sort Builtin lane
good, but this length will be sorted by ascending. To convert it to descending sort, we use lambda to not give us the lane
, but -1 * len
:
Matches sorted (key = lambda wd: -len (wd))
Now you can print the longest word in matches [0], or it can be repeated on all matches And can print them.
(I was surprised that the approach of this cruel force goes so well. I used the word list 2of12inf.txt, which contains more than 80,000 words, and for a list of 10 characters , I get back the list of matches in my small 1.99 GHz about 0.8 seconds.)
Comments
Post a Comment