- 0
- 0
- 约1.07万字
- 约 8页
- 2023-10-12 发布于北京
- 举报
Round 13# Unflatten a list
Description
You get an array of integers and have to unflatten it by these rules:
You start at the first number.
If this number x is smaller than 3, take this number x direct
for the new array and continue with the next number.
If this number x is greater than 2, take the next x numbers ( lusive
this number) as a
sub-array in the new array. Continue with the next number AFTER this taken
numbers.
If there are too few numbers to take by number, take the last available
numbers.
The given array will always contain numbers. There will only be numbers 0.
Example
[1,4,5,2,1,2,4,5,2,6,2,3,3] - [1,[4,5,2,1],2,[4,5,2,6],2,[3,3]]
Steps:
The 1 is added directly to the new array.
The next number is 4. So the next 4 numbers (4,5,2,1) are added as sub-
array in the new array.
The 2 is added directly to the new array.
The next number is 4. So the next 4 numbers (4,5,2,6) are added as sub-
array in the new array.
The 2 is added directly to the new array.
The next number is 3. So the next 3 numbers would be taken. There are
only 2,
so take these (3,3) as sub-array in the new array.
unflatten ([ 3, 5, 2, 1 ])
[[ 3,5,2 ], 1 ]
unflatten ([ 1, 4, 5, 2, 1, 2, 4, 5, 2, 6, 2, 3, 3 ])
[1, [4,5,2,1], 2, [4,5,2,6], 2, [3, 3] ]
unflatten ([ 1, 1, 1, 1 ])
[ 1, 1, 1, 1 ]
unflatten ([ 1 ])
[ 1 ]
unflatten ([ 99, 1, 1, 1 ])
[ [ 99, 1, 1, 1 ] ]
unflatten ([ 3, 1, 1, 3, 1, 1 ])
[[3,1,1], [3,1,1]]
Start from here
def unflatten (flat_array):
#your code here
local answers
#1
def unflatten (flat_array):
ret = []
i = 0
while (i len (flat_array)):
if flat_array[i] 3:
ret.append (flat_array[i])
您可能关注的文档
最近下载
- 外研版七年级下册英语广西金秀县桐木中学七年级下学期期末考试题.pdf VIP
- 苏州市中考:《化学》2024年-2022年考试真题与参考答案.pdf
- 实用英语口语8000句.doc VIP
- 国际物流中心研论文 .doc VIP
- 2026 年苏教版七年级语文上册期末学业水平测试试卷(附答案可下载).docx VIP
- (最新)医疗机构工作人员廉洁从业九项准则试题(含答案).docx VIP
- 常用英语口语2000句.pdf VIP
- 与高频电子镇流器一起使用的LED驱动器和LED照明系统.pdf VIP
- DB44_T 2763-2025 杨梅容器育苗技术规程.docx
- DB44_T 2764-2025 杨梅鲜果快递技术规程.docx
原创力文档

文档评论(0)