forked from yznpku/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-indices.py
executable file
·65 lines (59 loc) · 3.08 KB
/
generate-indices.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
#!/usr/bin/python3
#
# This script will use challenge.json to generate README.md
# for the repository. It will compare the challenge list
# with all solutions present in the repository to calculate
# progress and fill up links.
#
# To get an up-to-date version of challenges.json, run
# 'update-challenge-list.py'.
import json
import os.path
def anchor_from_title(title):
return '-'.join(title.lower().split())
with open(os.path.dirname(__file__) + '/../challenges.json', 'r') as file_challenges, open(os.path.dirname(__file__) + '/../README.md', 'w') as fout:
json_root = json.loads(file_challenges.read())
# count finished challenges and prepare rows
for track in json_root['tracks']:
track['finish-count'] = 0
track['count'] = 0
for chapter in track['chapters']:
chapter['finish-count'] = 0
chapter['count'] = 0
for i, challenge in enumerate(chapter['challenges']):
path_to_challenge = 'solution/practice/%s/%s/%s' % (track['name'], chapter['name'], challenge['name'])
challenge['table-row'] = [str(i + 1),
'[%s](https://www.hackerrank.com/challenges/%s)' % (challenge['title'], challenge['name']),
]
track['count'] += 1
chapter['count'] += 1
if os.path.exists(os.path.dirname(__file__) + '/../' + path_to_challenge + '/solution.py'):
track['finish-count'] += 1
chapter['finish-count'] += 1
challenge['table-row'].append('[Solution & Comment](%s/solution.py)' % path_to_challenge)
else:
challenge['table-row'].append('WIP')
# write to README.md
fout.write('HackerRank Solutions in Python3\n======\n\n')
fout.write('This is a collection of my [HackerRank](https://www.hackerrank.com/) solutions written in Python3. '
'The goal of this series is to keep the code as concise and efficient as possible. '
'It might not be perfect due to the limitation of my ability and skill, so '
'feel free to make suggestions if you spot something that can be improved.\n\n'
'The index below is auto-generated. See [update-challenge-list.py](util/update-challenge-list.py) '
'and [generate-indices.py](util/generate-indices.py).\n\n')
fout.write('Index\n======\n\n')
for track in json_root['tracks']:
fout.write('* [%s](#%s) (%d/%d)\n' % (track['title'], anchor_from_title(track['title']), track['finish-count'], track['count']))
fout.write('\n')
for track in json_root['tracks']:
fout.write(track['title'] + '\n------\n\n')
for chapter in track['chapters']:
fout.write('* [%s](#%s) (%d/%d)\n' % (chapter['title'], anchor_from_title(chapter['title']), chapter['finish-count'], chapter['count']))
fout.write('\n')
for chapter in track['chapters']:
fout.write('### %s\n' % chapter['title'])
fout.write(' | '.join(['\\#', 'Challenge', 'Solution']) + '\n')
fout.write('|'.join([':---:'] * 3) + '\n')
for challenge in chapter['challenges']:
fout.write(' | '.join(challenge['table-row']) + '\n')
fout.write('\n')