惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
D
Docker
J
Java Code Geeks
腾讯CDC
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
M
MIT News - Artificial intelligence
L
LangChain Blog
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
博客园 - Franky
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
N
Netflix TechBlog - Medium
B
Blog RSS Feed
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
使用 Python 写 OJ 题目格式生成工具|
2016-12-10 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

使用正则表达式将文本分割开,然后拼接成应该的格式

转码成 utf-8 输出出来

算是第一次学 Python 吧
学习了下一些基本的语法

#coding:utf-8
# -*- coding: utf-8 -*-
import re
import time
import urllib.request

# 格式
str1="""{% raw %}
<div>
    <div class="oj">   
        <div class="part" title="Description">
{% endraw %}
            $
{% raw %}
        </div>
        <div class="part" title="Input">
{% endraw %}
            $
{% raw %}
        </div>
        <div class="part" title="Output">
{% endraw %}
            $
{% raw %}
        </div>
        <div class="samp">
            <div class="clear"></div>
            <div class="input part" title="Sample Input">
{% endraw %}
                $
{% raw %}
            </div>
            <div class="output part" title="Sample Output">
{% endraw %}
                $
{% raw %}
            </div>
            <div class="clear"></div>
        </div>
    </div>
</div>
{% endraw %}"""


# 删除空行
def d1(str):
    str=str.replace('\n\n','')
    return str
def d2(str):
    str=str.replace('\n\n','\n')
    return str

# 末尾加两个空格
def k(str):
    p = re.compile(r'\n')
    line = p.split(str)
    str=''
    for i in range(0,len(line)):
        str+=line[i]+'  \n'
    return str

# 获取时间
def GetNowTime():
    return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))



#====================================#
#               main                 #
#====================================#

# 读入内容
str2 = ''
f = open('in.txt')
while True:
    line = f.readline()
    if len(line) == 0:
        break
    str2 += line+'\n'
f.close()

p = re.compile(r'\$')
p2 = re.compile(r'Input|Output|Sample Input|Sample Output')

ss=p.split(str1)
ss2 = p2.split(str2)

str0=''

for i in range(0,5):
    if i < 3:
        ss2[i] = k(d1(ss2[i]))
    else:
        ss2[i] = k(d2(ss2[i]))

for i in range(0, 5):
    str0 += ss[i] + ss2[i]
str0 += ss[5]

oj = input("输入OJ名称:")
num = input("输入题目编号:")
name = input("输入题目名称:")

title = '---\ntitle: '+oj+" "+num+"."+name+"\ndate: "+GetNowTime()+"\ncategories: "+'题解'+"\ntags:\n - "+oj+"\n---\n\n# "+'题目'+"\n"

url = "https://github.com/OhYee/sourcecode.io/blob/master/"+oj+"/"+num+"."+name+".cpp"
url = urllib.request.quote(url)

code = "\n\n# 题解\n\n\n\n# 代码\n```cpp "+ name + url+" 代码备份\n\n```"


# 强制转换为 utf8
ans = title + str0 +code
ans = ans.encode('UTF-8').decode('GBK')

f = open('in.txt', 'w')
f.write(ans)            
f.close()