Skip to content

Commit

Permalink
add tests and example solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiox committed Jan 27, 2024
1 parent cf1d2ae commit 43ee62a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 34 deletions.
44 changes: 44 additions & 0 deletions exercises/practice/list-ops/.meta/example.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
proc append*(list1, list2: seq[int]): seq[int] =
if list2.len == 0:
return list1
return append(list1 & list2[0], list2[1..^1])

proc concatenate*(lists: seq[seq[int]]): seq[int] =
var newList = newSeq[int]()
for list in lists:
newList = append(newList, list)
return newList

proc filter*(predicate: proc(x: int): bool, list: seq[int]): seq[int] =
var newList = newSeq[int]()
for x in list:
if predicate(x):
newList.add(x)
return newList

proc length*(list: seq[int]): int =
var len = 0
for _ in list:
len += 1
return len

proc map*(function: proc(x: int): int, list: seq[int]): seq[int] =
var newList = newSeq[int]()
for x in list:
newList.add(function(x))
return newList

proc foldl*(function: proc(x, y: int): int, list: seq[int], accumulator: int): int =
if list.len == 0:
return accumulator
return foldl(function, list[1..^1], function(accumulator, list[0]))

proc foldr*(function: proc(x, y: int): int, list: seq[int], accumulator: int): int =
if list.len == 0:
return accumulator
return function(foldr(function, list[1..^1], accumulator), list[0])

proc reverse*(list: seq[int]): seq[int] =
var newList = newSeq[int]()
for x in list: newList.insert(x, 0)
return newList
8 changes: 4 additions & 4 deletions exercises/practice/list-ops/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ 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"
# [d6ecd72c-197f-40c3-89a4-aa1f45827e09]
# description = "concatenate a list of lists -> list of nested lists"

[0524fba8-3e0f-4531-ad2b-f7a43da86a16]
description = "filter list returning only values that satisfy the filter function -> empty list"
Expand Down Expand Up @@ -102,5 +102,5 @@ 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"
# [40872990-b5b8-4cb8-9085-d91fc0d05d26]
# description = "reverse the elements of the list -> list of lists is not flattened"
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*(predicate: proc(x: int): bool, list: seq[int]): seq[int] =
discard

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

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

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

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

proc reverse*(list: seq[int]): seq[int] =
discard
76 changes: 46 additions & 30 deletions exercises/practice/list-ops/test_list_ops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,100 +4,116 @@ import list_ops

suite "append entries to a list and return the new list":
test "empty lists": # 485b9452-bf94-40f7-a3db-c3cf4850066a
discard
check append(@[], @[]) == newSeq[int]()

test "list to empty list": # 2c894696-b609-4569-b149-8672134d340a
discard
check append(@[], @[1, 2, 3, 4]) == @[1, 2, 3, 4]

test "empty list to list": # e842efed-3bf6-4295-b371-4d67a4fdf19c
discard
check append(@[1, 2, 3, 4], @[]) == @[1, 2, 3, 4]

test "non-empty lists": # 71dcf5eb-73ae-4a0e-b744-a52ee387922f
discard
check append(@[1, 2], @[2, 3, 4, 5]) == @[1, 2, 2, 3, 4, 5]


suite "concatenate a list of lists":
test "empty list": # 28444355-201b-4af2-a2f6-5550227bde21
discard
check concatenate(@[]) == newSeq[int]()

test "list of lists": # 331451c1-9573-42a1-9869-2d06e3b389a9
discard
check concatenate(@[@[1, 2], @[3], @[], @[4, 5, 6]]) == @[1, 2, 3, 4, 5, 6]

test "list of nested lists": # d6ecd72c-197f-40c3-89a4-aa1f45827e09
discard
# test "list of nested lists": # d6ecd72c-197f-40c3-89a4-aa1f45827e09
# 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": # 0524fba8-3e0f-4531-ad2b-f7a43da86a16
discard
check filter(predicate, @[]) == newSeq[int]()

test "non-empty list": # 88494bd5-f520-4edb-8631-88e415b62d24
discard
check filter(predicate, @[1, 2, 3, 5]) == @[1, 3, 5]


suite "returns the length of a list":
test "empty list": # 1cf0b92d-8d96-41d5-9c21-7b3c37cb6aad
discard
check length(@[]) == 0

test "non-empty list": # d7b8d2d9-2d16-44c4-9a19-6e5f237cb71e
discard
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": # c0bc8962-30e2-4bec-9ae4-668b8ecd75aa
discard
check map(function, @[]) == newSeq[int]()

test "non-empty list": # 11e71a95-e78b-4909-b8e4-60cdcaec0e91
discard
check map(function, @[1, 3, 5, 7]) == @[2, 4, 6, 8]


suite "folds (reduces) the given list from the left with a function":
test "empty list": # 613b20b7-1873-4070-a3a6-70ae5f50d7cc
discard
func function(x: int, y: int): int = x + y
check foldl(function, @[], 2) == 2

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

test "direction dependent function applied to non-empty list": # d2cf5644-aee1-4dfc-9b88-06896676fe27
discard
func function(x: int, y: int): int | float = x - y
check foldl(function, @[1, 2, 3, 4], 24) == 14

test "empty list": # 36549237-f765-4a4c-bfd9-5d3a8f7b07d2
discard
func function(x: int, y: int): int = y + x
check foldl(function, @[], 2) == 2

test "direction independent function applied to non-empty list": # 7a626a3c-03ec-42bc-9840-53f280e13067
discard
func function(x: int, y: int): int = y + x
check foldl(function, @[1, 2, 3, 4], 5) == 15

test "direction dependent function applied to non-empty list": # d7fcad99-e88e-40e1-a539-4c519681f390
discard
func function(x: int, y: int): int = y - x
check foldl(function, @[1, 2, 3, 4], 5) == 7


suite "folds (reduces) the given list from the right with a function":
test "empty list": # aeb576b9-118e-4a57-a451-db49fac20fdc
discard
func function(x: int, y: int): int = x + y
check foldr(function, @[], 2) == 2

test "direction independent function applied to non-empty list": # c4b64e58-313e-4c47-9c68-7764964efb8e
discard
func function(x: int, y: int): int = x + y
check foldr(function, @[1, 2, 3, 4], 5) == 15

test "direction dependent function applied to non-empty list": # be396a53-c074-4db3-8dd6-f7ed003cce7c
discard
func function(x: int, y: int): int | float = x - y
check foldr(function, @[1, 2, 3, 4], 24) == 14

test "empty list": # 17214edb-20ba-42fc-bda8-000a5ab525b0
discard
func function(x: int, y: int): int = y + x
check foldr(function, @[], 2) == 2

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

test "direction dependent function applied to non-empty list": # 8066003b-f2ff-437e-9103-66e6df474844
discard
func function(x: int, y: int): int = y - x
check foldr(function, @[1, 2, 3, 4], 5) == 3


suite "reverse the elements of the list":
test "empty list": # 94231515-050e-4841-943d-d4488ab4ee30
discard
check reverse(@[]) == newSeq[int]()

test "non-empty list": # fcc03d1e-42e0-4712-b689-d54ad761f360
discard
check reverse(@[1, 2, 3, 4]) == @[4, 3, 2, 1]

test "list of lists is not flattened": # 40872990-b5b8-4cb8-9085-d91fc0d05d26
discard
# test "list of lists is not flattened": # 40872990-b5b8-4cb8-9085-d91fc0d05d26
# check reverse(@[@[], @[1], @[2, 3]]) == @[@[2, 3], @[1], @[]]

0 comments on commit 43ee62a

Please sign in to comment.