hogwarts
(霍格沃兹测试学院官方)
1
给定一个数字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") == []
joyoko
(徐子宇)
2
什么情况。
"""
: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):
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' ))
joyoko
(徐子宇)
8
奇数位填充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)]
Olivia
10
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") == []
HLK
12
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") == []
def solution(n: int, v1, v2) -> list:
# your code here
# l = []
# if n == 0:
# return l
# while len(l) < n:
# for i in [v1, v2]:
# if len(l) < n:
# l.append(i)
# return l
return [v1 if i % 2 == 0 else v2 for i in range(0, n)]
print(solution(10, "bue", "red"))
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") == []
lekaixin
(ALe阿乐)
18
def solution(n: int, v1, v2)-> list:
res_list=[]
for i in range(n):
if i % 2 == 0:
res_list.append(v1)
else:
res_list.append(v2)
return res_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") == []