News for hackers. Stuff that matters.
Re:Why it’s hard for programmers to write a program to flatt... by alain_gilbert on Saturday February 25, 2017 @11:35PM (#13735700)
Non recursive algo with python:

  def flatten(array):
      queue = [array]
      result = []
      while len(queue) > 0:
          item = queue.pop(0)
          if isinstance(item, list):
              for sub_item in item:
                  queue.append(sub_item)
          else:
              result.append(item)
      return result

  a = [1,[2,3], [4, [5,6]]]
  print 'input:', a, '\noutput:', flatten(a)

There are no comments on this comment yet.