Skip to content

Instantly share code, notes, and snippets.

@popey456963
Last active December 10, 2016 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save popey456963/7227c374eea03d7bf7d358c9ad00a84c to your computer and use it in GitHub Desktop.
Save popey456963/7227c374eea03d7bf7d358c9ad00a84c to your computer and use it in GitHub Desktop.
[2016-12-09] Challenge #294 [Intermediate] Rack management 2 (Bonus 1)
# [2016-12-09] Challenge #294 [Intermediate] Rack management 2
# https://www.reddit.com/r/dailyprogrammer/comments/5h40ml/20161207_challenge_294_intermediate_rack/
import copy, random, timeit
words = [[line.strip(), 0] for line in open("enable1.txt") if len(line.strip()) < 11]
tilescores = dict(zip("abcdefghijklmnopqrstuvwxyz?", [1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10,0]))
char_freq = {'a': 22, 'c': 15, 'b': 9, 'e': 25, 'd': 16, 'g': 13, 'f': 7, 'i': 23, 'h': 10, 'k': 6, 'j': 1, 'm': 11, 'l': 17, 'o': 19, 'n': 20, 'q': 0, 'p': 12, 's': 24, 'r': 21, 'u': 14, 't': 18, 'w': 5, 'v': 4, 'y': 8, 'x': 2, 'z': 3}
def calcScore(word):
return sum(j * tilescores[char] for j, char in enumerate(word, 1))
def sortWord(word):
word = "".join([p[0] for p in sorted([[letter, char_freq[letter]] for letter in word], key=lambda x: x[1])])
return word
def bestWord(hand):
for word in words:
temp_hand = hand[:]
yes = True
for letter in word[2]:
if letter in temp_hand:
temp_hand = temp_hand.replace(letter, "", 1)
continue
else:
yes = False
break
if yes:
return word
if __name__ == "__main__":
for index, item in enumerate(words):
words[index][1] = calcScore(item[0])
words[index].append(sortWord(item[0]))
words = sorted(words, key=lambda x: x[1], reverse=True)
print(bestWord("iogsvooely"))
print(bestWord("seevurtfci"))
print(bestWord("vepredequi"))
test = [''.join(random.choice("abcdefghijklmnopqrstuvwxyz?") for x in range(20)) for x in range(1000)]
mainCounter = 0
start = timeit.default_timer()
for i in test:
bestWord(i)
print(timeit.default_timer() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment