面试题汇总

from collections import Counter

# 求因数(完全数),如:6=1+2+3
def test1(num):
    l = []
    st = ''
    for a in range(1, num):
        if num % a == 0:
            l.append(a)
    if sum(l) == num:
        for b in range(len(l)):
            if b == 0:
                st = '%s %s %s' % (num, '=', l[b])
            else:
                st = '%s %s %s' % (st, '+', l[b])
    print(st)


# 求数组中重复N次的值
def test2(num):
    arr = [1, 3, 8, 9, 15, 12, 15, 3, 3, 9]
    arr_new = []
    for a in arr:
        if arr.count(a) == num:
            arr_new.append(a)
    # 去重
    print(list(set(arr_new)))


# 将输入的字母转成大写
def test3():
    s = input("请输入内容:")
    f = open('test', 'w')
    f.write(s.upper())
    f.close()


# 计算数组重复个数及排序,且输出前N个数
def test4(N):
    arr = [1, 2, 3, 4, 5, 3, 2, 1, 4, 5, 6, 4, 2, 3, 4, 6, 2, 2, 9, 11]
    # 输出计数最多前N个数
    print(Counter(arr).most_common(N))
    # 输出计数最少后N个数
    print(Counter(arr).most_common()[:N:-1])

 

#数值排序
lst = [8,5,13,11]
for j in range(len(lst)-1):
   for i in range(len(lst)-j-1):
        print(lst[i],lst[i+1])
        if lst[i] > lst[i+1]:
            lst[i],lst[i+1] = lst[i+1],lst[i]
print(lst)


#字符排序
lis = ['d','a','c']
list.sort()
print(list)

 

转载于:https://www.cnblogs.com/ai594ai/p/8944184.html

你可能感兴趣的:(面试)