【每日一题20220510】小弟个数

:mage:‍ 给定一个数字列表,请编写一个函数,找出其中每个索引位置上的数字的右侧存在多少个比它小的数字,返回个数组成的列表。

【示例】
输入:[1, 2, 0]
输出:[1, 1, 0]
解释:索引0对应数字1,右边是2和0,只有0比它小,所以个数是1。以此类推,结果得到[1, 1, 0]

题目难度:简单
题目来源:CodeWars-How many are smaller than me?

def solution(nums: list)-> list:
    # your code here

assert solution([1, 2, 0]) == [1, 1, 0]
assert solution([1, 2, 3]) == [0, 0, 0]
assert solution([5, 4, 7, 9, 2, 4, 4, 5, 6]) == [4, 1, 5, 5, 0, 0, 0, 0, 0]
def solution(nums: list)-> list:
    # your code here
    res = []
    for i in range(len(nums)):
        count = 0
        for j in range(i+1,len(nums)):
            if nums[i] > nums[j]:
                count += 1
        res.append(count)
    return res

assert solution([1, 2, 0]) == [1, 1, 0]
assert solution([1, 2, 3]) == [0, 0, 0]
assert solution([5, 4, 7, 9, 2, 4, 4, 5, 6]) == [4, 1, 5, 5, 0, 0, 0, 0, 0]
def solution(list_a):
    list_b = list()
    for i in range(len(list_a)):
        temp = 0
        for j in range(i + 1, len(list_a)):
            if list_a[i] > list_a[j]:
                temp += 1
        list_b.append(temp)

    return list_b


def test_solution():
    assert solution([1, 2, 0]) == [1, 1, 0]
    assert solution([1, 2, 3]) == [0, 0, 0]
    assert solution([5, 4, 7, 9, 2, 4, 4, 5, 6]) == [4, 1, 5, 5, 0, 0, 0, 0, 0]
def solution(nums: list)-> list:
    # your code here
    return [len(list(filter(lambda x: x < nums[i], nums[i::]))) for i in range(len(nums))]
1 Like

c玩家追求时间最少,python玩家追求代码最少 :stuck_out_tongue_winking_eye:

1 Like
def solution(nums: list) -> list:
    ls = []
    for i in range(len(nums)):
        count_num = 0
        for j in nums[i + 1:]:
            if nums[i] > j:
                count_num += 1
        ls.append(count_num)
    return ls


if __name__ == '__main__':
    assert solution([1, 2, 0]) == [1, 1, 0]
    assert solution([1, 2, 3]) == [0, 0, 0]
    assert solution([5, 4, 7, 9, 2, 4, 4, 5, 6]) == [4, 1, 5, 5, 0, 0, 0, 0, 0]
def solution(nums: list)-> list:
    total_list = [0] * len(nums)
    for i in range(len(nums)):
        for j in nums[i+1::]:
            if j < nums[i]:
                total_list[i] = total_list[i] + 1
    return total_list
def solution(nums: list) -> list:
    result = []
    l = len(nums)
    for i in range(l - 1):
        k = 0
        for j in range(i + 1, l):
            if nums[i] > nums[j]:
                k += 1
        result.append(k)
    result.append(0)
    return result


assert solution([1, 2, 0]) == [1, 1, 0]
assert solution([1, 2, 3]) == [0, 0, 0]

def solution(nums: list)-> list:
    result = []
    while len(nums)>0:
        count = len([item for item in nums[1:] if item < nums[0]])
        result.append(count)
        nums.remove(nums[0])
    return result
	
assert solution([1, 2, 0]) == [1, 1, 0]
assert solution([1, 2, 3]) == [0, 0, 0]
assert solution([5, 4, 7, 9, 2, 4, 4, 5, 6]) == [4, 1, 5, 5, 0, 0, 0, 0, 0]
def solution(nums: list)-> list:
    return [len([item for item in nums[i+1:] if item < nums[i]]) for i in range(len(nums))]
	
assert solution([1, 2, 0]) == [1, 1, 0]
assert solution([1, 2, 3]) == [0, 0, 0]
assert solution([5, 4, 7, 9, 2, 4, 4, 5, 6]) == [4, 1, 5, 5, 0, 0, 0, 0, 0]
def solution(nums: list)-> list:
    """
    #方法一
       for i in range(len(nums)):
           count=0
           for j in nums[i+1:]:
               if nums[i]> j:
                   count += 1
           nums[i]=count
       return nums
    """
    return [sum(nums[i]> j for j in nums[i+1:]) for i in range(len(nums))]
关闭