-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.py
68 lines (57 loc) · 2.29 KB
/
questions.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
57
58
59
60
61
62
63
64
65
66
67
68
import os
class QuestionMatcher:
def __init__(self, text_file, programs_directory):
self.text_file = text_file
self.programs_directory = programs_directory
self.text_content = self.read_text_from_text()
self.c_programs = self.programs()
def read_text_from_text(self):
with open(self.text_file, "r") as file:
text = file.readlines()
return text
def questions(self):
question_arr = []
for i in range(len(self.text_content)):
if "." in self.text_content[i]:
question_arr.append(self.text_content[i])
return question_arr
def extract_numeric_prefix(self, program_name):
try:
return int(program_name.split('_')[0])
except ValueError:
return 0
def programs(self):
programs_list = os.listdir(self.programs_directory)
c_programs = []
for i in range(len(programs_list)):
if f"{i}_" and ".c" in programs_list[i]:
c_programs.append(programs_list[i])
c_programs = sorted(c_programs, key=self.extract_numeric_prefix)
return c_programs
def match_questions_with_programs(self):
question_num = len(self.c_programs)
return self.questions()[:question_num]
def matched_questions(self):
matched_questions = self.match_questions_with_programs()
ques_arr = []
for questions in matched_questions:
ques_arr.append(questions.split("\n")[0])
return ques_arr
def programs_paths(self):
programs_paths = []
for i in range(len(self.c_programs)):
programs_paths.append(os.path.join(self.programs_directory, self.c_programs[i]))
return programs_paths
def programs_names(self):
programs_names = []
for i in range(len(self.c_programs)):
programs_names.append(self.c_programs[i].split(".")[0])
return programs_names
if __name__ == "__main__":
text_file_path = "data/Questions.txt"
programs_directory_path = "C_programs"
matcher = QuestionMatcher(text_file_path, programs_directory_path)
matched_questions = matcher.match_questions_with_programs()
print(matcher.programs_paths())
print(matched_questions)
print(matcher.programs_names())