-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
executable file
·95 lines (81 loc) · 3.14 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
#!/bin/python
import glob
import os
import json
import pandas as pd
# Create Sheet
if os.path.exists("./sheet.csv"):
os.remove("./sheet.csv")
f = open("sheet.csv", "a", encoding="utf-8")
f.write(f"#,Type,Problem,Link,Difficulty,Code\n")
f.close()
PROGRAM_LOCATION = "https://raw.githubusercontent.com/glowfi/DS/main/Programs"
directories = sorted(glob.glob("./Programs/*"))
dic = {}
idx = 0
for directory in directories:
print(f"Parsing {directory} ....")
if os.path.isdir(f"./{directory}"):
files = glob.glob(f"{directory}/*")
currentData = []
for file in files:
file_ext_allowed = file.find("py")
if file_ext_allowed != -1:
with open(f"{file}") as f:
first_line = f.readline()
data = first_line.split(",")
try:
if data[1]:
dataStructureType = file.split("/")[2].split("_")[1]
link = data[0].strip(" ").replace("# ", "")
difficulty = data[1].strip("\n").lstrip(" ").rstrip(" ")
tmp = file.split("/")[-1].strip(" ").replace(".py", "")
extractNumber = tmp.split("_")[0]
problemStatement = " ".join(tmp.split("_")[1:])
loc = "/".join(file.split("/")[2:])
code = f"{PROGRAM_LOCATION}/{loc}"
currentData.append(
[
int(extractNumber),
dataStructureType,
problemStatement,
link,
difficulty,
code,
]
)
except Exception as e:
name = file.split("./")[-1]
print(f"Exception Handled for {name}!")
currentData.sort(key=lambda x: x[0])
# Save dict to work with JSON
dataStructureType = currentData[0][1]
dic[dataStructureType] = currentData
# Create CSV
for (
extractNumber,
dataStructureType,
problemStatement,
link,
difficulty,
code,
) in currentData:
f = open("sheet.csv", "a", encoding="utf-8")
f.write(
f"{idx},{dataStructureType},{problemStatement},{link},{difficulty},{code}\n"
)
idx += 1
f.close()
# Dump JSON
json_object = json.dumps(dic, indent=4)
with open("data.json", "w") as outfile:
outfile.write(json_object)
# Dump Readme
df = pd.read_csv("sheet.csv")
markdown_table = df.to_markdown(index=False)
title = "A collection of data-structures and algorithms"
img = "\n![LOGO](./logo.jpg)\n"
siteLink = "\n### Hosted Site : https://dssheet.vercel.app"
subHeading = "\n### Questions\n"
with open("README.md", "w") as rf:
rf.write(f"# DS\n\n> {title}\n{img}{siteLink}\n{subHeading}\n{markdown_table}")