Skip to content

Commit 7b6766a

Browse files
sync flatten-array (#575)
[no important files changed]
1 parent e0efd88 commit 7b6766a

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Instructions
22

3-
Take a nested list and return a single flattened list with all values except nil/null.
3+
Take a nested array of any depth and return a fully flattened array.
44

5-
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.
5+
Note that some language tracks may include null-like values in the input array, and the way these values are represented varies by track.
6+
Such values should be excluded from the flattened array.
67

7-
For example:
8+
Additionally, the input may be of a different data type and contain different types, depending on the track.
89

9-
input: [1,[2,3,null,4],[null],5]
10+
Check the test suite for details.
1011

11-
output: [1,2,3,4,5]
12+
## Example
13+
14+
input: `[1, [2, 6, null], [[null, [4]], 5]]`
15+
16+
output: `[1, 2, 6, 4, 5]`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Introduction
2+
3+
A shipment of emergency supplies has arrived, but there's a problem.
4+
To protect from damage, the items — flashlights, first-aid kits, blankets — are packed inside boxes, and some of those boxes are nested several layers deep inside other boxes!
5+
6+
To be prepared for an emergency, everything must be easily accessible in one box.
7+
Can you unpack all the supplies and place them into a single box, so they're ready when needed most?
Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[8c71dabd-da60-422d-a290-4a571471fb14]
13+
description = "empty"
414

515
[d268b919-963c-442d-9f07-82b93f1b518c]
616
description = "no nesting"
717

18+
[3f15bede-c856-479e-bb71-1684b20c6a30]
19+
description = "flattens a nested array"
20+
821
[c84440cc-bb3a-48a6-862c-94cf23f2815d]
922
description = "flattens array with just integers present"
1023

@@ -14,8 +27,40 @@ description = "5 level nesting"
1427
[d572bdba-c127-43ed-bdcd-6222ac83d9f7]
1528
description = "6 level nesting"
1629

30+
[0705a8e5-dc86-4cec-8909-150c5e54fa9c]
31+
description = "null values are omitted from the final result"
32+
include = false
33+
34+
[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
35+
description = "consecutive null values at the front of the list are omitted from the final result"
36+
include = false
37+
38+
[bc72da10-5f55-4ada-baf3-50e4da02ec8e]
39+
description = "consecutive null values at the front of the array are omitted from the final result"
40+
include = false
41+
reimplements = "c6cf26de-8ccd-4410-84bd-b9efd88fd2bc"
42+
43+
[382c5242-587e-4577-b8ce-a5fb51e385a1]
44+
description = "consecutive null values in the middle of the list are omitted from the final result"
45+
include = false
46+
47+
[6991836d-0d9b-4703-80a0-3f1f23eb5981]
48+
description = "consecutive null values in the middle of the array are omitted from the final result"
49+
include = false
50+
reimplements = "382c5242-587e-4577-b8ce-a5fb51e385a1"
51+
1752
[ef1d4790-1b1e-4939-a179-51ace0829dbd]
1853
description = "6 level nest list with null values"
1954

55+
[dc90a09c-5376-449c-a7b3-c2d20d540069]
56+
description = "6 level nested array with null values"
57+
include = false
58+
reimplements = "ef1d4790-1b1e-4939-a179-51ace0829dbd"
59+
2060
[85721643-705a-4150-93ab-7ae398e2942d]
2161
description = "all values in nested list are null"
62+
63+
[51f5d9af-8f7f-4fb5-a156-69e8282cb275]
64+
description = "all values in nested array are null"
65+
include = false
66+
reimplements = "85721643-705a-4150-93ab-7ae398e2942d"

exercises/practice/flatten-array/flatten-array_spec.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ describe('flatten-array', function()
2525
end)
2626

2727
it('should flatten arrays with no elements to an empty array', function()
28+
assert.are.same({}, flatten({}))
29+
30+
assert.are.same({}, flatten({ { {} } }))
31+
2832
assert.are.same({}, flatten({ { { {} } }, { {} } }))
2933
end)
3034
end)

0 commit comments

Comments
 (0)