Skip to content

Commit 1237797

Browse files
authored
Sync flatten-array (#921)
Closes #910
1 parent 9a6f9f2 commit 1237797

File tree

7 files changed

+150
-56
lines changed

7 files changed

+150
-56
lines changed

bin/auto-sync.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ difference-of-squares
2424
dnd-character
2525
eliuds-eggs
2626
etl
27+
flatten-array
2728
flower-field
2829
food-chain
2930
gigasecond
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?

exercises/practice/flatten-array/.meta/config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"arueckauer",
77
"kunicmarko20",
88
"kytrinyx",
9-
"neenjaw"
9+
"neenjaw",
10+
"A-O-Emmanuel"
1011
],
1112
"files": {
1213
"solution": [
@@ -19,7 +20,7 @@
1920
".meta/example.php"
2021
]
2122
},
22-
"blurb": "Take a nested list and return a single list with all values except nil/null",
23+
"blurb": "Take a nested list and return a single list with all values except nil/null.",
2324
"source": "Interview Question",
2425
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
2526
}

exercises/practice/flatten-array/.meta/example.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
function flatten($array = [])
Lines changed: 45 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,37 @@ 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+
33+
[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
34+
description = "consecutive null values at the front of the list are omitted from the final result"
35+
include = false
36+
37+
[bc72da10-5f55-4ada-baf3-50e4da02ec8e]
38+
description = "consecutive null values at the front of the array are omitted from the final result"
39+
reimplements = "c6cf26de-8ccd-4410-84bd-b9efd88fd2bc"
40+
41+
[382c5242-587e-4577-b8ce-a5fb51e385a1]
42+
description = "consecutive null values in the middle of the list are omitted from the final result"
43+
include = false
44+
45+
[6991836d-0d9b-4703-80a0-3f1f23eb5981]
46+
description = "consecutive null values in the middle of the array are omitted from the final result"
47+
reimplements = "382c5242-587e-4577-b8ce-a5fb51e385a1"
48+
1749
[ef1d4790-1b1e-4939-a179-51ace0829dbd]
1850
description = "6 level nest list with null values"
51+
include = false
52+
53+
[dc90a09c-5376-449c-a7b3-c2d20d540069]
54+
description = "6 level nested array with null values"
55+
reimplements = "ef1d4790-1b1e-4939-a179-51ace0829dbd"
1956

2057
[85721643-705a-4150-93ab-7ae398e2942d]
2158
description = "all values in nested list are null"
59+
include = false
60+
61+
[51f5d9af-8f7f-4fb5-a156-69e8282cb275]
62+
description = "all values in nested array are null"
63+
reimplements = "85721643-705a-4150-93ab-7ae398e2942d"

exercises/practice/flatten-array/FlattenArrayTest.php

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\TestDox;
287

298
class FlattenArrayTest extends TestCase
309
{
@@ -33,40 +12,121 @@ public static function setUpBeforeClass(): void
3312
require_once 'FlattenArray.php';
3413
}
3514

15+
/**
16+
* uuid 8c71dabd-da60-422d-a290-4a571471fb14
17+
*/
18+
#[TestDox('empty')]
19+
public function testEmpty(): void
20+
{
21+
$input = [];
22+
$expected = [];
23+
$this->assertEquals($expected, flatten($input));
24+
}
25+
26+
/**
27+
* uuid d268b919-963c-442d-9f07-82b93f1b518c
28+
*/
29+
#[TestDox('no nesting')]
3630
public function testWithOutNesting(): void
3731
{
3832
$input = [0, 1, 2];
3933
$expected = [0, 1, 2];
4034
$this->assertEquals($expected, flatten($input));
4135
}
36+
37+
/**
38+
* uuid 3f15bede-c856-479e-bb71-1684b20c6a30
39+
*/
40+
#[TestDox('flattens a nested array')]
41+
public function testFlattensANestedArray(): void
42+
{
43+
$input = [[[]]];
44+
$expected = [];
45+
$this->assertEquals($expected, flatten($input));
46+
}
47+
48+
/**
49+
* uuid c84440cc-bb3a-48a6-862c-94cf23f2815d
50+
*/
51+
#[TestDox('flattens array with just integers present')]
4252
public function testArrayWithJustIntegersPresent(): void
4353
{
4454
$input = [1, [2, 3, 4, 5, 6, 7], 8];
4555
$expected = [1, 2, 3, 4, 5, 6, 7, 8];
4656
$this->assertEquals($expected, flatten($input));
4757
}
4858

59+
/**
60+
* uuid d3d99d39-6be5-44f5-a31d-6037d92ba34f
61+
*/
62+
#[TestDox('5 level nesting')]
4963
public function testFiveLevelNesting(): void
5064
{
5165
$input = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2];
5266
$expected = [0, 2, 2, 3, 8, 100, 4, 50, -2];
5367
$this->assertEquals($expected, flatten($input));
5468
}
5569

