0

Given a list of ints which could have more lists of ints, we should return a flatten list.

For example:

  • flat_list([1, [2, 2, 2], 4]) returns [1, 2, 2, 2, 4]

  • flat_list([-1, [1, [-2], 1], -1]) returns [-1, 1, -2, 1, -1]

I have thought the pseudocode as follows:

if input_list length is 1
   return input_list

for i, number in input_list
   if number is an int
     return [number] + flat_list(input_list[i+1:])
   if number is a List
     return flat_list(input_list[i+1:]

I have difficulties because of I do not know how to persist the list we build between each recursive call.

To be specific I have difficulties with the line:

return [number] + flat_list(input_list[i+1:])

Because I think returning the number as [number] won't create the desired output.

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Yone
  • 2,064
  • 5
  • 25
  • 56

0 Answers0