1. 数列描述
示例代码:
# 核心 从第一项 推 第N项目
# 第一项
a0 = '1'
# 推到 第N项
N = 4
def fun(a0):# 计算每一项的具体值result = ''left = 0cursor = 0while cursor < len(a0):if a0[cursor] != a0[left]:count = cursor -leftresult += "{}{}".format(str(count),a0[left])left = cursorcursor += 1count = cursor - leftresult += "{}{}".format(str(count),a0[left])return resultdef fun1(a0,N):i = 1res = None# 逐项推导while i <= N:# 具体推导每一项的值res = fun(a0)a0 = resi += 1return resr = fun1(a0,4)
print(r)
2. 矩阵最大值
题目描述:
示例代码:
N = 5
s = ['1,0,0,0,1','0,0,0,1,1','0,1,0,1,0','1,0,0,1,1','1,0,1,0,1'
] def fun1(s1,s2):r1 = int(s1,2)r2 = int(s2,2)return r1 if r1 > r2 else r2def fun(s,N):n = 1result = []while n <= N:# 移一位if n % 2 == 1:s_list = s[n - 1].split(',')# 左移一位left1 = ''.join(s_list[1:]) + s_list[0]# 右移一位right1 = s_list[-1] + ''.join(s_list[:-1])# 添加左移一位 和 右移一位 的最大值result.append(fun1(left1,right1))# 移两位else:s_list = s[n - 1].split(',')# 左移二位left2 = ''.join(s_list[2:] + s_list[0:2])# 右移二位right2 = ''.join(s_list[-1:-3:-1] + s_list[:-2])# 添加左移二位 和 右移二位 的最大值result.append(fun1(left2,right2))n += 1return sum(result)r = fun(s,N)
print(r)
3. 数据分类
示例代码:
# 测试数据
s1 = 3
ss1 = 4
sss1 = [256, 257, 258, 259, 260, 261, 262, 263, 264, 265]
tmp_dict = {'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15}
def fun(num):# 计算十六进制数 各位数之和num_str = hex(num)[2:]tmp = [tmp_dict[_] if _ in tmp_dict else int(_) for _ in num_str]return sum(tmp)def fun1(s,ss,sss):tmp_list = [fun(_) for _ in sss]tmp1_list = [_ % ss for _ in tmp_list]res = []for i in range(s):res.append(tmp1_list.count(i))return max(res)r = fun1(s,ss,sss)
print(r)