70+
/**
71+
* uuid d572bdba-c127-43ed-bdcd-6222ac83d9f7
72+
*/
73+
#[TestDox('6 level nesting')]
5674
public function testSixLevelNesting(): void
5775
{
5876
$input = [1, [2, [[3]], [4, [[5]]], 6, 7], 8];
5977
$expected = [1, 2, 3, 4, 5, 6, 7, 8];
6078
$this->assertEquals($expected, flatten($input));
6179
}
62-
public function testSixLevelNestListWithNullValues(): void
80+
81+
/**
82+
* uuid 0705a8e5-dc86-4cec-8909-150c5e54fa9c
83+
*/
84+
#[TestDox('null values are omitted from the final result')]
85+
public function testNullValuesAreOmittedFromTheFinalResult(): void
86+
{
87+
$input = [1, 2, null];
88+
$expected = [1, 2];
89+
$this->assertEquals($expected, flatten($input));
90+
}
91+
92+
/**
93+
* uuid bc72da10-5f55-4ada-baf3-50e4da02ec8e
94+
*/
95+
#[TestDox('consecutive null values at the front of the array are omitted from the final result')]
96+
public function testConsecutiveNullValuesAtTheFrontOfTheArrayAreOmittedFromTheFinalResult(): void
97+
{
98+
$input = [null, null, 3];
99+
$expected = [3];
100+
$this->assertEquals($expected, flatten($input));
101+
}
102+
103+
/**
104+
* uuid 6991836d-0d9b-4703-80a0-3f1f23eb5981
105+
*/
106+
#[TestDox('consecutive null values in the middle of the array are omitted from the final result')]
107+
public function testConsecutiveNullValuesInTheMiddleOfTheArrayAreOmittedFromTheFinalResult(): void
108+
{
109+
$input = [1, null, null, 4];
110+
$expected = [1, 4];
111+
$this->assertEquals($expected, flatten($input));
112+
}
113+
114+
/**
115+
* uuid dc90a09c-5376-449c-a7b3-c2d20d540069
116+
*/
117+
#[TestDox('6 level nested array with null values')]
118+
public function testSixLevelNestedArrayWithNullValues(): void
63119
{
64120
$input = [0, 2, [[2, 3], 8, [[100]], null, [[null]]], -2];
65121
$expected = [0, 2, 2, 3, 8, 100, -2];
66122
$this->assertEquals($expected, flatten($input));
67123
}
68124

69-
public function testAllValuesInNestedListAreNull(): void
125+
/**
126+
* uuid 51f5d9af-8f7f-4fb5-a156-69e8282cb275
127+
*/
128+
#[TestDox('all values in nested array are null')]
129+
public function testAllValuesInNestedArrayAreNull(): void
70130
{
71131
$input = [null, [[[null]]], null, null, [[null, null], null], null];
72132
$expected = [];

0 commit comments

Comments
 (0)