以下介绍自己的代码风格,不代表正确!
变量命名
变量尽量使用有意义的英文单词及其组合
如果变量和内置变量同名,可以在后面加下划线
比如Python表示长度,由于len
是内置变量,所以可以用len_
。
为了防止变量名重复,可以在私有变量在前面加下划线
比如
for _log_len_counter, _trace_len_counter in pool.starmap(origin_to_vocab, tqdm(id_s)):
变量尽量使用小写英文字母,用下划线分隔单词
比如id_metric_float_path
id_metric_int_path
id_log_float_path
函数也使用小写英文字母加下划线,并且写好类型注解,便于自动补全
比如
def string_to_id(raw: str, len_: int) -> list:
def normalize(df: pd.DataFrame, col_name: str):
def trunc_pad_to_len(df: pd.DataFrame, len_):
常量使用大写英文字母
比如
PAD = "<PAD>"
UNK = "<UNK>"
类使用驼峰法命名,即每个单词的首字母大写,其它字母小写
比如
class CycleList(list):
代码习惯
使用短路优先来降低缩进层级
比如
for element in iterable:
if some_condition(element):
do_something(element)
if another_condition(element):
do_another_thing(element)
可以写成
for element in iterable:
if not some_condition(element):
continue
do_something(element)
if not another_condition(element):
continue
do_another_thing(element)
尽量使用Python的标准库实现,Python的标准库中有很多方便的实现。
下面这几种都是比较常用的
from pathlib import Path
from collections import Counter
from collections import defaultdict
比如这个题目【MT2129 来给单词分类】
现有如下单词分类原则:两个单词可以分为一类当且仅当组成这两个单词的各个字母的数量均相等。
现有nnn个单词均由大写字母组成,每个单词长度小于等于100。求单词被分为几类。
格式
第一行输入单词个数nnn;
以下nnn行每行一个单词。
一个整数表示类数。
样例 1
3
AABAC
CBAAA
AAABB
复制
2
复制
备注
对于70%的数据满足n≤100n≤100n≤100;
对于100%的数据满足n≤10000n≤10000n≤10000。
可以直接使用Python完成
from collections import Counter
from operator import itemgetter
def word_to_class(word: str):
# 二维元组好像没法去重,那就扁平化一下
temp = Counter(word)
temp = list(temp.items())
temp.sort(key=itemgetter(0))
temp = tuple(zip(*temp))
return temp[0] + temp[1]
n = int(input())
word_classes = set()
for i in range(n):
word_classes.add(word_to_class(input()))
print(len(word_classes))
在能够面向对象的时候尽量面向对象
比如这个题目【MT2125 一样的虫子】
有NNN只虫子,每只虫子有666条腿,每条腿都有长度而且长得很奇怪,如图所示:
第iii只虫子六条腿的长度从某条腿开始顺时针依次记为a1,a2,…,a6a_1, a_2, …, a_6a1,a2,…,a6。
从任何一条腿开始顺时针或逆时针往后记录长度,得到的六元组都代表形状相同的虫子。
例如a1,a2,…,a6a_1, a_2, …, a_6a1,a2,…,a6和a2,a3,…,a6,a1a_2, a_3 ,…, a_6,a_1a2,a3,…,a6,a1就是形状相同的虫子;a1,a2,…,a6a_1, a_2, …, a_6a1,a2,…,a6和a6,a5,…,a1a_6, a_5, …, a_1a6,a5,…,a1也是相同的虫子。
我们称两只虫子相同,当且仅当它们各自从某一条腿开始顺时针或逆时针记录长度,能得到两个相同的六元组。
小码哥请你计算这NNN只虫子中是否存在两只相同的虫子。
格式
输入的第一行将包含一个整数 nnn,即要检查的虫子的数量;
接下来是 nnn 行,每行描述一个虫子。
每只虫子将由包含六个数a1,a2,…,a6a_1,a_2, …,a_6a1,a2,…,a6的行来描述,即虫子腿的长度。
如果所有的虫子都是不同的,你的程序应该打印消息:No
;
如果有两只虫子相同,你的程序应该打印消息:found.
。
样例 1
2
1 2 3 4 5 6
4 3 2 1 6 5
复制
found.
复制
备注
其中:0<n≤100000,0≤a1,…,a6≤10000000 < n ≤ 100000,0≤ a_1,…,a_6 ≤ 10000000<n≤100000,0≤a1,…,a6≤1000000
可以直接使用Python完成
class CycleList(list):
def iter_from_i(self, i, clockwise=True):
if clockwise:
return self[i:] + self[:i]
else:
return self[i::-1] + self[:i:-1]
class Bug:
def __init__(self, legs: CycleList):
possible_ids = []
for i in range(len(legs)):
possible_ids.append(legs.iter_from_i(i, True))
possible_ids.append(legs.iter_from_i(i, False))
self.id = tuple(min(possible_ids))
def __eq__(self, other):
return self.id == other.id
id_existed = set()
n = int(input())
def judge_bugs_same(n: int):
for i in range(n):
legs = CycleList((input().split()))
bug = Bug(legs)
if bug.id in id_existed:
return True
else:
id_existed.add(bug.id)
return False
if judge_bugs_same(n):
print("found.")
else:
print("No")
这里抽象出了CycleList和Bug两个对象,并且重写了Bug的__eq__内置方法。