-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_db.py
56 lines (44 loc) · 1.91 KB
/
create_db.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
48
49
50
51
52
53
54
55
56
import os
import sqlite3
# paths to directories
problems_dir = "./problems/processed_texts"
solutions_dir = "./solutions"
database_path = "cpr.db"
# function to create and populate the database
def create_database():
# connect to SQLite database (it creates the file if it doesn't exist)
conn = sqlite3.connect(database_path)
cursor = conn.cursor()
# create table
cursor.execute('''
CREATE TABLE IF NOT EXISTS problem_solution_pairs (
id INTEGER PRIMARY KEY,
problems TEXT NOT NULL,
solutions TEXT NOT NULL
)
''')
# get list of problem files in the directory
problem_files = sorted(os.listdir(problems_dir)) # get all files and sort them
for problem_file in problem_files:
if problem_file.endswith(".txt"): # ensure it's a text file
problem_id = os.path.splitext(problem_file)[0] # extract file name without extension
# read problem text
with open(os.path.join(problems_dir, problem_file), 'r', encoding='utf-8') as pf:
problem_text = pf.read()
# read corresponding solution text
solution_file = f"id{problem_id}.py"
solution_path = os.path.join(solutions_dir, solution_file)
if os.path.exists(solution_path):
with open(solution_path, 'r', encoding='utf-8') as sf:
solution_text = sf.read()
# insert into the database
cursor.execute('''
INSERT INTO problem_solution_pairs (id, problems, solutions)
VALUES (?, ?, ?)
''', (problem_id, problem_text, solution_text))
# commit and close connection
conn.commit()
conn.close()
print(f"Database created and populated successfully at {database_path}")
if __name__ == "__main__":
create_database()