Skip to content

Commit

Permalink
Add flatten-array exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Oct 11, 2024
1 parent ec94aae commit ab09a1c
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "flatten-array",
"name": "Flatten Array",
"uuid": "d3f7f539-dc10-4c7a-8378-7625362e8a6f",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
Expand Down
11 changes: 11 additions & 0 deletions exercises/practice/flatten-array/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Instructions

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

The challenge is to take an arbitrarily-deep nested list-like structure and produce a flattened structure without any nil/null values.

For example:

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

output: [1,2,3,4,5]
19 changes: 19 additions & 0 deletions exercises/practice/flatten-array/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"flatten-array.R"
],
"test": [
"test_flatten-array.R"
],
"example": [
".meta/example.R"
]
},
"blurb": "Take a nested list and return a single list with all values except nil/null.",
"source": "Interview Question",
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
}
10 changes: 10 additions & 0 deletions exercises/practice/flatten-array/.meta/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
flatten <- function(input) {
flattened <- c()

for (element in input) {
if (is.null(element)) continue
flattened <- c(flattened, element)
}

flattened
}
43 changes: 43 additions & 0 deletions exercises/practice/flatten-array/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[8c71dabd-da60-422d-a290-4a571471fb14]
description = "empty"

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

[3f15bede-c856-479e-bb71-1684b20c6a30]
description = "flattens a nested array"

[c84440cc-bb3a-48a6-862c-94cf23f2815d]
description = "flattens array with just integers present"

[d3d99d39-6be5-44f5-a31d-6037d92ba34f]
description = "5 level nesting"

[d572bdba-c127-43ed-bdcd-6222ac83d9f7]
description = "6 level nesting"

[0705a8e5-dc86-4cec-8909-150c5e54fa9c]
description = "null values are omitted from the final result"

[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
description = "consecutive null values at the front of the list are omitted from the final result"

[382c5242-587e-4577-b8ce-a5fb51e385a1]
description = "consecutive null values in the middle of the list are omitted from the final result"

[ef1d4790-1b1e-4939-a179-51ace0829dbd]
description = "6 level nest list with null values"

[85721643-705a-4150-93ab-7ae398e2942d]
description = "all values in nested list are null"
3 changes: 3 additions & 0 deletions exercises/practice/flatten-array/flatten-array.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flatten <- function(input) {

}
68 changes: 68 additions & 0 deletions exercises/practice/flatten-array/test_flatten-array.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
source("./flatten-array.R")
library(testthat)

test_that("empty", {
inputs <- c()
expected <- c()
expect_equal(flatten(inputs), expected)
})

test_that("no nesting", {
inputs <- c(0, 1, 2)
expected <- c(0, 1, 2)
expect_equal(flatten(inputs), expected)
})

test_that("flattens a nested array", {
inputs <- c(c(c()))
expected <- c()
expect_equal(flatten(inputs), expected)
})

test_that("flattens array with just integers present", {
inputs <- c(1, c(2, 3, 4, 5, 6, 7), 8)
expected <- c(1, 2, 3, 4, 5, 6, 7, 8)
expect_equal(flatten(inputs), expected)
})

test_that("5 level nesting", {
inputs <- c(0, 2, c(c(2, 3), 8, 100, 4, c(c(c(50)))), -2)
expected <- c(0, 2, 2, 3, 8, 100, 4, 50, -2)
expect_equal(flatten(inputs), expected)
})

test_that("6 level nesting", {
inputs <- c(1, c(2, c(c(3)), c(4, c(c(5))), 6, 7), 8)
expected <- c(1, 2, 3, 4, 5, 6, 7, 8)
expect_equal(flatten(inputs), expected)
})

test_that("null values are omitted from the final result", {
inputs <- c(1, 2, NULL)
expected <- c(1, 2)
expect_equal(flatten(inputs), expected)
})

test_that("consecutive null values at the front of the list are omitted from the final result", { # nolint
inputs <- c(NULL, NULL, 3)
expected <- c(3)
expect_equal(flatten(inputs), expected)
})

test_that("consecutive null values in the middle of the list are omitted from the final result", { # nolint
inputs <- c(1, NULL, NULL, 4)
expected <- c(1, 4)
expect_equal(flatten(inputs), expected)
})

test_that("6 level nest list with null values", {
inputs <- c(0, 2, c(c(2, 3), 8, c(c(100)), NULL, c(c(NULL))), -2)
expected <- c(0, 2, 2, 3, 8, 100, -2)
expect_equal(flatten(inputs), expected)
})

test_that("all values in nested list are null", {
inputs <- c(NULL, c(c(c(NULL))), NULL, NULL, c(c(NULL, NULL), NULL), NULL)
expected <- c()
expect_equal(flatten(inputs), expected)
})

0 comments on commit ab09a1c

Please sign in to comment.