10
10
from bs4 import BeautifulSoup
11
11
12
12
PROBLEMS_LIST_FILE_MD = "scripts/problems_list.md"
13
- PROBLEMS_LIST_FILE_JSON = "website /problems_list.json"
13
+ PROBLEMS_LIST_FILE_JSON = "scripts /problems_list.json"
14
14
15
15
PROBLEM_ID_STRING = "Id"
16
16
PROBLEM_TYPE_STRING = "Type"
22
22
PROBLEM_TAGS_STRING = "Tags"
23
23
PROBLEM_LANGUAGES_STRING = "Languages"
24
24
25
+
25
26
def find_files ():
26
27
"""Return the list of files to process."""
27
28
result = {}
@@ -38,9 +39,10 @@ def find_files():
38
39
languages = ""
39
40
if metadata ["type" ] == "Coding" :
40
41
languages = dirs
41
- result [root ] = (metadatafile , languages )
42
+ result [root ] = (metadatafile , languages )
42
43
return result
43
44
45
+
44
46
def create_problems_list (files ):
45
47
"""Creates the list of problems in markdown and json files."""
46
48
file_to_update_md = open (PROBLEMS_LIST_FILE_MD , "w+" )
@@ -54,41 +56,46 @@ def create_problems_list(files):
54
56
count = 0
55
57
data_all = []
56
58
for item in files .items ():
57
- data = {}
58
- count = count + 1
59
- data [PROBLEM_ID_STRING ] = count
60
-
61
59
input_file = open (item [1 ][0 ], mode = "r" , encoding = "utf-8" )
62
60
text = input_file .read ()
63
- jsonParsed = json .loads (text )
61
+ json_parsed = json .loads (text )
62
+
63
+ if 'skip_for_problems_list' in json_parsed and json_parsed [
64
+ 'skip_for_problems_list' ] == "True" :
65
+ continue
66
+
67
+ data = {}
68
+ count = count + 1
69
+ data [PROBLEM_ID_STRING ] = count
64
70
65
- problem_type = jsonParsed ["type" ]
71
+ problem_type = json_parsed ["type" ]
66
72
data [PROBLEM_TYPE_STRING ] = problem_type
67
-
68
- problem_name = jsonParsed ["name" ]
73
+
74
+ problem_name = json_parsed ["name" ]
69
75
data [PROBLEM_NAME_STRING ] = problem_name
70
76
71
- problem_origin = jsonParsed ["origin" ]["name" ]
77
+ problem_origin = json_parsed ["origin" ]["name" ]
72
78
data [PROBLEM_ORIGIN_STRING ] = problem_origin
73
79
74
- pathSeparator = os .sep
75
- clean_path = os .path .normpath (item [0 ]).replace (pathSeparator , "/" )
80
+ path_separator = os .sep
81
+ clean_path = os .path .normpath (item [0 ]).replace (path_separator , "/" )
76
82
link = os .path .join (clean_path )
77
83
data [PROBLEM_LINK_STRING ] = link
78
84
79
- companies = filter (None , jsonParsed ["companies" ])
85
+ companies = filter (None , json_parsed ["companies" ])
80
86
companies = list (companies )
81
87
companies_string = ", " .join (companies )
82
88
data [PROBLEM_COMPANIES_STRING ] = list (companies )
83
89
84
- categories = filter (None , jsonParsed ["categories" ])
85
- categories = getCategories (list (categories ), [], "" , 0 )
86
- categories = filter (None , categories )
87
- categories = list (categories )
88
- categories_string = ", " .join (categories )
89
- data [PROBLEM_CATEGORIES_STRING ] = categories
90
+ categories = filter (None , json_parsed ["categories" ])
91
+ all_categories = []
92
+ result = get_all_categories (list (categories ), all_categories )
93
+ result = filter (None , result )
94
+ result = list (result )
95
+ categories_string = ", " .join (result )
96
+ data [PROBLEM_CATEGORIES_STRING ] = result
90
97
91
- tags = filter (None , jsonParsed ["tags" ])
98
+ tags = filter (None , json_parsed ["tags" ])
92
99
tags = list (tags )
93
100
tags_string = ", " .join (tags )
94
101
data [PROBLEM_TAGS_STRING ] = list (tags )
@@ -106,31 +113,54 @@ def create_problems_list(files):
106
113
text = f"| { count } | { problem_type } | [{ problem_name } ]({ link } ) | { problem_origin } | { companies_string } | { categories_string } | { tags_string } | { languages_string } |"
107
114
file_to_update_md .write ("\n " + text )
108
115
print (f"Total problems: { count } " )
109
- json_data = json .dumps (data_all )#, indent=2)
116
+ json_data = json .dumps (data_all ) #, indent=2)
110
117
file_to_update_json = open (PROBLEMS_LIST_FILE_JSON , "w+" )
111
118
file_to_update_json .write (json_data )
112
119
file_to_update_json .close ()
113
120
file_to_update_md .close ()
114
121
115
- def getCategories (categories , result , category , level ):
122
+
123
+ def get_all_categories (categories , all_categories ):
124
+ result = []
116
125
for item in categories :
117
- if level == 0 :
118
- category = ""
119
- if len (category ) > 0 :
120
- category = category + " -> " + item ["name" ] if len (item ["name" ]) > 0 else ""
121
- else :
122
- category = item ["name" ] if len (item ["name" ]) > 0 else ""
123
- children = item ["children" ]
124
- if len (children ) > 0 :
125
- getCategories (children , result , category , level + 1 )
126
- else :
127
- result .append (category )
126
+ categories = get_categories (item )
127
+ if len (categories ) > 0 :
128
+ all_categories .append (categories )
129
+ for item in all_categories :
130
+ for category in item :
131
+ if len (category ) > 0 :
132
+ str_temp = ' -> ' .join (category )
133
+ result .append (str_temp )
128
134
return result
129
135
136
+
137
+ def get_categories (current_item ):
138
+ all_paths = []
139
+ get_categories_recursive (current_item , [], all_paths )
140
+ return all_paths
141
+
142
+
143
+ def get_categories_recursive (current_item , current_category , all_categories ):
144
+ if current_item == None or len (current_item ) == 0 :
145
+ return
146
+ if 'name' in current_item and len (current_item ['name' ]) > 0 :
147
+ current_category .append (current_item ['name' ])
148
+ # Reached the end.
149
+ if 'children' not in current_item or ('children' in current_item and len (
150
+ current_item ['children' ]) == 0 ) and len (current_category ) > 0 :
151
+ all_categories .append (list (current_category ))
152
+ elif 'children' in current_item and len (current_item ['children' ]) > 0 :
153
+ for child in current_item ['children' ]:
154
+ get_categories_recursive (child , current_category , all_categories )
155
+ if len (current_category ) > 0 :
156
+ del current_category [- 1 ]
157
+
158
+
130
159
def main ():
131
160
"""main method."""
132
161
files = find_files ()
133
162
create_problems_list (files )
134
163
164
+
135
165
if __name__ == '__main__' :
136
166
main ()
0 commit comments