-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadBoard.py
43 lines (38 loc) · 1.32 KB
/
loadBoard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import random
#Citation (Load Board): https://www.cs.cmu.edu/~112-3/notes/tp-sudoku-hints.html
def loadBoardPaths(filters):
boardPaths = [ ]
for filename in os.listdir(f'boards/'):
if filename.endswith('.txt'):
if hasFilters(filename, filters):
boardPaths.append(f'boards/{filename}')
return boardPaths
#Citation (Load Board): https://www.cs.cmu.edu/~112-3/notes/tp-sudoku-hints.html
def hasFilters(filename, filters=None):
if filters == None: return True
for filter in filters:
if filter not in filename:
return False
return True
#Citation (Read File): Code provided in 15-112 Piazza Post
def readFile(path):
with open(path, "rt") as f:
return f.read()
#processing the board to turn it into a list
def lstBoard(s):
board = []
for line in s.splitlines():
numberLst = line.split()
numLst = []
for numStr in numberLst:
numLst.append(int(numStr))
board.append(numLst)
return board
#Citation (Load Board): Code provided in 15-112 Piazza Post
def loadBoard(filters):
filesLst = loadBoardPaths(filters)
filename = random.choice(filesLst)
pathToFile = f'{filename}'
fileContents = readFile(pathToFile)
return lstBoard(fileContents)