#!python3
#-*- coding:utf-8 -*-
'''
题目描述
请设计一个高效算法,再给定的字符串数组中,找到包含"Coder"的字符串(不区分大小写),并将其作为一个新的数组返回。
结果字符串的顺序按照"Coder"出现的次数递减排列,若两个串中"Coder"出现的次数相同,则保持他们在原数组中的位置关系。
给定一个字符串数组A和它的大小n,请返回结果数组。
保证原数组大小小于等于300,其中每个串的长度小于等于200。同时保证一定存在包含coder的字符串。
测试样例:
["i am a coder","Coder Coder","Code"],3
返回:["Coder Coder","i am a coder"]
'''
class Coder:
def findCoder(self, A, n):
# write code here
list1 = []
list2 = []
list1_len = 0
for i_ in A:
i_len = len(i_)
i_low = i_.lower()
coder_num = 0
for index in range(0, i_len):
if i_low[index:index+5] == 'coder':
coder_num += 1
if coder_num > 0:
list1_len += 1
list1.append(coder_num)
list2.append(i_)
for i in range(0, list1_len-1):
for j in range(0, list1_len-1-i):
if list1[j] < list1[j+1]:
list1[j], list1[j+1] = list1[j+1], list1[j]
list2[j], list2[j+1] = list2[j+1], list2[j]
return list2
if __name__ == '__main__':
""
c = Coder()
A = ["i am a coder", "Coder Coder", "Code"]
n = 3
c.findCoder(A, n)
招聘编程题0x01
最新推荐文章于 2022-09-08 17:33:23 发布