Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises(list-ops): implement #557

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5b98df4
add list-ops exercise configuration
Norbiox Jan 25, 2024
cf1d2ae
add tests file scaffold
Norbiox Jan 25, 2024
43ee62a
add tests and example solutions
Norbiox Jan 27, 2024
8fbe293
change difficulty and move higher in config
Norbiox Jan 27, 2024
ddc71e6
remove trailing newlines
Norbiox Jan 27, 2024
31a2f5c
exercises(list-ops): example: refactor `length`
ee7 Jan 27, 2024
03e10f3
exercises(list-ops): example: refactor `length` further
ee7 Jan 27, 2024
26b4b6e
exercises(list-ops): example: use result, not newList
ee7 Jan 27, 2024
c5ffbc3
exercises(list-ops): example: format loop consistently
ee7 Jan 27, 2024
aa462b6
exercises(list-ops): example: refactor `reverse`
ee7 Jan 27, 2024
4a3cd6c
exercises(list-ops): example: remove parens for `add`
ee7 Jan 27, 2024
7c3c54f
exercises(list-ops): example: refactor `map`
ee7 Jan 27, 2024
d43eea4
exercises(list-ops): example: avoid `return`
ee7 Jan 27, 2024
eedf26c
exercises(list-ops): example: use `func`
ee7 Jan 27, 2024
d628b5b
exercises(list-ops): example: wrap (now-)long lines
ee7 Jan 27, 2024
ae8ae1b
exercises(list-ops): example: refactor `append`
ee7 Jan 27, 2024
aa8f036
exercises(list-ops): tests: prefer to check len == 0
ee7 Jan 27, 2024
54aa4e7
exercises(list-ops): example: prefer openArray
ee7 Jan 27, 2024
cea855e
exercises(list-ops): example, stub, test: write `list` first
ee7 Jan 27, 2024
42896c8
exercises(list-ops): tests: remove unimplemented test cases
ee7 Jan 27, 2024
f945be9
exercises(list-ops): tests: remove UUID comments
ee7 Jan 27, 2024
ecf1542
exercises(list-ops): tests: remove float from return type
ee7 Jan 27, 2024
231798d
exercises(list-ops): example: use `toOpenArray`, not slicing
ee7 Jan 27, 2024
ce422d4
exercises(list-ops): tests.toml: exclude unimplemented test cases
ee7 Jan 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,14 @@
"integers"
]
},
{
"slug": "list-ops",
"name": "List Ops",
"uuid": "779343ba-9b40-4d43-b696-2267c4eecde0",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "linked-list",
"name": "Linked List",
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/list-ops/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Instructions

Implement basic list operations.

In functional languages list operations like `length`, `map`, and `reduce` are very common.
Implement a series of basic list operations, without using existing functions.

The precise number and names of the operations to be implemented will be track dependent to avoid conflicts with existing names, but the general operations you will implement include:

- `append` (_given two lists, add all items in the second list to the end of the first list_);
- `concatenate` (_given a series of lists, combine all items in all lists into one flattened list_);
- `filter` (_given a predicate and a list, return the list of all items for which `predicate(item)` is True_);
- `length` (_given a list, return the total number of items within it_);
- `map` (_given a function and a list, return the list of the results of applying `function(item)` on all items_);
- `foldl` (_given a function, a list, and initial accumulator, fold (reduce) each item into the accumulator from the left_);
- `foldr` (_given a function, a list, and an initial accumulator, fold (reduce) each item into the accumulator from the right_);
- `reverse` (_given a list, return a list with all the original items, but in reversed order_).

Note, the ordering in which arguments are passed to the fold functions (`foldl`, `foldr`) is significant.
17 changes: 17 additions & 0 deletions exercises/practice/list-ops/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"Norbiox"
],
"files": {
"solution": [
"list_ops.nim"
],
"test": [
"test_list_ops.nim"
],
"example": [
".meta/example.nim"
]
},
"blurb": "Implement basic list operations."
}
49 changes: 49 additions & 0 deletions exercises/practice/list-ops/.meta/example.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
func append*(list1, list2: seq[int]): seq[int] =
if list2.len == 0:
return list1
result = newSeq[int](list1.len + list2.len)
var i = 0
for x in list1:
result[i] = x
inc i
for x in list2:
result[i] = x
inc i

func concatenate*(lists: openArray[seq[int]]): seq[int] =
result = @[]
for list in lists:
result = append(result, list)

func filter*(list: openArray[int], predicate: proc(x: int): bool {.noSideEffect.}): seq[int] =
result = @[]
for x in list:
if predicate(x):
result.add x

func length*(list: openArray[int]): int =
list.len

func map*(list: openArray[int], function: proc(x: int): int {.noSideEffect.}): seq[int] =
result = newSeq[int](list.len)
for i, x in list:
result[i] = function(x)

func foldl*(list: openArray[int], function: proc(x, y: int): int {.noSideEffect.},
accumulator: int): int =
if list.len == 0:
accumulator
else:
foldl(list.toOpenArray(1, list.high), function, function(accumulator, list[0]))

func foldr*(list: openArray[int], function: proc(x, y: int): int {.noSideEffect.},
accumulator: int): int =
if list.len == 0:
accumulator
else:
function(foldr(list.toOpenArray(1, list.high), function, accumulator), list[0])

func reverse*(list: openArray[int]): seq[int] =
result = newSeq[int](list.len)
for i, x in list:
result[list.high - i] = x
108 changes: 108 additions & 0 deletions exercises/practice/list-ops/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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.

[485b9452-bf94-40f7-a3db-c3cf4850066a]
description = "append entries to a list and return the new list -> empty lists"

[2c894696-b609-4569-b149-8672134d340a]
description = "append entries to a list and return the new list -> list to empty list"

[e842efed-3bf6-4295-b371-4d67a4fdf19c]
description = "append entries to a list and return the new list -> empty list to list"

[71dcf5eb-73ae-4a0e-b744-a52ee387922f]
description = "append entries to a list and return the new list -> non-empty lists"

[28444355-201b-4af2-a2f6-5550227bde21]
description = "concatenate a list of lists -> empty list"

[331451c1-9573-42a1-9869-2d06e3b389a9]
description = "concatenate a list of lists -> list of lists"

[d6ecd72c-197f-40c3-89a4-aa1f45827e09]
description = "concatenate a list of lists -> list of nested lists"
include = false

[0524fba8-3e0f-4531-ad2b-f7a43da86a16]
description = "filter list returning only values that satisfy the filter function -> empty list"

[88494bd5-f520-4edb-8631-88e415b62d24]
description = "filter list returning only values that satisfy the filter function -> non-empty list"

[1cf0b92d-8d96-41d5-9c21-7b3c37cb6aad]
description = "returns the length of a list -> empty list"

[d7b8d2d9-2d16-44c4-9a19-6e5f237cb71e]
description = "returns the length of a list -> non-empty list"

[c0bc8962-30e2-4bec-9ae4-668b8ecd75aa]
description = "return a list of elements whose values equal the list value transformed by the mapping function -> empty list"

[11e71a95-e78b-4909-b8e4-60cdcaec0e91]
description = "return a list of elements whose values equal the list value transformed by the mapping function -> non-empty list"

[613b20b7-1873-4070-a3a6-70ae5f50d7cc]
description = "folds (reduces) the given list from the left with a function -> empty list"
include = false

[e56df3eb-9405-416a-b13a-aabb4c3b5194]
description = "folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list"
include = false

[d2cf5644-aee1-4dfc-9b88-06896676fe27]
description = "folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list"
include = false

[36549237-f765-4a4c-bfd9-5d3a8f7b07d2]
description = "folds (reduces) the given list from the left with a function -> empty list"
reimplements = "613b20b7-1873-4070-a3a6-70ae5f50d7cc"

[7a626a3c-03ec-42bc-9840-53f280e13067]
description = "folds (reduces) the given list from the left with a function -> direction independent function applied to non-empty list"
reimplements = "e56df3eb-9405-416a-b13a-aabb4c3b5194"

[d7fcad99-e88e-40e1-a539-4c519681f390]
description = "folds (reduces) the given list from the left with a function -> direction dependent function applied to non-empty list"
reimplements = "d2cf5644-aee1-4dfc-9b88-06896676fe27"

[aeb576b9-118e-4a57-a451-db49fac20fdc]
description = "folds (reduces) the given list from the right with a function -> empty list"
include = false

[c4b64e58-313e-4c47-9c68-7764964efb8e]
description = "folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list"
include = false

[be396a53-c074-4db3-8dd6-f7ed003cce7c]
description = "folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list"
include = false

[17214edb-20ba-42fc-bda8-000a5ab525b0]
description = "folds (reduces) the given list from the right with a function -> empty list"
reimplements = "aeb576b9-118e-4a57-a451-db49fac20fdc"

[e1c64db7-9253-4a3d-a7c4-5273b9e2a1bd]
description = "folds (reduces) the given list from the right with a function -> direction independent function applied to non-empty list"
reimplements = "c4b64e58-313e-4c47-9c68-7764964efb8e"

[8066003b-f2ff-437e-9103-66e6df474844]
description = "folds (reduces) the given list from the right with a function -> direction dependent function applied to non-empty list"
reimplements = "be396a53-c074-4db3-8dd6-f7ed003cce7c"

