-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
executable file
·140 lines (115 loc) · 4.38 KB
/
gen.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/python
import json
import re
import glob
import os
from typing import TypedDict
# Question type
class Question(TypedDict):
id: int
topic: str
problem_name: str
problem_link: str
difficulty: str
solution_link: str
language: str
# Language type
class Language(TypedDict):
name: str
comment_string_single_line: str
extension: str
location: str
extra_text_after_first_line: str
def getProblemName(filename: str) -> str:
idx = -1
for i, char in enumerate(filename):
if char == "_":
idx = i
break
if idx != -1:
return filename[idx + 1 :].replace("_", " ")
return ""
def sort_file_folders(data: list[str]) -> list[str]:
"""
Sorts a list of directory names alphabetically, considering numeric prefixes.
Args:
data (list[str]): A list of directory names.
Returns:
list[str]: The sorted list of directory names.
"""
def convert(text):
"""Converts a character to its integer value if it's a digit, otherwise converts it to lowercase."""
return int(text) if text.isdigit() else text.lower()
def alphanum_key(key):
"""Creates a tuple of tuples where each inner tuple contains a string and its corresponding integer value."""
return [convert(c) for c in re.split("([0-9]+)", key)]
# Use the custom sorting key to sort the directories
return sorted(data, key=alphanum_key)
def extensionExists(languages: dict[str, Language], extension: str):
for language in languages:
if languages[language]["extension"] == extension:
return True
return False
def buildSolutionLink(language: str, file: str):
base_git_url = "https://raw.githubusercontent.com/glowfi/DS/main/Programs"
return f"{base_git_url}/{language}/{file}"
languages: dict[str, Language] = {
"python": {
"name": "python",
"comment_string_single_line": "#",
"extension": ".py",
"location": os.path.abspath("./Programs/python"),
"extra_text_after_first_line": "",
},
"go": {
"name": "go",
"comment_string_single_line": "//",
"extension": ".go",
"location": os.path.abspath("./Programs/go"),
"extra_text_after_first_line": "\npackage main",
},
}
final_data: list[Question] = []
for language in languages.keys():
base_lang: str = languages[language]["name"]
base_dir_new_lang: str = os.path.abspath(f"./Programs/{base_lang}")
directories: list[str] = sort_file_folders(glob.glob(f"{base_dir_new_lang}/*"))
for directory in directories:
topic: str = os.path.basename(directory)
directoryPath: str = f"{base_dir_new_lang}/{topic}"
files: list[str] = sort_file_folders(glob.glob(f"{directory}/*"))
questionCount: int = 1
for file in files:
name, extension = os.path.splitext(file)
filename = os.path.basename(name)
if extensionExists(languages, extension):
with open(f"{file}") as fp:
first_line = fp.readline()
data = (
first_line.replace(
languages[language]["comment_string_single_line"], ""
)
.strip(" ")
.strip("\n")
).split(",")
if len(data) < 2:
print(f"Skipping {file}")
continue
else:
problem_link, difficulty = data
newQuestion: Question = {
"id": questionCount,
"topic": topic.split("_")[1],
"language": languages[language]["name"],
"difficulty": difficulty.strip(" "),
"problem_name": getProblemName(filename),
"problem_link": problem_link,
"solution_link": buildSolutionLink(
languages[language]["name"],
f"{topic}/{filename}{languages[language]["extension"]}",
),
}
final_data.append(newQuestion)
questionCount += 1
with open("data.json", "w") as data:
data.write(json.dumps(final_data, indent=4))