Python:每日一题 172
本帖最后由 冬雪雪冬 于 2018-3-28 19:32 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:
这次还是一道简单的题目。
建立一个函数newlist(n),参数n为0或正整数,返回一个列表。
当n = 0
返回
[]
当n = 1
返回
[]
当n = 2
返回
[,
]
当n = 3
返回
[,
]
当n = 4
返回
[,
,
]
......
当n = 12
返回
[,
,
,
,
]
即n = 0返回空列表,n >= 1 返回二维列表,子列表的最大长度分别为1,2,3,4...... 本帖最后由 graceasyi 于 2018-3-28 09:31 编辑
抢个沙发~{:7_113:}
def newlist(n):
if n:
lbd = lambda x: sum(range(1, x+1))
r =
t =
return for i in r]
else:
return []
for n in range(20):
print(newlist(n))
结果:
[]
[]
[, ]
[, ]
[, , ]
[, , ]
[, , ]
[, , , ]
[, , , ]
[, , , ]
[, , , ]
[, , , , ]
[, , , , ]
[, , , , ]
[, , , , ]
[, , , , ]
[, , , , , ]
[, , , , , ]
[, , , , , ]
[, , , , , ]
答案 def newlist(n):
flag = 1
num = 1
my_list = []
while n>num:
if num+flag<=n+1:
list0 =
else:
list0 =
my_list.append(list0)
flag += 1
num = list0[-1]+1
return my_list def newlist(n):
num = 1
s = -1
list1 = []
f = True
for i in range(1,n + 1):
if num > s:
list1.append()
num = 0
num += 1
s += 1
else:
list1.append(i)
num += 1
for i in list1:
if i == list1:
print('['+str(i)+',')
elif i == list1[-1]:
print(str(i)+']')
else:
print(str(i)+',')
newlist(12) def newlist(n):
num = 1
s = -1
list1 = []
f = True
for i in range(1,n+1):
if num > s:
list1.append()
num = 0
num +=1
s +=1
else:
list1.append(i)
num+=1
for i in list1:
if i == list1:
print('['+str(i)+',')
elif i == list1[-1]:
print(str(i)+']')
else:
print(str(i)+',') def newlist(n):
a=list(range(1,n+1))
t=[]
i=0
while a!=[]:
if a[:i]!=[]:
t.append(a[:i])
a=a
i+=1
return t
print(newlist(0))
print(newlist(1))
print(newlist(3))
print(newlist(4))
print(newlist(12))
#!/usr/bin/python
def f(n):
result = []
if n == 0:
return result
elif n > 0:
i = 1
length = 1
result.append([])
while i < n + 1:
if len(result[-1])<length:
result[-1].append(i)
i += 1
else:
length += 1
result.append([])
return result
print(f(0))
print(f(1))
print(f(2))
print(f(3))
print(f(4))
print(f(12))
print(f(18))
##输出结果:
[]
[]
[, ]
[, ]
[, , ]
[, , , , ]
[, , , , , ] def newlist(n):
lis = []
if n < 0:
return 'Error'
elif n == 0:
return lis
else:
lis1 =
k = 1
while len(lis1) > k:
lis2 = lis1[:k]
lis1 = lis1
k += 1
lis.append(lis2)
if len(lis1) != 0:
lis.append(lis1)
return lis
n = int(input('请输入自然数:\n'))
result = newlist(n)
print(result) def newlist(n):
if n == 0:
s = []
print(s)
return []
else:
new = []
for i in range(1,n+1):
new.append(i)
#return(new)
l =len(new)
s = []
for i in range(1,n+1):
#y = []
a = int((i-1)*i/2)
b = int(i*(i+1)/2)
if a <=l:
y = new
s.append(y)
else:
break
print(s)
return s
不知道怎么在列表中加入换行符,学习 本帖最后由 坑得飞起 于 2018-3-27 20:48 编辑
n=int(input())
i,j,s1,s2=1,1,[],[]
while (i<=n)and(n>0) :
if i==int(0.5*j*j+0.5*j) :
s2.append(i)
s1.append(s2)
s2,j=[],j+1
else :
s2.append(i)
i+=1
if s2!=[] :
s1.append(s2)
print(s1)
{:10_256:} {:10_256:} {:10_256:} def newlist(n):
if n == 0:
return []
else:
new_list = []
line_list = []
lines = 1
for i in range(1,n+1):
line_list.append(i)
if i == n:
new_list.append(line_list)
else:
if len(line_list) == lines:
new_list.append(line_list)
line_list = []
lines += 1
return new_list def newlist(num):
stringlist = []
for i in range(num, 0, -1):
stringlist.append(i)
i = 1
listnew = []
while len(stringlist) > 0:
tmpstring = []
for x in range(0, i):
if len(stringlist) > 0:
tmpstring.append(stringlist.pop())
i += 1
listnew.append(tmpstring)
return listnew
print(newlist(23))
def newliest(n):
if n == 0:
return []
elif n == 1:
return []
elif n == 2:
return [,]
else:
l =
new_l = []
s = 0
for i in range(1,n):
if s<len(l):
new_l.append(l)
s += i
return new_l rsm 发表于 2018-3-27 21:41
def newliest(n):
if n == 0:
return []
方法挺蠢的{:5_99:} def newlist(n):
if n < 0:
print '参数错误!函数仅支持非负数参数!'
return
elif n == 0:
return []
else:
numlist_2d = []
start = 1
length = 1
temp = n
while temp > 0:
numlist_2d.append()
start = start + length
length += 1
temp -= length
else:
if n > numlist_2d[-1][-1]:
numlist_2d.append([-1] + 1, n+1)])
returnnumlist_2d
print newlist(19) 本帖最后由 yunying12321 于 2018-3-27 22:04 编辑
def newlist(n):
lst = []
m = []
x = count = 1
if n > 0:
while True:
for ss in range(count):
m.append(x)
x += 1
if x > n:
break
lst.append(m)
m = []
count += 1
if x > n:
break
return lst
if __name__ == '__main__':
while True:
n = int(input('输入整数:'))
print(newlist(n))
def array_0(n):
list1=[]
w=1
while n:
if (1+w)*w/2 < n:
list1.append(*w)
w = w + 1
else:
list1.append(* int(n-(w-1)*w/2))
break
return list1
def newlist(n):
if n == 0:
list2=[]
else:
list2=array_0(n)
m=1
for i in range(n):
for j in range(i+1):
if m<=n:
list2 = m
m=m+1
print(list2)
newlist(16)
def newlist(n):
res = []
if n == 0:
return res
else:
num = 1
row = 1
while row * (row + 1) / 2 < n:
row += 1
for i in range(1, row + 1):
s = []
for each in range(i):
if num <= n:
s.append(num)
num += 1
res.append(s)
return res
n = int(input())
print(newlist(n))
def newlist(n):
if n==0:
return []
elif n==1:
return []
else:
k=[]
i=1
lis = list(range(1,n+1))
whilelen(lis) !=0:
print('i',i)
m = lis[:i]
print(m)
print(lis)
k.append(m)
lis =lis
i+=1
returnk