-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku_Data_Reader.py
47 lines (36 loc) · 1.5 KB
/
Sudoku_Data_Reader.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
44
45
46
47
# Author: Terence Tang
# Class: CS325 Analysis of Algorithms
# Assignment: Portfolio Project - Sudoku Puzzle Game
# Date: 3/8/2021
# Description: Program for reading a dataset which contains preset sudoku puzzles. Returns random puzzles from the
# dataset as requested.
# Sudoku data provided from Kaggle - https://www.kaggle.com/bryanpark/sudoku
import csv
import random
class Data:
""" Data object which represents sudoku puzzle data provided. Has methods for getting a random puzzle and
solution from the dataset. """
def __init__(self):
self.headers = []
self.puzzles = []
self.solutions = []
self.read_data_csv_file()
def read_data_csv_file(self):
""" Opens and reads the data input csv file and moves info to memory for manipulation """
with open("sudoku_data.csv", 'r', encoding="utf8") as csv_data_file:
csv_reader = csv.reader(csv_data_file)
self.headers = next(csv_reader)
for sudoku_set in csv_reader:
self.puzzles.append(sudoku_set[0])
self.solutions.append(sudoku_set[1])
csv_data_file.close()
def get_sudoku_set(self):
""" Returns a random sudoku puzzle from the dataset. """
num = random.randrange(len(self.puzzles))
return self.puzzles[num], self.solutions[num]
def main():
data = Data()
print(data.headers)
sudoku = data.get_sudoku_set()
print(sudoku)
if __name__ == "__main__": main()