未完待续~
HJ1 字符串最后一个单词的长度
while True:
try:
print(len(input().split()[-1]))
except:
break
HJ2 计算字符个数
while True:
try:
str_data = input().strip().lower()
char_data = input().strip().lower()
print(str_data.count(char_data))
except:
break
HJ 明明的随机数
方1:set+sorted内置
while True:
try:
n = int(input().strip())
data_list = []
for i in range(n):
data_list.append(int(input().strip()))
data_list = sorted(list(set(data_list)))
for i in data_list:
print(i)
except:
break
方2:定义新数组变量去重
while True:
try:
n = int(input().strip())
data_list = []
new_list = []
for i in range(n):
data_list.append(int(input().strip()))
for i in data_list:
if i not in new_list:
new_list.append(i)
while new_list:
print(min(new_list))
new_list.remove(min(new_list))
except:
break
HJ4 字符串分隔
使用join()重建data
while True:
try:
data = input()
while data:
data = list(data)
if(len(data)<=8):
res = ''.join(data)+'0'*(8-len(data))
print(res)
break
else:
res = ''.join(data[:8])
print(res)
data = ''.join(data[8:])
except:
break
更快的方式:
while True:
try:
data = input().strip()
if 0<len(data)<=8:
print(data+'0'*(8-len(data)))
else:
n = len(data)//8
m = len(data)%8
for i in range(n):
print(data[i*8:(i+1)*8])
if m!=0:
print(data[n*8:]+'0'*(8-m))
except:
break
HJ13 句子逆序
import sys
for line in sys.stdin:
data = line.strip().split()
print(' '.join(data[::-1]))
HJ17 坐标移动
import sys
def solution(end,i):
x,y = end[0],end[1]
if i==' ':
return end
if len(i)<=1 or len(i)>=4 or i.count('A')+i.count('S')+i.count('W')+i.count('D')>=2:
return end
a = i[0]
b = int(i[1:])
if a=='A':
x -=b
elif a=='S':
y -=b
elif a=='W':
y += b
else:
x+=b
return [x,y]
for line in sys.stdin:
sym = line.strip().split(';')
end = [0,0]
for i in sym:
end=solution(end,i)
print(','.join([str(x) for x in end]))
HJ20 密码验证合格程序
import sys
def is_con2(data):
flag=[0,0,0,0]
for i in data:
if 'a'<=i<='z':
if flag[0]==1:continue
flag[0]=1
elif 'A'<=i<='Z':
if flag[1]==1:continue
flag[1]=1
elif '0'<=i<='9':
if flag[2]==1:continue
flag[2]=1
else:
if flag[3]==1:continue
flag[3]=1
if sum(flag)>=3:return 'OK'
return 'NG'
def is_con3(data):
len_d = len(data)
for i in range(3,int(len_d/2)):
for j in range(len_d-i):
str1=data[j:j+i]
str2=data[j+i:]
if str1 in str2:
return 'NG'
return 'OK'
def solution(data):
if len(data)>8:
if is_con2(data)=='OK':
if is_con3(data)=='OK':
return 'OK'
return 'NG'
for line in sys.stdin:
data = line.strip()
res = solution(data)
print(res)
HJ23 删除字符串中出现次数最少的字符串
import sys
for line in sys.stdin:
data = line.strip()
data_s = list(set(list(data)))
nums ={}
min_c=len(data)
for i in data_s:
if min_c>data.count(i):
min_c=data.count(i)
nums[i] = data.count(i)
for k,v in nums.items():
if v==min_c:
data=data.replace(k,'')
print(data)
HJ24 合唱队
待补充
HJ25 数据分类处理
这到题题意描述也太难以理解了吧,甚至我是对着例子去扣的意思,而且我觉得题意有歧义,[相同的R<i>只需要输出索引小的以及满足条件的I,索引大的需要过滤掉] 好像也不是说R就要去重吧,我第一次读觉得是重复的R只输出对应满足条件的I中最小的下标的值。 总之要做的事情实际上就是对R去重排序,然后依次找出I里面包含R的数字的下标和值,当然当轮循环的tmp列表需要加上被包含的r和长度。然后最后输出的时候要在开头加上整体的总长度。
while True:
try:
res = []
I = input().split()[1:]
R = list(set(map(int,input().split()[1:])))
R = sorted(R)
for r in R:
res_tmp = []
for i,i_d in enumerate(I):
if str(r) in str(i_d):
index = str(i)
res_tmp = res_tmp +[index,i_d]
if res_tmp:
res_tmp = [str(r),str(int(len(res_tmp)/2))]+res_tmp
res = res + res_tmp
print(str(len(res))+" "+" ".join(res))
except:
break
HJ29 字符串加解密
此题思路上感觉比较简单,加密和解密分别一套ifelse判断对应的情况,然后进行转换即可,有一些小tips值得记录:字符转数字ord() ,数字转字符chr() ,大写转小写lower() ,小写转大写upper() ,是否是数字isdigit() ,是否是小写islower() .
def get_encryption(enc):
'''
加密
'''
res = ''
for i in enc:
if i.isdigit():
if i=='9':res = res +'0'
else:res += str(int(i)+1)
else:
ord_tmp = ord(i)
if i.islower():
if ord_tmp==z_num: res = res+'A'
else:res += chr(ord_tmp+1).upper()
else:
if ord_tmp==Z_num: res = res+'a'
else:res += chr(ord_tmp+1).lower()
return res
def get_decryption(dec):
'''
解密
'''
res = ''
for i in dec:
if i.isdigit():
if i=='0':res = res +'9'
else: res += str(int(i)-1)
else:
ord_tmp = ord(i)
if i.islower():
if ord_tmp==a_num: res = res+'Z'
else:res += chr(ord_tmp-1).upper()
else:
if ord_tmp==A_num: res = res+'z'
else:res += chr(ord_tmp-1).lower()
return res
z_num = ord('z')
Z_num = ord('Z')
a_num = ord('a')
A_num = ord('A')
while True:
try:
enc = input().strip()
dec = input().strip()
en_res = get_encryption(enc)
de_res = get_decryption(dec)
print(en_res)
print(de_res)
except:
break
HJ30字符串合并处理
这道题题意重新阐述一下:只按照题意方式处理0-9,a-f,A-F的字符,除此之外的保持不变。所以思路就是若字符是0-9,则直接转二进制,再逆序转十进制,再转16进制,若为字母,则先转十进制,再转二进制,再逆序,再转十进制,最后转16进制。
有些地方要注意: 1.十进制转二进制用bin(),二进制形式为‘0bXXX’,获取后面的XXX记得str[2:] ,然后不足四位的进行补充;二进制或十六进制转十进制用int;十进制转十六进制用hex,十六进制形式为‘0xXXX’,获取后面的‘XXX’记得str[2:] 2.末尾需要注意如果奇数列和偶数列长度不同,在chain.from_iterable(zip(str1,str2)) 这一步合并列表之后,长的那一列会被遗弃,所以需要添加一步处理。
from itertools import chain
template = 'abcdefABCDEF0123456789'
def solution(data):
'''
主程序入口
'''
data = list(data)
res = ''
str1 = []
str2 = []
for i,i_data in enumerate(data):
if i%2==0:str1.append(i_data)
else:str2.append(i_data)
str1.sort()
str2.sort()
tmp = ''
for str_data in [str1,str2]:
for i in range(len(str_data)):
i_data = str_data[i]
if i_data in template:
if i_data.isdigit():
tmp = bin(int(i_data))[2:]
if len(tmp)<=3:
tmp = '0'*(4-len(tmp))+tmp
tmp = '0b'+ tmp[::-1]
str_data[i] = str(hex(int(tmp,2))[2:].upper())
else:
tmp = '0x'+i_data
tmp = bin(int(tmp,16))[2:]
tmp = '0b'+tmp[::-1]
str_data[i] = str(hex(int(tmp,2))[2:].upper())
else:continue
result = list(chain.from_iterable(zip(str1,str2)))
if len(str1)>len(str2):result+=str1[-1]
elif len(str2)>len(str1):result+=str2[-1]
res = ''.join(result)
return res
while True:
try:
data = input().strip().split()
data = data[0]+data[1]
res = solution(data)
print(res)
except:
break
HJ32 密码截取
manacher算法 待补充
HJ33 整数与ip的转换
思路比较简单,ip转十进制的时候分开转二进制,然后拼接到一起,十进制转ip则反向。只是要注意每次处理之前记得填充0.填充使用zfill()
import sys
def solution(data):
if len(data)==4:
res = []
for i in data:
tmp = bin(int(i))[2:]
tmp = tmp.zfill(8)
res.append(tmp)
result = ''.join(res)
result = int('0b'+result,2)
else:
res = []
data = data[0]
data = bin(int(data))[2:]
data = data.zfill(32)
for i in range(4):
tmp = data[i*8:(i+1)*8]
tmp = str(int(tmp,2))
res.append(tmp)
result = '.'.join(res)
return result
for line in sys.stdin:
data = line.strip().split('.')
res = solution(data)
print(res)
青古の每篇一歌 《锦鲤抄》 阳光微凉 琴弦微凉 风声过往 人间仓皇
|