import itertools from collections import OrderedDict wordDict = set(['abc' , 'abcc' , 'def' , 'zxc' , 'abcd' ]) letScore = {'a': 1, 'b': 2, 'c': 3 , 'd' : 4} letters = ['a','b','c'] newLet = 'd' currMaxWord = '' currMaxScore = 0 #Compute score of a word (Give word as input, Returns int score ) def compScore(word): score = 0 for char in word: score += letScore[char] return score #Create all permutations of words for a list of letters (Input : List of current letters , New Letter. Returns SET of all permutations ) def createPerm(inp, newLet): inp += newLet out = set() #Set because permutations doesn't take care of reps for item in range(0, len(inp)+1): for perm in itertools.permutations(inp, item): out.add("".join(perm)) return out #Find candidates in word dictionary (Imput : All permutations. Returns Maximum scoring word, its score and all dictionary of all candidates) def findCand(letPerm): wordCand = dict() maxScore = 0 maxWord = '' for word in letPerm: if word in wordDict: score = compScore(word) wordCand[word] = score #wordCan has all candidate words if score > maxScore: maxScore = score maxWord = word return maxWord, maxScore, wordCand def bidNow(bid): print('Bidding : ' + str(bid)) letPerm = createPerm(letters, newLet) mw, ms, cand = findCand(letPerm) if (ms==0): #None of permutations are present in dict bidNow(0) elif (ms!=0 and ms>=currMaxScore): #We have a new word which has the highest score till now currMaxWord = mw currMaxScore = ms bidNow(ms-1) else: #None of the new permutations have a higher score than the current max print('Nothing') print cand print(letPerm) print(compScore('abc'))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question