给定正整数n,请编写一个函数,按照下方的造型进行返回。
1
2 3
4 5 6
7 8 9 10题目难度:简单
题目来源:udemy视频教程
def number_pattern(n: int) -> str:
pass
assert number_pattern(1) == "1 "
assert number_pattern(2) == "1 \n2 3 "
assert number_pattern(4) == "1 \n2 3 \n4 5 6 \n7 8 9 10"
给定正整数n,请编写一个函数,按照下方的造型进行返回。
1
2 3
4 5 6
7 8 9 10题目难度:简单
题目来源:udemy视频教程
def number_pattern(n: int) -> str:
pass
assert number_pattern(1) == "1 "
assert number_pattern(2) == "1 \n2 3 "
assert number_pattern(4) == "1 \n2 3 \n4 5 6 \n7 8 9 10"
暂时只想到这个答案,挺糟糕的
def number_pattern(n: int) -> str:
result = ""
end = [int(a*(a+1)/2)+1 for a in range(1, n)]
for i in range(1, int(n*(n+1)/2)+1):
if i in end:
result = result + "\n"
result = result + str(i) + " "
return result
assert number_pattern(1) == "1 "
assert number_pattern(2) == "1 \n2 3 "
assert number_pattern(4) == "1 \n2 3 \n4 5 6 \n7 8 9 10 "
public StringBuilder numberPattern(Integer integer){
StringBuilder res = new StringBuilder();
int tmp = 1;
for (int i = 0; i < integer; i++) {
for (int j = 0; j < i+1; j++) {
res.append(tmp++);
res.append(" ");
}
res.append('\n');
}
return res;
}
@Test
public void testNumberPattern(){
assert numberPattern(1).equals("1");
assert numberPattern(2).equals("1 \n" +
"2 3 ");
assert numberPattern(4).equals("1 \n" +
"2 3 \n" +
"4 5 6 \n" +
"7 8 9 10 ");
}
res = ""
count, i = 1, 1
while i < n + 1:
for j in range(i, min(i + count, n+1)):
res += str(j) + " "
res += '\n'
i += count
count += 1
return res
def number_pattern(n: int) -> str:
num_list = list(range(sum(range(1, n + 1)), 0, -1))
res = []
for x in range(1, n + 1):
temp = ''
for y in range(1, x + 1):
temp += f"{num_list.pop()} "
res.append(temp)
return '\n'.join(res)
assert number_pattern(1) == "1 "
assert number_pattern(2) == "1 \n2 3 "
assert number_pattern(4) == "1 \n2 3 \n4 5 6 \n7 8 9 10 "
def number_pattern(n: int) -> str:
num_list_end = sum((range(1, n + 1)))
num_list_start = sum((range(1, n + 1))) - n + 1
if n == 1:
return '1'
else:
return number_pattern(n - 1) + '\n' + ' '.join(map(lambda x: str(x), range(num_list_start, num_list_end + 1)))
参考题解:
def number_pattern(n: int) -> str:
num = 1
lines = []
for i in range(1, n + 1):
line = ""
for j in range(1, i + 1):
line += f"{num} "
num = num + 1
lines.append(line)
return '\n'.join(lines)
assert number_pattern(1) == "1 "
assert number_pattern(2) == "1 \n2 3 "
assert number_pattern(4) == "1 \n2 3 \n4 5 6 \n7 8 9 10 "
def number_pattern(n: int) -> str:
data = []
for i in range(1, n + 1):
line = []
# 前i-1行数字总个数
count = sum([j for j in range(1, i)])
# 第i行 :前i-1行数字总个数count+1 至 count+i
for i in range(1, i + 1):
line.append(str(count + i))
# 将列表以空格连接形成字符串,末尾加空格
data.append(' '.join(line) + ' ')
# 最后将列表以换行符连接形成字符串
return '\n'.join(data)
assert number_pattern(1) == "1 "
assert number_pattern(2) == "1 \n2 3 "
assert number_pattern(4) == "1 \n2 3 \n4 5 6 \n7 8 9 10 "
"""
数据推导(n=5):
传入的n值为(n): 1, 2, 3, 4, 5
对应字串长度(lens): 1, 3, 6, 10, 15
换行的数字(start):2, 4, 7, 11
【规律】:当前元素值=之前所有元素的总和
"""
def number_pattern(n: int) -> str:
start = []
end = []
result = ""
# 遍历行数,格式如:[1, 2, 3, 4, 5],行数等于n
lens = [i for i in range(1, n+1)]
# 确定每行末尾的数字,格式如:[1, 3, 6, 10, 15],规律:当前元素值=之前所有元素的总和
for i in lens:
start.append(i)
end.append(sum(start))
# 确定换行的数字,格式如: [2, 4, 7, 11],用于拼接换行符"\n"
starts = [i+1 for i in end if i < end[-1]]
for i in range(1, sum(lens)+1):
if i in starts:
a = "\n" + str(i) + " "
result += a
else:
result += str(i) + " "
return result
assert number_pattern(1) == "1 "
assert number_pattern(2) == "1 \n2 3 "
assert number_pattern(3) == "1 \n2 3 \n4 5 6 "
assert number_pattern(4) == "1 \n2 3 \n4 5 6 \n7 8 9 10 "
assert number_pattern(5) == "1 \n2 3 \n4 5 6 \n7 8 9 10 \n11 12 13 14 15 "