AI量化知识树

笔试

由bqjbfe2i创建,最终由bqjbfe2i 被浏览 73 用户

# 102
def func(a):
    '''
    a: 输入数组,已经排好序
    返回值:出现次数最多的元素,如果有多个,输出最早出现的
    '''
    dic = dict()
    for x in range(len(a)):
        if a[x] in dic.keys():
            dic[a[x]] += 1
        else:
            dic[a[x]] = 1

    c = max(dic.values())
    for key, value in dic.items():
        if value == c:
            return key
# 在这里编写测试用例,覆盖尽可能多的情况
assert func([1, 1, 2, 3, 4, 5, 5, 5, 6]) == 5    # 5 出现了3次
assert func([00000,0,1,1,2,4,1,2,1]) == 1
assert func([None]) == None




# 103
def func(a):
    '''
    a: 输入数组,已经排好序
    返回值:输出数组,去掉重复元素
    '''
    b = list(set(a))
    return b

assert func([1, 1, 2, 3, 4, 5, 5, 5, 6]) == [1, 2, 3, 4, 5, 6]


#104
def func(a, b):
    '''
    a: 严格递增的有序数组
    b: 给定数字
    '''
    sum1 = 0

    for x in range(len(a)-1):
        start = x
        end = x+1
        while True:
            sum_num = a[start] + a[end]
            if sum_num == b:
                sum1 += 1
                break
            if sum_num < b:
                end += 1
                if end > len(a)-1:
                    break
            if sum_num > b:
                break
    return sum1
assert func([1, 2], 4) == 0    
assert func([1, 2, 3, 4, 5, 6, 9], 8) == 2  

\

{link}