Skip to content

Commit

Permalink
Add List Ops exercise (#596)
Browse files Browse the repository at this point in the history
* [New Exercise]: List Ops

* Tidy up List Ops exercise
- Reduce difficulty (since most of the skeleton has been added)
- Move template helper methods to generator_plugin
- Add spacing between tests

* Fix exercise reference in List Ops skeleton
  • Loading branch information
kahgoh authored Jan 29, 2024
1 parent 4ae0a5d commit b879cbc
Show file tree
Hide file tree
Showing 10 changed files with 988 additions and 153 deletions.
624 changes: 490 additions & 134 deletions config.json

Large diffs are not rendered by default.

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": [
"kahgoh"
],
"files": {
"solution": [
"src/list_ops.cr"
],
"test": [
"spec/list_ops_spec.cr"
],
"example": [
".meta/src/example.cr"
]
},
"blurb": "Implement basic list operations."
}
59 changes: 59 additions & 0 deletions exercises/practice/list-ops/.meta/src/example.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module ListOps(T)
def self.append(list1 : Array(T), list2 : Array(T)) : Array(T)
concat([list1, list2])
end

def self.concat(lists : Array(Array(T)))
result = [] of T
lists.each { |sublist|
sublist.each { |elem| result << elem }
}
result
end

def self.filter(list : Array(T), function : T -> Bool) : Array(T)
filtered = [] of T
list.each { |elem|
if function.call(elem)
filtered << elem
end
}
filtered
end

def self.length(list : Array(T)) : Int
count = 0
list.each { |_| count = count + 1 }
count
end

def self.map(list : Array(T), function : T -> _)
mapped = [] of T
list.each { |elem| mapped << function.call(elem) }
mapped
end

def self.foldl(list : Array(T), initial : _, function : _, T -> _)
acc = initial
list.each { |elem| acc = function.call(acc, elem) }
acc
end

def self.foldr(list : Array(T), initial : _, function : _, T -> _)
acc = initial
copy = list.clone
until copy.empty?
acc = function.call(acc, copy.pop)
end
acc
end

def self.reverse(list : Array(T)) : Array(T)
reversed = [] of T
copy = list.clone
until copy.empty?
reversed << copy.pop
end
reversed
end
end
58 changes: 58 additions & 0 deletions exercises/practice/list-ops/.meta/test_template.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "spec"
require "../src/*"

<%-
# These are uuids of the test cases that use nested arrays.
tests_with_nested = ["d6ecd72c-197f-40c3-89a4-aa1f45827e09", "40872990-b5b8-4cb8-9085-d91fc0d05d26"]
-%>
describe "<%-= to_capitalized(@json["exercise"].to_s) %>" do
<%- @json["cases"].as_a.each do |describe| -%>
describe "<%= describe["description"].to_s %>" do
<%- describe["cases"].as_a.each do |cases| -%>
<%= status() %> "<%= cases["description"] %>" do
<%- if cases["property"].to_s == "append" -%>
ListOps.<%= cases["property"] %>(<%= list_ops_array(cases["input"]["list1"]) %>, <%= list_ops_array(cases["input"]["list2"]) %>).should eq(<%= list_ops_array(cases["expected"]) %>)
<%- end -%>
<%- if cases["property"].to_s == "concat" -%>
ListOps.<%= cases["property"] %>(<%= list_ops_nestable(cases["input"]["lists"]) %>).should eq(<%=
if tests_with_nested.any?(cases["uuid"])
list_ops_nestable(cases["expected"])
else
list_ops_array(cases["expected"])
end%>)
<%- end -%>
<%- if cases["property"].to_s == "filter" -%>
filter = <%= list_ops_function(cases["input"]["function"], ["Int32"]) %>
ListOps.<%= cases["property"] %>(<%= list_ops_array(cases["input"]["list"]) %>, filter).should eq(<%= list_ops_array(cases["expected"]) %>)
<%- end -%>
<%- if cases["property"].to_s == "length" -%>
ListOps.<%= cases["property"] %>(<%= list_ops_array(cases["input"]["list"]) %>).should eq(<%= cases["expected"] %>)
<%- end -%>
<%- if cases["property"].to_s == "map" -%>
mapper = <%= list_ops_function(cases["input"]["function"], ["Int32"]) %>
ListOps.<%= cases["property"] %>(<%= list_ops_array(cases["input"]["list"]) %>, mapper).should eq(<%= list_ops_array(cases["expected"]) %>)
<%- end -%>
<%- if ["foldl", "foldr"].any?(cases["property"]) -%>
<%- # If the function performs a division, use Float64 -%>
<%- if cases["input"]["function"].to_s.matches?(/.*->.*\/.*/) -%>
folder = <%= list_ops_function(cases["input"]["function"], ["Float64", "Int32"]) %>
ListOps.<%= cases["property"] %>(<%= list_ops_array(cases["input"]["list"]) %>, <%= cases["input"]["initial"] %>.to_f64, folder).should eq(<%= cases["expected"] %>.to_f64)
<%- else -%>
folder = <%= list_ops_function(cases["input"]["function"], ["Int32", "Int32"]) %>
ListOps.<%= cases["property"] %>(<%= list_ops_array(cases["input"]["list"]) %>, <%= cases["input"]["initial"] %>, folder).should eq(<%= cases["expected"] %>)
<%- end -%>
<%- end -%>
<%- if cases["property"].to_s == "reverse" -%>
<%- if tests_with_nested.any?(cases["uuid"]) -%>
ListOps.<%= cases["property"] %>(<%= list_ops_nestable(cases["input"]["list"]) %>).should eq(<%= list_ops_nestable(cases["expected"]) %>)
<%- else -%>
ListOps.<%= cases["property"] %>(<%= list_ops_array(cases["input"]["list"]) %>).should eq(<%= list_ops_array(cases["expected"]) %>)
<%- end -%>
<%- end -%>
end

