给定两个正整数m和n,任务是生成一组由有序的,包含2个元素的元组(a,b)
构成的列表。规则是m<=a<=b<=n
。
示例:
输入:(m=2, n=4)
,输出:[(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
题目难度:简单
题目来源:codewars
def generate_pairs(m:int, n:int) -> list:
pass
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
def generate_pairs(m: int, n: int):
list_res = []
for i in range(m, n + 1):
for j in range(m, n + 1):
if i <= j:
list_res.append((i, j))
return list_res
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
def generate_pairs(m: int, n: int) -> list:
return [(i, j) for i in range(m, n + 1) for j in range(i, n + 1)]
if __name__ == "__main__":
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
public ArrayList<ArrayList<Integer>> Generate_pairs(int m,int n){
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<>();
for (int i = m; i <= n; i++) {
for (int j = i; j <=n ; j++) {
ArrayList<Integer> integers = new ArrayList<>();
integers.add(i);
integers.add(j);
arrayList.add(integers);
}
}
return arrayList;
}
def generate_pairs(m:int, n:int) -> list:
return [(x,y) for x in range(m,n+1) for y in range(m,n+1) if y >= x]
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
def generate_pairs(m:int, n:int) -> list:
return [(i,j) for i in range(m,n+1) for j in range(i,n+1)]
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
def generate_pairs(m: int, n: int) -> list:
result = []
for i in range(m, n + 1):
for j in range(i, n + 1):
result.append((i, j))
return result
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
Huis
8
def generate_pairs(m:int, n:int) -> list:
if m<0 or m>n: return []
result = []
for i in range(m,n+1):
for j in range(i,n+1):
result.append((i,j))
return result
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
assert generate_pairs(-1, 0) == []
assert generate_pairs(2, 1) == []
使用推导式可以做到一行解题(拾人牙慧)
def generate_pairs(m: int, n: int) -> list:
"""
:param m:
:param n:
:return: list of tuples
"""
return [(x, y) for x in range(m, n+1) for y in range(x, n+1)]
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]
def generate_pairs(m: int, n: int) -> list:
li = []
for a in range(m, n+1):
for b in range(m, n+1):
if a <= b:
li.append((a, b))
return li
def generate_pairs(m:int, n:int) -> list:
return [(a,b) for a in range(m,n+1) for b in range(a,n+1)]
lekaixin
(ALe阿乐)
12
def generate_pairs(m:int, n:int) -> list:
res_list=[]
for i in range(m,n+1):
for j in range(i,n+1):
tuple_index = (i,j)
res_list.append(tuple_index)
return res_list
assert generate_pairs(2, 4) == [(2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
assert generate_pairs(0, 1) == [(0, 0), (0, 1), (1, 1)]
assert generate_pairs(0, 0) == [(0, 0)]