Skip to content

Instantly share code, notes, and snippets.

/main.py Secret

Created December 25, 2016 05:04
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 anonymous/937120fe7db14cc3c57158243c2a72a6 to your computer and use it in GitHub Desktop.
Save anonymous/937120fe7db14cc3c57158243c2a72a6 to your computer and use it in GitHub Desktop.
import re
SCORE_MAP = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4,
'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1,
'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1,
's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8,
'y': 4, 'z': 10, '?': 0}
def main() -> None:
file_name = 'input/inputs'
file = open(file_name)
# Main Challenge + Bonus #1
for line in file:
words = line.split(' ')
tile_set = re.sub("[^a-z0-9?]+", "", words[0], flags=re.IGNORECASE)
word_to_make = re.sub("[^a-z0-9]+", "", words[1], flags=re.IGNORECASE)
print('scramble(' + str(tile_set) + ', ' + word_to_make + '): '
+ str(can_make_word(word_to_make, tile_set) != -1))
print()
# Bonus #2
# valid_words = []
# for line in file:
# tile_set = re.sub("[^a-z0-9?]+", "", line, flags=re.IGNORECASE)
# for english_word in open('input/wordlist.txt'):
# good_word = re.sub("[^a-z0-9?]+", "", english_word, flags=re.IGNORECASE)
# if can_make_word(good_word, tile_set) != -1:
# valid_words.append(good_word)
# valid_words = sorted(valid_words, key=len)
# print('longest(\"' + tile_set + '") -> \"' + valid_words[-1] + '\"')
# valid_words = []
# Bonus #3
# scores_dict = {}
# for line in file:
# tile_set = re.sub("[^a-z0-9?]+", "", line, flags=re.IGNORECASE)
# for english_word in open('input/wordlist.txt'):
# good_word = re.sub("[^a-z0-9?]+", "", english_word, flags=re.IGNORECASE)
# score = can_make_word(good_word, tile_set)
# if score != -1:
# scores_dict[score] = good_word
# sorted_scores = sorted(scores_dict.keys())
# largest_sum_word = scores_dict[sorted_scores[-1]]
# print('highest(\"' + line + '\") -> \"' + largest_sum_word + '\"')
# scores_dict = {}
def can_make_word(word_to_make: str, tile_set: str):
final_score = 0
tiles = []
for char in tile_set:
tiles.append(char)
blank_tiles = []
for tile in tiles:
if tile == '?':
blank_tiles.append('?')
while '?' in tiles:
tiles.remove('?')
for char in word_to_make:
if char in tiles:
tiles.remove(char)
final_score += SCORE_MAP[char]
else:
if len(blank_tiles) > 0:
blank_tiles.remove('?')
else:
return -1
return final_score
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment