Skip to content

Commit

Permalink
Add flatten-array
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Jul 31, 2024
1 parent 93073b7 commit dfef21a
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "flatten-array",
"name": "Flatten Array",
"uuid": "963cd26f-59ef-4651-86a2-bb1018d32523",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "prime-factors",
"name": "Prime Factors",
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": [
"keiravillekode"
],
"files": {
"solution": [
"src/FlattenArray.idr"
],
"test": [
"test/src/Main.idr"
],
"example": [
"example/FlattenArray.idr"
]
},
"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"
}
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"
11 changes: 11 additions & 0 deletions exercises/practice/flatten-array/example/FlattenArray.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module FlattenArray

public export
data Tree a = Hollow | Leaf a | Branch (List (Tree a))

export
flatten : Tree a -> List a
flatten Hollow = []
flatten (Leaf n) = [n]
flatten (Branch []) = []
flatten (Branch (x::xs)) = (flatten x) ++ (flatten (Branch xs))
3 changes: 3 additions & 0 deletions exercises/practice/flatten-array/flatten-array.ipkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package flatten-array
modules = FlattenArray
sourcedir = "src"
10 changes: 10 additions & 0 deletions exercises/practice/flatten-array/pack.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[custom.all.flatten-array]
type = "local"
path = "."
ipkg = "flatten-array.ipkg"
test = "test/test.ipkg"

[custom.all.flatten-array-test]
type = "local"
path = "test"
ipkg = "test.ipkg"
8 changes: 8 additions & 0 deletions exercises/practice/flatten-array/src/FlattenArray.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module FlattenArray

public export
data Tree a = Hollow | Leaf a | Branch (List (Tree a))

export
flatten : Tree a -> List a
flatten tree = ?flatten_rhs
30 changes: 30 additions & 0 deletions exercises/practice/flatten-array/test/src/Main.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Main

import System
import Tester
import Tester.Runner

import FlattenArray

tests : List Test
tests =
[ test "empty" (assertEq (flatten $ Branch []) [])
, test "no nesting" (assertEq (flatten $ Branch [Leaf 0, Leaf 1, Leaf 2]) [0, 1, 2])
, test "flattens a nested array" (assertEq (flatten $ Branch [Branch [Branch []]]) [])
, test "flattens array with just integers present" (assertEq (flatten $ Branch [Leaf 1, Branch [Leaf 2, Leaf 3, Leaf 4, Leaf 5, Leaf 6, Leaf 7], Leaf 8]) [1, 2, 3, 4, 5, 6, 7, 8])
, test "5 level nesting" (assertEq (flatten $ Branch [Leaf 0, Leaf 2, Branch [Branch [Leaf 2, Leaf 3], Leaf 8, Leaf 100, Leaf 4, Branch [Branch [Branch [Leaf 50]]]], Leaf (-2)]) [0, 2, 2, 3, 8, 100, 4, 50, -2])
, test "6 level nesting" (assertEq (flatten $ Branch [Leaf 1, Branch [Leaf 2, Branch [Branch [Leaf 3]], Branch [Leaf 4, Branch [Branch [Leaf 5]]], Leaf 6, Leaf 7], Leaf 8]) [1, 2, 3, 4, 5, 6, 7, 8])
, test "null values are omitted from the final result" (assertEq (flatten $ Branch [Leaf 1, Leaf 2, Hollow]) [1, 2])
, test "consecutive null values at the front of the list are omitted from the final result" (assertEq (flatten $ Branch [Hollow, Hollow, Leaf 3]) [3])
, test "consecutive null values in the middle of the list are omitted from the final result" (assertEq (flatten $ Branch [Leaf 1, Hollow, Hollow, Leaf 4]) [1, 4])
, test "6 level nest list with null values" (assertEq (flatten $ Branch [Leaf 0, Leaf 2, Branch [Branch [Leaf 2, Leaf 3], Leaf 8, Branch [Branch [Leaf 100]], Hollow, Branch [Branch [Hollow]]], Leaf (-2)]) [0, 2, 2, 3, 8, 100, -2])
, test "all values in nested list are null" (assertEq (flatten $ Branch [Hollow, Branch [Branch [Branch [Hollow]]], Hollow, Hollow, Branch [Branch [Hollow, Hollow], Hollow], Hollow]) [])
]

export
main : IO ()
main = do
success <- runTests tests
if success
then putStrLn "All tests passed"
else exitFailure
6 changes: 6 additions & 0 deletions exercises/practice/flatten-array/test/test.ipkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package flatten-array-test
depends = flatten-array
, tester
main = Main
executable = "flatten-array-test"
sourcedir = "src"
14 changes: 14 additions & 0 deletions generators/exercises/flatten_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

def generate_test(case):
def to_tree(data):
if data is None:
return "Hollow"
if isinstance(data, list):
return "Branch " + str(list(map(to_tree, data))).replace("'", "")
if data < 0:
data = f"({data})"
return f"Leaf {data}"
property = case["property"]
expected = case["expected"]
tree = to_tree(case["input"]["array"])
return f'assertEq ({property} $ {tree}) {expected}'

0 comments on commit dfef21a

Please sign in to comment.