<%- end -%>
end

<%- end -%>
end
106 changes: 106 additions & 0 deletions exercises/practice/list-ops/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# 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"

[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"
118 changes: 118 additions & 0 deletions exercises/practice/list-ops/spec/list_ops_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
require "spec"
require "../src/*"

describe "ListOps" do
describe "append entries to a list and return the new list" do
it "empty lists" do
ListOps.append([] of Int32, [] of Int32).should eq([] of Int32)
end

pending "list to empty list" do
ListOps.append([] of Int32, [1, 2, 3, 4]).should eq([1, 2, 3, 4])
end

pending "empty list to list" do
ListOps.append([1, 2, 3, 4], [] of Int32).should eq([1, 2, 3, 4])
end

pending "non-empty lists" do
ListOps.append([1, 2], [2, 3, 4, 5]).should eq([1, 2, 2, 3, 4, 5])
end
end

describe "concatenate a list of lists" do
pending "empty list" do
ListOps.concat([] of Array(Int32)).should eq([] of Int32)
end

pending "list of lists" do
ListOps.concat([[1, 2], [3], [] of Int32, [4, 5, 6]]).should eq([1, 2, 3, 4, 5, 6])
end

pending "list of nested lists" do
ListOps.concat([[[1], [2]], [[3]], [[] of Int32], [[4, 5, 6]]]).should eq([[1], [2], [3], [] of Int32, [4, 5, 6]])
end
end

describe "filter list returning only values that satisfy the filter function" do
pending "empty list" do
filter = ->(x : Int32) { x % 2 == 1 }
ListOps.filter([] of Int32, filter).should eq([] of Int32)
end

pending "non-empty list" do
filter = ->(x : Int32) { x % 2 == 1 }
ListOps.filter([1, 2, 3, 5], filter).should eq([1, 3, 5])
end
end

describe "returns the length of a list" do
pending "empty list" do
ListOps.length([] of Int32).should eq(0)
end

pending "non-empty list" do
ListOps.length([1, 2, 3, 4]).should eq(4)
end
end

describe "return a list of elements whose values equal the list value transformed by the mapping function" do
pending "empty list" do
mapper = ->(x : Int32) { x + 1 }
ListOps.map([] of Int32, mapper).should eq([] of Int32)
end

pending "non-empty list" do
mapper = ->(x : Int32) { x + 1 }
ListOps.map([1, 3, 5, 7], mapper).should eq([2, 4, 6, 8])
end
end

describe "folds (reduces) the given list from the left with a function" do
pending "empty list" do
folder = ->(acc : Int32, el : Int32) { el * acc }
ListOps.foldl([] of Int32, 2, folder).should eq(2)
end

pending "direction independent function applied to non-empty list" do
folder = ->(acc : Int32, el : Int32) { el + acc }
ListOps.foldl([1, 2, 3, 4], 5, folder).should eq(15)
end

pending "direction dependent function applied to non-empty list" do
folder = ->(acc : Float64, el : Int32) { el / acc }
ListOps.foldl([1, 2, 3, 4], 24.to_f64, folder).should eq(64.to_f64)
end
end

describe "folds (reduces) the given list from the right with a function" do
pending "empty list" do
folder = ->(acc : Int32, el : Int32) { el * acc }
ListOps.foldr([] of Int32, 2, folder).should eq(2)
end

pending "direction independent function applied to non-empty list" do
folder = ->(acc : Int32, el : Int32) { el + acc }
ListOps.foldr([1, 2, 3, 4], 5, folder).should eq(15)
end

pending "direction dependent function applied to non-empty list" do
folder = ->(acc : Float64, el : Int32) { el / acc }
ListOps.foldr([1, 2, 3, 4], 24.to_f64, folder).should eq(9.to_f64)
end
end

describe "reverse the elements of the list" do
pending "empty list" do
ListOps.reverse([] of Int32).should eq([] of Int32)
end

pending "non-empty list" do
ListOps.reverse([1, 3, 5, 7]).should eq([7, 5, 3, 1])
end

pending "list of lists is not flattened" do
ListOps.reverse([[1, 2], [3], [] of Int32, [4, 5, 6]]).should eq([[4, 5, 6], [] of Int32, [3], [1, 2]])
end
end
end
Loading

0 comments on commit b879cbc

Please sign in to comment.