Skip to content

Commit 5ec9059

Browse files
committed
Updated create_problems_list.py.
Updated `.gitignore`.
1 parent 95f06d9 commit 5ec9059

File tree

3 files changed

+67
-35
lines changed

3 files changed

+67
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog][Keep a Changelog], and this project ad
1010

1111
- Updated extension name.
1212
- Updated Changelog.
13+
- Updated `create_problems_list.py`.
14+
- Updated `.gitignore`.
1315

1416
## [0.0.3] - 2021-01-05
1517

static-to-copy/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ build
2828
.classpath
2929
.project
3030
bin
31-
website/problems_list.json
31+
scripts/problems_list.json
3232
scripts/problems_list.md
3333

3434
htmlcov

static-to-copy/scripts/create_problems_list.py

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from bs4 import BeautifulSoup
1111

1212
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"
1414

1515
PROBLEM_ID_STRING = "Id"
1616
PROBLEM_TYPE_STRING = "Type"
@@ -22,6 +22,7 @@
2222
PROBLEM_TAGS_STRING = "Tags"
2323
PROBLEM_LANGUAGES_STRING = "Languages"
2424

25+
2526
def find_files():
2627
"""Return the list of files to process."""
2728
result = {}
@@ -38,9 +39,10 @@ def find_files():
3839
languages = ""
3940
if metadata["type"] == "Coding":
4041
languages = dirs
41-
result[root] = (metadatafile, languages)
42+
result[root] = (metadatafile, languages)
4243
return result
4344

45+
4446
def create_problems_list(files):
4547
"""Creates the list of problems in markdown and json files."""
4648
file_to_update_md = open(PROBLEMS_LIST_FILE_MD, "w+")
@@ -54,41 +56,46 @@ def create_problems_list(files):
5456
count = 0
5557
data_all = []
5658
for item in files.items():
57-
data = {}
58-
count = count+1
59-
data[PROBLEM_ID_STRING] = count
60-
6159
input_file = open(item[1][0], mode="r", encoding="utf-8")
6260
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
6470

65-
problem_type = jsonParsed["type"]
71+
problem_type = json_parsed["type"]
6672
data[PROBLEM_TYPE_STRING] = problem_type
67-
68-
problem_name = jsonParsed["name"]
73+
74+
problem_name = json_parsed["name"]
6975
data[PROBLEM_NAME_STRING] = problem_name
7076

71-
problem_origin = jsonParsed["origin"]["name"]
77+
problem_origin = json_parsed["origin"]["name"]
7278
data[PROBLEM_ORIGIN_STRING] = problem_origin
7379

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, "/")
7682
link = os.path.join(clean_path)
7783
data[PROBLEM_LINK_STRING] = link
7884

79-
companies = filter(None, jsonParsed["companies"])
85+
companies = filter(None, json_parsed["companies"])
8086
companies = list(companies)
8187
companies_string = ", ".join(companies)
8288
data[PROBLEM_COMPANIES_STRING] = list(companies)
8389

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
9097

91-
tags = filter(None, jsonParsed["tags"])
98+
tags = filter(None, json_parsed["tags"])
9299
tags = list(tags)
93100
tags_string = ", ".join(tags)
94101
data[PROBLEM_TAGS_STRING] = list(tags)
@@ -106,31 +113,54 @@ def create_problems_list(files):
106113
text = f"| {count} | {problem_type} | [{problem_name}]({link}) | {problem_origin} | {companies_string} | {categories_string} | {tags_string} | {languages_string} |"
107114
file_to_update_md.write("\n" + text)
108115
print(f"Total problems: {count}")
109-
json_data = json.dumps(data_all)#, indent=2)
116+
json_data = json.dumps(data_all) #, indent=2)
110117
file_to_update_json = open(PROBLEMS_LIST_FILE_JSON, "w+")
111118
file_to_update_json.write(json_data)
112119
file_to_update_json.close()
113120
file_to_update_md.close()
114121

115-
def getCategories(categories, result, category, level):
122+
123+
def get_all_categories(categories, all_categories):
124+
result = []
116125
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)
128134
return result
129135

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+
130159
def main():
131160
"""main method."""
132161
files = find_files()
133162
create_problems_list(files)
134163

164+
135165
if __name__ == '__main__':
136166
main()

0 commit comments

Comments
 (0)