刚作完华为的笔试题,简要描述一下三道编程题的解决方法以及python代码实现html
第一题大体描述:python
给定两个已经升序排序好的的序列A={a1,a2,a3,...an} 和B={b1,b2,b3...bn} ,一个数R,找出知足如下条件的的(ai,bj)序列对正则表达式
1.ai<=bj编程
2.bj和ai二者的距离 知足 bj-ai<=R ,要是该条件不知足,就从序列B中找出 和ai 距离最接近R的一个点bj(同时要知足条件1)app
输入样例:A={1,3,5},b={2,4,6},R=1测试
输出样例:(1,2)(3,4)(5,6)spa
解决思路:遍历全部序列对,找出知足条件的对便可code
代码以下:(测试经过)orm
import sys s=sys.stdin.readline() #获取A、B、R的值,用正则表达式匹配会更容易 a=s.find('{') b=s.find('}') a_str=s[a+1:b].split(',') A=[int(x) for x in a_str] a=s.rfind('{') b=s.rfind('}') b_str=s[a+1:b].split(',') B=[int(x) for x in b_str] a=s.rfind('=') R=int(s[a+1:]) RES=[] for x in A: has_find=False for y in B: if x<= y and y-x<=R: RES.append((x,y)) has_find=True elif x<=y and y-x>R and has_find==False: RES.append((x,y)) break for x in RES: print('({},{})'.format(x[0],x[1]),end='')
第二题大体描述:htm
对一行给定的字符串进行反转输出,同时去除中间不知足条件的分隔符
输入字符串:I am an 20-years out--standing @ * -stu- dent
去除分割符并反转以后,输出字符串(子字符串以一个空格隔开):dent stu standing out 20-years an am I
分割符描述以下:
一、除了字母、数字和 - 以外,其余的都是分割符,如输入字符串中的@ *等都属于分割符
二、20-years中的'-' 表示的是链接符,即当‘-’两边都有字母、数字时,‘-’就属于链接符,不然属于分割符
三、out--standing中的‘--’表示分割符,应该拆分为两个字字符串out 和 standing
解决思路:用栈去实现,遍历输入字符串的字符:
一、遇到字母和数字就入栈。
二、遇到‘-’的时候就判断是分割符仍是链接符,要是是分割符,就弹出全部栈元素,构成一个子字符串,不然就入栈
三、遇到其余分割符,弹出全部栈元素,构成子字符串
找到全部子字符串,就能够作反序输出处理
python代码以下:(测试经过)
import sys #使用栈去解决问题 #s="I am an 20-years out--standing @ * -stu- dent" s=sys.stdin.readline() word=[] res=[] for x in s: if '0'<= x <='9' or 'a'<=x <='z' or 'A'<=x<='Z': word.append(x) elif x=='-': if len(word)==0: continue else: if word[-1]=='-': word.pop() res.append(''.join(word)) word = [] else: word.append(x) else: if len(word)>0: if(word[-1]=='-'): word.pop() res.append(''.join(word)) word=[] if(len(word)>0): if word[-1]=='-': word.pop() res.append(''.join(word)) for s in res[::-1]: print(s,end=' ')
第三题大体描述以下:
给定多组本来的航班预订信息(航班号,座位号,乘客姓名),以及多组要改签的航班信息(本来航班号,本来座位号,新航班号,新座位号)
输出最后的航班预订信息,要是有重复的内容,以最新改签的为标准
输入的内容以下: 3 表示本来的航班信息数,2表示要改签的航班数
3
CZ7132,A1,ZHANGSAN
CZ7132,A2,ZHAOSI
CZ7156,A2,WANGWU
2
CZ7132,A1,CZ7156,A2
CZ7156,A2,CZ7156,A3
输出内容以下:
CZ7132,A2,ZHAOSI
CZ7156,A2,ZHANGSA
CZ7156,A3,WANGW
解决思路,采用python的字典去表示机票位置信息和乘客姓名的对应关系 {piao:name}{name:piao),先找出须要修改航班的乘客姓名,再依次更新该乘客的航班号,航班座位
代码以下:
#输入: #3 #CZ7132,A1,ZHANGSAN #CZ7132,A2,ZHAOSI #CZ7156,A2,WANGWU #2 #CZ7132,A1,CZ7156,A2 #CZ7156,A2,CZ7156,A3 #输出 #CZ7132,A2,ZHAOSI #CZ7156,A2,ZHANGSAN #CZ7156,A3,WANGWU import sys old_booking_piao_name={} old_booking_name_piao={} sum_num=int(input()) for i in range(sum_num): msg=input() m=msg.rfind(',') piao=msg[0:m] name=msg[m+1:] old_booking_name_piao[name]=piao old_booking_piao_name[piao]=name chang_booking=[] num=int(input()) for i in range(num): msg = input() m = msg.split(',') old = m[0]+','+m[1] new = m[2]+','+m[3] # print(old,new) chang_booking.append((old,new)) new_booking_name_piao_copy=old_booking_name_piao.copy() for x in chang_booking: name=old_booking_piao_name[x[0]] new_booking_name_piao_copy.pop(name) new_booking_name_piao_copy[name]=x[1] # print(new_booking_name_piao_copy) for key,val in new_booking_name_piao_copy.items(): print(val+','+key)
原文出处:https://www.cnblogs.com/mangojun/p/11510959.html