Avatar of JaeHyoLee

JaeHyoLee's solution

to Flatten Array in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Take a nested list and return a single flattened list with all values except nil/null.

The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.

For Example

input: [1,[2,3,null,4],[null],5]

output: [1,2,3,4,5]

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

Interview Question https://reference.wolfram.com/language/ref/Flatten.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

flatten-array_spec.lua

--[[-
Note that because ipairs and the length operator work correctly only for sequences
in Lua, no arrays with nils are included in these tests. This means that the provided
example is not included here:

  input: [1,[2,3,null,4],[null],5]
  output: [1,2,3,4,5]

See https://www.lua.org/manual/5.3/manual.html#3.4.7 for more information.
]]

local flatten = require 'flatten-array'

describe('flatten-array', function()
  it('should flatten arrays with a single level of nesting', function()
    assert.are.same(
      { 1, 2, 3, 4, 5, 6, 7, 8 },
      flatten({ 1, { 2, 3, 4, 5, 6, 7 }, 8 })
    )
  end)

  it('should flatten deeply nested arrays', function()
    assert.are.same(
      { 0, 2, 2, 3, 8, 100, 4, 50, -2 },
      flatten({ 0, 2, { { 2, 3 }, 8, 100, 4, { { { 50 } } } }, -2 })
    )

    assert.are.same(
      { 1,2,3,4,5,6,7,8 },
      flatten({ 1, { 2, { { 3 } }, { 4, { { 5 } } }, 6, 7 }, 8 })
    )
  end)

  it('should flatten arrays containing empty arrays', function()
    assert.are.same(
      { 0, 2, 2, 3, 8, 100, -2 },
      flatten({ 0, 2, { { 2, 3 }, 8, { { 100 } }, { {} } }, -2 })
    )
  end)

  it('should flatten arrays with no elements to an empty array', function()
    assert.are.same(
      {},
      flatten({ { { {  } } }, { { } } })
    )
  end)
end)
local function flatten_array(s, flatten_result)
  local flatten = {}
  for k, v in ipairs(s) do
    if (type(v) == 'table') then
      flatten_array(v, flatten_result or flatten)
    else
      table.insert(flatten_result or flatten, v)
    end
  end
  return flatten_result or flatten
end

return flatten_array

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?