8000 add class ProgLang to define all leetcode support languages · fancymax/leetcode@2ff4115 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ff4115

Browse files
committed
add class ProgLang to define all leetcode support languages
1 parent f043975 commit 2ff4115

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

leetcode_generate.py

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,6 @@
2626
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36' # NOQA
2727
}
2828

29-
# TODO: define a class to put file ext and annotation
30-
31-
FILE_EXT = {
32-
'c++': 'cpp',
33-
'java': 'java',
34-
'python': 'py',
35-
'c': 'c',
36-
'c#': 'cs',
37-
'javascript': 'js',
38-
'ruby': 'rb',
39-
'swift': 'swift',
40-
'go': 'go'
41-
}
42-
43-
FILE_ANNO = {
44-
'c++': '//',
45-
'java': '//',
46-
'python': '#',
47-
'c': '//',
48-
'c#': '//',
49-
'javascript': '//',
50-
'ruby': '#',
51-
'swift': '//',
52-
'go': '//'
53-
}
54-
5529

5630
def get_config_from_file():
5731
cp = configparser.ConfigParser()
@@ -98,6 +72,23 @@ def check_and_make_dir(dirname):
9872
os.mkdir(dirname)
9973

10074

75+
class ProgLang:
76+
def __init__(self, language, ext, annotation):
77+
self.language = language
78+
self.ext = ext
79+
self.annotation = annotation
80+
81+
ProgLangList = [ProgLang('c++', 'cpp', '//'),
82+
ProgLang('java', 'java', '//'),
83+
ProgLang('python', 'py', '#'),
84+
ProgLang('c', 'c', '//'),
85+
ProgLang('c#', 'cs', '//'),
86+
ProgLang('javascript', 'js', '//'),
87+
ProgLang('ruby', 'rb', '#'),
88+
ProgLang('swift', 'swift', '//'),
89+
ProgLang('go', 'go', '//')]
90+
91+
ProgLangDict = dict((item.language, item) for item in ProgLangList)
10192
CONFIG = get_config_from_file()
10293

10394

@@ -128,7 +119,7 @@ def __init__(self):
128119
self.num_lock = 0
129120

130121
self.solutions = []
131-
self.language = CONFIG['language']
122+
self.proglang = ProgLangDict[CONFIG['language']]
132123

133124
self.base_url = 'https://leetcode.com'
134125
self.session = requests.Session()
@@ -204,7 +195,7 @@ def _load_solutions_by_language(self):
204195
# runTime = -1 if runText == 'N/A' else int(runText[:-3])
205196
language = i('tr>td:nth-child(5)').text().strip()
206197
capital_title = i('tr>td:nth-child(2)').text().strip()
207-
if pass_status and language == self.language:
198+
if pass_status and language == self.proglang.language:
208199
if capital_title not in self.solutions:
209200
self.solutions.append(capital_title)
210201
next_page_flag = '$(".next").addClass("disabled");' in content
@@ -248,7 +239,7 @@ def write_readme(self):
248239
(Notes: :lock: means you need to buy a book from Leetcode to unlock the problem)
249240
250241
| # | Title | Source Code | Article | Difficulty |
251-
|:---:|:---:|:---:|:---:|:---:|'''.format(language=self.language,
242+
|:---:|:---:|:---:|:---:|:---:|'''.format(language=self.proglang.language,
252243
tm=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
253244
num_solved=self.num_solved, num_total=self.num_total,
254245
num_lock=self.num_lock, repo=CONFIG['repo'])
@@ -262,9 +253,9 @@ def write_readme(self):
262253
else:
263254
if item.pass_status and item.capital_title in self.solutions:
264255
dirname = '{id}-{title}'.format(id=str(item.id).zfill(3), title=item.title)
265-
language = '[{language}]({repo}/blob/master/{dirname}/{title}.{ext})'.format(language=self.language, repo=CONFIG['repo'],
256+
language = '[{language}]({repo}/blob/master/{dirname}/{title}.{ext})'.format(language=self.proglang.language, repo=CONFIG['repo'],
266257
dirname=dirname, title=item.title,
267-
ext=FILE_EXT[self.language])
258+
ext=self.proglang.ext)
268259
else:
269260
language = ''
270261

@@ -305,10 +296,10 @@ def _generate_submissions_by_quiz(self, quiz):
305296
yield data
306297

307298
def _get_quiz_and_code_by_language(self, quiz):
308-
submissions_language = [i for i in list(self._generate_submissions_by_quiz(quiz)) if i['language'].lower() == self.language]
299+
submissions_language = [i for i in list(self._generate_submissions_by_quiz(quiz)) if i['language'].lower() == self.proglang.language]
309300
submissions = [i for i in submissions_language if i['status']]
310301
if not submissions:
311-
raise Exception('No pass {language} solution in question:{title}'.format(language=self.language, title=quiz.title))
302+
raise Exception('No pass {language} solution in question:{title}'.format(language=self.proglang.language, title=quiz.title))
312303

313304
if len(submissions) == 1:
314305
sub = submissions[0]
@@ -340,23 +331,23 @@ def download_quiz_code_to_dir(self, quiz):
340331
check_and_make_dir(dirname)
341332

342333
path = os.path.join(HOME, dirname)
343-
fname = '{title}.{ext}'.format(title=quiz.title, ext=FILE_EXT[self.language])
334+
fname = '{title}.{ext}'.format(title=quiz.title, ext=self.proglang.ext)
344335
filename = os.path.join(path, fname)
345336
# quote question
346337
# quote_question = '\n'.join(['# '+i for i in question.split('\n')])
347338

348339
l = []
349340
for item in question.split('\n'):
350341
if item.strip() == '':
351-
l.append(FILE_ANNO[self.language])
342+
l.append(self.proglang.annotation)
352343
else:
353-
l.append('{anno} {item}'.format(anno=FILE_ANNO[self.language], item=item))
344+
l.append('{anno} {item}'.format(anno=self.proglang.annotation, item=item))
354345
quote_question = '\n'.join(l)
355346

356347
import codecs
357348
with codecs.open(filename, 'w', 'utf-8') as f:
358-
print("begin to write file")
359-
content = '# -*- coding:utf-8 -*-' + '\n' * 3 if self.language == 'python' else ''
349+
print('write to file ->', fname)
350+
content = '# -*- coding:utf-8 -*-' + '\n' * 3 if self.proglang.language == 'python' else ''
360351
content += quote_question
361352
content += '\n' * 3
362353
content += code
@@ -384,7 +375,7 @@ def _download_quiz(self, quiz):
384375
print('{id}-{title} not pass'.format(id=quiz.id, title=quiz.title))
385376
else:
386377
if quiz.capital_title not in self.solutions:
387-
print('{id}-{title} pass in other language not using {language}'.format(id=quiz.id, title=quiz.title, language=self.language))
378+
print('{id}-{title} pass in other language not use {language}'.format(id=quiz.id, title=quiz.title, language=self.proglang.language))
388379
else:
389380
print('{id}-{title} pass'.format(id=quiz.id, title=quiz.title))
390381
self.download_quiz_code_to_dir(quiz)

0 commit comments

Comments
 (0)
0