Click here to Skip to main content
15,877,966 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],
Example2: input: list = [1,2,3,4,5] output: [[1,2,3,4,5],

What I have tried:

aList = []
while ogList != []:
  ogList = ogList[1:-1]
return aList
Updated 4-Sep-22 20:45pm

1 solution

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

olist = []
recur_triangle([1,2,3,4,5], 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