【每日一题20220617】交替填充

:mage:‍ 给定一个数字n和另外两个值,请编写一个函数,构造一个长度为n的列表,并交替使用这两个值填充到列表中。

【示例】
输入:5, True, False
输出:[True, False, True, False, True]
解释:列表会包含5个元素,从True开始填充,然后是False,以此类推。

题目难度:简单
题目来源:codewars- Length and two values.

def solution(n: int, v1, v2)-> list:
    # your code here

assert solution(5, True, False) == [True, False, True, False, True]
assert solution(10, "blue", "red") == ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
assert solution(0, "one", "two") == []

什么情况。

"""
:mage:‍ 给定一个数字列表,依次找出每个位置上的数字,在其右侧有多少个比它小的数。

【示例】
输入:[1, 2, 0]
输出:[1, 1, 0]
解释:第一位上是1,右边只有1个0比它小;第二位上是2,右边只有1个0比它小;第三位是0,右侧没有数字比它小。
"""
def solution(nums: list)-> list:
    # your code here
    start_num = 0
    while start_num < len(nums):
        count = 0
        for i in nums[start_num+1:]:
            if i < nums[start_num]:
                count += 1
        nums[start_num] = count
        start_num += 1
    return nums



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

def solution(n: int, v1, v2)-> list:
    return [v1 if i % 2 == 0 else v2 for i in range(n)]

2 Likes

我刚才看的也是你这题… 一刷新就换题了好像…

估计还没定稿就被你们蹲到了

def solution(n: int, v1, v2):
    a = [v1, v2]
    b = n // len(a)
    c = n % len(a)
    if c == 0:
        return a * b
    else:
        return (a * b + [v1])
def  Alternate_Twokey(num:int, key1 , key2 ):
    """
    num:交换的次数
    key1:交换的值1
    key2:交换的值2
    """
    listkeys = []
    for i in range(num):
        if i % 2 ==0:
             listkeys.append(key1)
        if i % 2 == 1:
            listkeys.append(key2)
    return listkeys


print(Alternate_Twokey(5,'true' ,'flase' ))

奇数位填充v1,偶数位填充v2.

def solution(n: int, v1, v2)-> list:
    # your code here
    return [v2 if i % 2 ==0 else v1 for i in range(1, n+1)]

我点开codewar的时候还愣了一下,哈哈哈。

def solution(n: int, v1, v2)-> list:
    result = [v2 if i % 2 == 0 else v1 for i in range(1, n+1)]
    return result

if __name__ == '__main__':
    assert solution(5, True, False) == [True, False, True, False, True]
    assert solution(10, "blue", "red") == ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
    assert solution(0, "one", "two") == []
def solution(n: int, v1, v2) -> list:
    new_list = []
    for i in range(n):
        if i % 2 == 0:
            new_list.append(v1)
        else:
            new_list.append(v2)
    return new_list


assert solution(5, True, False) == [True, False, True, False, True]
assert solution(10, "blue", "red") == ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
assert solution(0, "one", "two") == []
def solution(n: int, v1, v2)-> list:
    return [v1, v2] * (n // 2) + [v1, v2][0:(n % 2)]

assert solution(5, True, False) == [True, False, True, False, True]
assert solution(10, "blue", "red") == ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
assert solution(0, "one", "two") == []
def solution(n: int, v1, v2):
    list_1 = []
    i = 0
    for i in range(n):
        if i % 2 == 0:
            list_1.append(v1)
        else:
            list_1.append(v2)
        i+=1
    print(list_1)
    return list_1
if __name__ =='__main':
    assert solution(5, True, False) == [True, False, True, False, True]
    assert solution(10, "blue", "red") == ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
    assert solution(0, "one", "two") == []
def solution(n: int, v1, v2)-> list:
    return [v1,v2]* (n//2)+[v1]*(n%2)
def solution(n: int, v1, v2)-> list:
    return [v1 if i%2==0 else v2 for i in range(n)]

assert solution(5, True, False) == [True, False, True, False, True]
assert solution(10, "blue", "red") == ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
assert solution(0, "one", "two") == []
关闭