Tips:答案在所有问题的后边 Githubd 地址:https://github测试数据/zlhcsm/python_algorithm/blob/master/practice1.py 里边还有好多真题代码呦!支持的大佬还请点个 星星
题1
简单的字符串排序 将字符串按ascii的顺序进行排序
注意,用例的输入是input()
输入描述
cba
输出描述
abc
示例1
输入 cba 输出 abc
示例2
输入 031FC 输出 013CF
题2字符映射 (1) 有一个字符生成装置,有按钮1-9,其中每个数字对应多个字母,对应关系如下:
1 – a,b,c 2 – d,e,f 3 – g,h,i 4 – j,k,l 5 – m,n,o 6 – p,q,r 7 – s,t,u 8 – v,w,x 9 – y,z
现在给出一个只包含1-9的点按序列,返回所有可能的映射组合,以字典序输出。
输入描述
输入: 13
输出描述
输出: ag ah ai bg bh bi cg ch ci
示例1
输入 13 输出 ag ah ai bg bh bi cg ch ci
题1代码
#coding=utf-8 # 获取输入的字符串 l = input() # 三个数组:c_array保存字符,n_array保存数字 # result保存结果数组 c_array = [] n_array = [] result = [] # 遍历输入,把字符串和数字分别加入对应数组 for i in l: if i.isalpha(): c_array.append(i) else: n_array.append(i) # 分别对应排序 n_array.sort() c_array.sort() # 将数字和字符顺序加入结果 result.extend(n_array) result.extend(c_array) # 将list结果转换为str print(''.join(result))
题2代码
# dic:保存对应关系 mid:用来存放中间变量 dic = [] mid = [] # 生成最初字符串数字对应关系 for index in range(26): mid.extend(chr(index+ ord('a'))) if ((index + 1) % 3 == 0) | (index == 25): # 遇到三个时加入另一个数字,25是因为最后的数字不够对应三个元素 dic.append(mid) mid = [] # 获取输入的值并且存储为list l = input() i_index = [] for item in l: i_index.extend(item) # 生成对应关系 # res里存放是结果数组 flag是标志是否是第一次循环 res = [] flag = 0 for item in i_index: if flag == 0: res = dic[int(item)-1] # 如果是第一次循环,就直接赋值就好 else: res = [(x + y) for x in res for y in dic[int(item)-1]] # 生成两个数组的笛卡尔积 flag = 1 # 第一次循环后,将标志位记为1 res.sort() # 排序 # 输出结果 for i in range(len(res)): print(res[i], end=' ')
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did125997