Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename utils.py, add solve_puzzles.py, small refactor
- Loading branch information
Philip Shemella
committed
May 13, 2019
1 parent
dc67143
commit 39d95a8
Showing
6 changed files
with
158 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
|
||
''' solve (cheat) puzzles ''' | ||
|
||
import params | ||
import utils | ||
|
||
import os | ||
import sys | ||
|
||
def solve(p): | ||
|
||
print ('letters:', p.get('letters',None)) | ||
print ('total_score:', p.get('total_score','')) | ||
print ('word_count:', p.get('word_count','')) | ||
print ('pangram(s):', ', '.join(p.get('pangram_list',[]))) | ||
print () | ||
|
||
# print all answers | ||
for x in p.get('word_list',[]): | ||
score = x.get('score') | ||
|
||
# add 7 points if word is pangram | ||
if x.get('word') in p.get('pangram_list',[]): | ||
score += + 7 | ||
utils.print_table((x.get('word'),score), 2, 10) | ||
|
||
return | ||
|
||
def main(): | ||
|
||
# try to read an existing or new puzzle from command line (not required) | ||
try: | ||
puzzle_idx = sys.argv[1].strip().upper() | ||
except: | ||
puzzle_idx = None | ||
|
||
if puzzle_idx is not None: | ||
|
||
# check validity of letters | ||
utils.check_letters(puzzle_idx) | ||
|
||
# choose standard sorting for all puzzle file names | ||
puzzle_idx = utils.sort_letters(puzzle_idx) | ||
|
||
# select puzzle, generate it if it doesn't exist | ||
puzl_path = utils.select_puzzle(puzzle_idx) | ||
|
||
# load json puzzle data | ||
puzl = utils.read_puzzle(puzl_path) | ||
|
||
# solve puzzle (cheat mode) | ||
solve(puzl) | ||
|
||
if __name__ == "__main__": | ||
|
||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python | ||
|
||
''' helper functions that are used between different parts of the code ''' | ||
|
||
import params | ||
import generate_puzzles | ||
|
||
import os | ||
import random | ||
import json | ||
import glob | ||
|
||
# check validity of provided letters | ||
def check_letters(pzl): | ||
|
||
if len(pzl) != len(list(set(pzl))): | ||
print('Invalid count of letters requested.') | ||
print('Exiting...') | ||
exit(0) | ||
|
||
elif len(pzl) != params.TOTAL_LETTER_COUNT: | ||
print('Invalid count of letters requested.') | ||
print('Exiting...') | ||
exit(0) | ||
else: | ||
return | ||
|
||
# smart sorting of letters, keeping first letter first | ||
def sort_letters(pzl): | ||
return pzl[0] + ''.join(sorted(pzl[1:])) | ||
|
||
|
||
def select_puzzle(puzl_idx=None): | ||
|
||
puzzles = glob.glob(params.PUZZLE_DATA_PATH + os.sep + '*.json') | ||
puzl_idx_list = [x.split(os.sep)[-1].split('.')[0] for x in puzzles] | ||
|
||
# scenario 1: no selection made, so return a random puzzle | ||
if puzl_idx is None: | ||
# return a random puzzle | ||
puzl_path = random.choice(puzzles) | ||
print ('You selected a random puzzle:',puzl_path) | ||
return puzl_path | ||
|
||
if len(puzl_idx) != params.TOTAL_LETTER_COUNT: | ||
print ('Puzzles must be ',str(params.TOTAL_LETTER_COUNT),'letters long. Please try again.') | ||
exit(0) | ||
|
||
# scenario 2: specific puzzle requested but not already available | ||
if puzl_idx in puzl_idx_list: | ||
print('Existing puzzle will be played:',puzl_idx) | ||
puzl_path = params.PUZZLE_DATA_PATH + os.sep + puzl_idx + '.json' | ||
# scenario 3: create a new puzzle because an existing one was not found | ||
else: | ||
puzl_idx = generate_puzzles.main(puzl_idx) | ||
print ('You created a new puzzle:',puzl_idx) | ||
puzl_path = params.PUZZLE_DATA_PATH + os.sep + puzl_idx + '.json' | ||
|
||
return puzl_path | ||
|
||
def read_puzzle(puzl_path): | ||
|
||
with open(puzl_path,'r') as fp: | ||
puzzles = json.load(fp) | ||
|
||
#print(len(puzzles.get('letters'),'total puzzle(s) were loaded') | ||
|
||
return puzzles | ||
|
||
def print_table(data, cols, wide): | ||
'''Prints formatted data on columns of given width.''' | ||
# https://stackoverflow.com/a/50215584/2327328 | ||
n, r = divmod(len(data), cols) | ||
pat = '{{:{}}}'.format(wide) | ||
line = '\n'.join(pat * cols for _ in range(n)) | ||
last_line = pat * r | ||
print(line.format(*data)) | ||
#print(last_line.format(*data[n*cols:])) |