蟒蛇第13轮13 atten列表描述你得到一个整数数组并且必须按.pdfVIP

  • 0
  • 0
  • 约1.07万字
  • 约 8页
  • 2023-10-12 发布于北京
  • 举报

蟒蛇第13轮13 atten列表描述你得到一个整数数组并且必须按.pdf

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])

文档评论(0)

1亿VIP精品文档

相关文档