1. 数列描述
示例代码:
# 核心 从第一项 推 第N项目
# 第一项
a0 = '1'
# 推到 第N项
N = 4
def fun(a0):
# 计算每一项的具体值
result = ''
left = 0
cursor = 0
while cursor < len(a0):
if a0[cursor] != a0[left]:
count = cursor -left
result += "{}{}".format(str(count),a0[left])
left = cursor
cursor += 1
count = cursor - left
result += "{}{}".format(str(count),a0[left])
return result
def fun1(a0,N):
i = 1
res = None
# 逐项推导
while i <= N:
# 具体推导每一项的值
res = fun(a0)
a0 = res
i += 1
return res
r = 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 r2
def fun(s,N):
n = 1
result = []
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 += 1
return 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)