美团的面试

美团面试

  • 虽然打算考研,但是之前面试的一些经验还是要记录下来
    面试比较惨烈,没怎么准备,可能也是那种笨蛋心理,万一准备了还失败,不是很丢脸,如果没准备失败还能给自己找点借口,多笨的心态
  • 给定一个数组[1,2,3,[[4,5,6],7,8],9],转化为[1,2,3,4,5,6,7,8,9],
# 给定一个数组[1,2,3,[[4,5,6],7,8],9],转化为[1,2,3,4,5,6,7,8,9]
x = [1,2,3,[[4,5,6],7,8],9]
y = []

def fun1(listname):
   for i in listname:
       if isinstance(i,list):
           fun1(i)
       else:
           y.append(i)
   return y

z = fun1(x)
print(z)
  • 给定一个数组[‘1a’,‘2b’,‘3c’,‘4d’,‘5a’],输出出现次数最多的字符前面的数字之和
# 给定一个数组['1a','2b','3c','4d','5a'],输出出现次数最多的字符前面的数字之和
import re
x = ['1a','2b','3c','4d','5a']
list1 = []
list2 = []


# 将数组每一个分割成为数字和字符,单独隔开,如‘1a’,分割成‘1’,‘a’,最后list1组成数字组,list2组成字符组
def fun2(listname):
    result = 0
    for i in listname:
        pattern = re.compile(r'(\d+)(\D+)')
        result1 = pattern.match(i)
        list1.append(result1.group(1))
        list2.append(result1.group(2))
    k = fun3(list2)
    for j in range(len(list2)):
        if list2[j] == k:
            result += int(list1[j])
    print(result)


def fun3(listname):
    s = set(listname)
    count = {}
    for i in s:
        count[i] = listname.count(i)
    maxvalue = max(count.values())
    for k,j in count.items():
        if j == maxvalue:
            return k


fun2(x)

  • 给定一个二维数组[[‘A’,‘B’],[‘1’,‘2’],[‘a’,‘b’]],将所有的排列顺序表示出来
#给定一个二维数组[['A','B'],['1','2'],['a','b']],将所有的排列顺序表示出来
x = [['A','B'],['1','2'],['a','b']]


#将二维数组进行翻转,然后将后面两个弹出来,进行fun2()排序合并操作,将返还的值加入数组中,直至数组的长度为1
def fun1(listname):
    listname.reverse()
    while(len(listname)!=1):
        y = listname.pop()
        z = listname.pop()
        # print(y,z)
        h = fun2(y,z)
        listname.append(h)
    print(listname)


def fun2(y,z):
    h = []
    for i in y:
        for j in z:
            temp = i+j
            h.append(temp)
    return h



fun1(x)

猜你喜欢

转载自blog.csdn.net/weixin_43903164/article/details/105325808