[94231515-050e-4841-943d-d4488ab4ee30]
description = "reverse the elements of the list -> empty list"

[fcc03d1e-42e0-4712-b689-d54ad761f360]
description = "reverse the elements of the list -> non-empty list"

[40872990-b5b8-4cb8-9085-d91fc0d05d26]
description = "reverse the elements of the list -> list of lists is not flattened"
include = false
23 changes: 23 additions & 0 deletions exercises/practice/list-ops/list_ops.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
proc append*(list1, list2: seq[int]): seq[int] =
discard

proc concatenate*(lists: seq[seq[int]]): seq[int] =
discard

proc filter*(list: seq[int], predicate: proc(x: int): bool): seq[int] =
discard

proc length*(list: seq[int]): int =
discard

proc map*(list: seq[int], function: proc(x: int): int): seq[int] =
discard

proc foldl*(list: seq[int], function: proc(x, y: int): int, accumulator: int): int =
discard

proc foldr*(list: seq[int], function: proc(x, y: int): int, accumulator: int): int =
discard

proc reverse*(list: seq[int]): seq[int] =
discard
112 changes: 112 additions & 0 deletions exercises/practice/list-ops/test_list_ops.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import unittest
import list_ops


suite "append entries to a list and return the new list":
test "empty lists":
check append(@[], @[]).len == 0

test "list to empty list":
check append(@[], @[1, 2, 3, 4]) == @[1, 2, 3, 4]

test "empty list to list":
check append(@[1, 2, 3, 4], @[]) == @[1, 2, 3, 4]

test "non-empty lists":
check append(@[1, 2], @[2, 3, 4, 5]) == @[1, 2, 2, 3, 4, 5]


suite "concatenate a list of lists":
test "empty list":
check concatenate(@[]).len == 0

test "list of lists":
check concatenate(@[@[1, 2], @[3], @[], @[4, 5, 6]]) == @[1, 2, 3, 4, 5, 6]


suite "filter list returning only values that satisfy the filter function":
func predicate(x: int): bool = x mod 2 == 1

test "empty list":
check filter(@[], predicate).len == 0

test "non-empty list":
check filter(@[1, 2, 3, 5], predicate) == @[1, 3, 5]


suite "returns the length of a list":
test "empty list":
check length(@[]) == 0

test "non-empty list":
check length(@[1, 2, 3, 4]) == 4


suite "return a list of elements whose values equal the list value transformed by the mapping function":
func function(x: int): int = x + 1

test "empty list":
check map(@[], function).len == 0

test "non-empty list":
check map(@[1, 3, 5, 7], function) == @[2, 4, 6, 8]


suite "folds (reduces) the given list from the left with a function":
test "empty list":
func function(x: int, y: int): int = x + y
check foldl(@[], function, 2) == 2

test "direction independent function applied to non-empty list":
func function(x: int, y: int): int = x + y
check foldl(@[1, 2, 3, 4], function, 5) == 15

test "direction dependent function applied to non-empty list":
func function(x: int, y: int): int = x - y
check foldl(@[1, 2, 3, 4], function, 24) == 14

test "empty list":
func function(x: int, y: int): int = y + x
check foldl(@[], function, 2) == 2

test "direction independent function applied to non-empty list":
func function(x: int, y: int): int = y + x
check foldl(@[1, 2, 3, 4], function, 5) == 15

test "direction dependent function applied to non-empty list":
func function(x: int, y: int): int = y - x
check foldl(@[1, 2, 3, 4], function, 5) == 7


suite "folds (reduces) the given list from the right with a function":
test "empty list":
func function(x: int, y: int): int = x + y
check foldr(@[], function, 2) == 2

test "direction independent function applied to non-empty list":
func function(x: int, y: int): int = x + y
check foldr(@[1, 2, 3, 4], function, 5) == 15

test "direction dependent function applied to non-empty list":
func function(x: int, y: int): int = x - y
check foldr(@[1, 2, 3, 4], function, 24) == 14

test "empty list":
func function(x: int, y: int): int = y + x
check foldr(@[], function, 2) == 2

test "direction independent function applied to non-empty list":
func function(x: int, y: int): int = y + x
check foldr(@[1, 2, 3, 4], function, 5) == 15

test "direction dependent function applied to non-empty list":
func function(x: int, y: int): int = y - x
check foldr(@[1, 2, 3, 4], function, 5) == 3


suite "reverse the elements of the list":
test "empty list":
check reverse(@[]).len == 0

test "non-empty list":
check reverse(@[1, 2, 3, 4]) == @[4, 3, 2, 1]
Loading