Leetcode 刷题助手-legit-python实现

来源:互联网 发布:四知先生翻译 编辑:程序博客网 时间:2024/04/29 04:00

问题来源

  在刷leetcode的时候,每做一道题,都需要在本地手动创建源文件,然后编译运行。对于我来说,还会在文件开头部分添加一些说明内容,包括问题描述,问题解决思路等。问题完成之后,我还会把源文件commit至本地的repository中。每次重复地手动做这么工作,让我萌生了写一个简单的自动化工具的想法。

问题需求分析:

  1.在给定问题url后,程序能够自动生成一下内容,比如在给定https://leetcode.com/problems/house-robber-iii/后,自动生成如下内容:

// Source       : https://leetcode.com/problems/house-robber-iii/// Problem name : House Robber III// Problem ID   : 337// difficulty   : Medium// Author       : Tyrion// Date         : 2016-09-06/*************************************************************************************** * The thief has found himself a new place for his thievery again. There is only one * entrance to this area, called the "root." Besides the root, each house has one and * only one parent house. After a tour, the smart thief realized that "all houses in * this place forms a binary tree". It will automatically contact the police if two * directly-linked houses were broken into on the same night. * ***************************************************************************************//*************************************************************************************** 1.  2. solution: ***************************************************************************************/

  2.在问题完成之后,能自动commit到本地的repository中,并在readme.md中添加相应信息。readme.md格式:

|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [C](./algorithms/c/string_to_integer_8.c)|Easy||338|[Counting Bits](https://leetcode.com/problems/counting-bits/)| [C](./algorithms/c/counting_bits_338.c)|Medium|

程序设计

  1. 维护一个config文件,包含一些配置信息,包括源文件地址,本次仓库地址等信息,具体如下:

author=Tyrionsource_folder=F:/Leetcode/destination_folder=F:/git/leetcode/algorithms/crelative_folder=./algorithm/creadme_addr=F:/git/leetcode/readme.mdlanguage=cgit_folder=F:/git/leetcode

  2. 程序开始运行时,读取config文件内容,并读取用户输入,输入可以有两种方式:

  1. create + url, 表示创建新的源文件,源文件的信息从url中自动抽取。
  2. commit + problem_id, 表示把problem_id对应的源文件commit到本地的repository

  3. create 模式
  在create模式下,首先需要根据URL爬取相应网页,提取网页内容。这部分利用urllib + xpath完成。提取出内容后,就需要创建源文件,把内容格式化到源文件当中。这当中的一个陷阱就是编码转换,很容易出现乱码。保证编码正确的关键点在于理解Python内部的字符串一般都是 Unicode编码。做一些编码转换通常是要以Unicode作为中间编码进行转换的,即先将其他编码的字符串解码(decode)成 Unicode,再从 Unicode编码(encode)成另一种编码。在这里我的处理是这样的:

  由于html页面采用的是utf-8编码,在爬取html到本地的时候,需要把 utf-8编码的字符串解码成Unicode编码的字符串。

# crawl a leetcode problem htmldef crawl_html(url):    response = request.urlopen(url)    content_bytes = response.read().decode("utf-8") #把utf-8编码的字符串转成Unicode编码的字符串    content = etree.HTML(content_bytes)    return content

  在windows下创建本地文件时,如果采用默认的open方法,那么创建的文件的编码就是GBK。如果直接把utf-8编码字符串解码出的Unicode字符串写入GBK编码的文件中,就有可能产生乱码。所以我们在创建源文件时需要指定编码,这里需要用到codecs里的open方法。创建文件后,根据config文件以及html内容把格式化数据写入源文件中。

# parse the html content and wirte the need content to source code filedef parse_content(url, content, folder, suffix):    title = content.xpath("//div[@class='question-title clearfix']/h3/text()")[0]    filename, name, id = generate_filename(title)    difficulty = content.xpath("//div[@class='question-info text-info']/ul/li[3]/strong/text()")[0]    #创建指定编码的源文件    file_handler = codecs.open(folder + '.'.join([filename, suffix]), 'w', "utf-8")    write_first_part(file_handler, url, name, id,difficulty)    write_second_part(file_handler, content)    file_handler.close()

4. commit
commit模式下,首先根据用户给定的problem_id去config文件指定的目录下找到源文件,并用python调用
git命令进行commit。这里需要安装windows git客户端,并把git路径加入系统环境变量。

import os# git add commit pushdef git_commit(git_folder, filename):    os.chdir(git_folder)    os.system("git add ./")    os.system(''.join(['git commit -m "add file: ', filename, '"']))

最后,使用pyinstaller把代码打包成exe可执行应用程序,all done!以后再也不用手动创建文件了,写文件头了!
程序的不足之处在于没有爬下来html里的代码模板部分,这部分还需要手动复制。以后有空再改进。

代码请见:https://github.com/Tyrion-python/leetcode_assistant

0 0