Click here to Skip to main content
15,513,489 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So I am trying to find a solution to printing one list that contains sublist of the given lists that creates a pyramid and if the given list is empty, return the empty list. If n is even, the last list should be the empty list. If n is odd, the last list should contain a single value. What I have is currently doing a infinite loop of an empty list
Example: input: list = [1,2,3,4] output: [[1,2,3,4],
                                          [2,3],
                                          []]
Example2: input: list = [1,2,3,4,5] output: [[1,2,3,4,5],
                                             [2,3,4],
                                              [3]]


What I have tried:

Python
aList = []
while ogList != []:
  aList.append(ogList)
  ogList = ogList[1:-1]
return aList
Posted
Updated 4-Sep-22 21:45pm
v2

1 solution

You correctly identified the recursive step, but failed to implement the recursive function. Try
Python
def recur_triangle(ilist, olist):
  olist.append(ilist)
  if len(ilist) < 2:
    return
  recur_triangle(ilist[1:-1], olist)

olist = []
recur_triangle([1,2,3,4,5], olist)
print(olist)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900