-
-
Notifications
You must be signed in to change notification settings - Fork 228
feat: add list-ops exercise #801
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"vaeng" | ||
], | ||
"files": { | ||
"solution": [ | ||
"list_ops.cpp", | ||
"list_ops.h" | ||
], | ||
"test": [ | ||
"list_ops_test.cpp" | ||
], | ||
"example": [ | ||
".meta/example.cpp", | ||
".meta/example.h" | ||
] | ||
}, | ||
"blurb": "Implement basic list operations." | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "list_ops.h" | ||
|
||
namespace list_ops { | ||
|
||
} // namespace list_ops |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <vector> | ||
|
||
namespace list_ops { | ||
|
||
template <typename T> | ||
void append(std::vector<T>& left, const std::vector<T>& right) { | ||
for (auto element : right) { | ||
left.emplace_back(element); | ||
} | ||
} | ||
|
||
template <typename T> | ||
std::vector<T> concat(const std::vector<std::vector<T>>& input) { | ||
std::vector<T> result{}; | ||
for (auto vec : input) { | ||
for (auto element : vec) { | ||
result.emplace_back(element); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
template <typename T, typename S> | ||
std::vector<T> filter(const std::vector<T>& input, S&& func) { | ||
std::vector<T> result{}; | ||
for (auto element : input) { | ||
if (func(element)) { | ||
result.emplace_back(element); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
template <typename T> | ||
size_t length(const std::vector<T>& input) { | ||
size_t counter{}; | ||
for ([[maybe_unused]] const auto& element : input) { | ||
++counter; | ||
} | ||
return counter; | ||
} | ||
|
||
template <typename T, typename S> | ||
std::vector<T> map(const std::vector<T>& input, S&& func) { | ||
std::vector<T> result{}; | ||
for (auto element : input) { | ||
result.emplace_back(func(element)); | ||
} | ||
return result; | ||
} | ||
|
||
template <typename T, typename S> | ||
T foldl(const std::vector<T>& input, T initial_value, S&& func) { | ||
T accumulator{initial_value}; | ||
for (auto element : input) { | ||
accumulator = func(accumulator, element); | ||
} | ||
return accumulator; | ||
} | ||
|
||
template <typename T, typename S> | ||
T foldr(const std::vector<T>& input, T initial_value, S&& func) { | ||
T accumulator{initial_value}; | ||
for (unsigned i = input.size(); i-- > 0;) { | ||
accumulator = func(accumulator, input.at(i)); | ||
} | ||
return accumulator; | ||
} | ||
|
||
template <typename T> | ||
std::vector<T> reverse(const std::vector<T>& input) { | ||
std::vector<T> result{}; | ||
for (unsigned i = input.size(); i-- > 0;) { | ||
result.emplace_back(input.at(i)); | ||
} | ||
return result; | ||
} | ||
|
||
} // namespace list_ops |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Get the exercise name from the current directory | ||
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) | ||
|
||
# Basic CMake project | ||
cmake_minimum_required(VERSION 3.5.1) | ||
|
||
# Name the project after the exercise | ||
project(${exercise} CXX) | ||
|
||
# Get a source filename from the exercise name by replacing -'s with _'s | ||
string(REPLACE "-" "_" file ${exercise}) | ||
|
||
# Implementation could be only a header | ||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp) | ||
set(exercise_cpp ${file}.cpp) | ||
else() | ||
set(exercise_cpp "") | ||
endif() | ||
|
||
# Use the common Catch library? | ||
if(EXERCISM_COMMON_CATCH) | ||
# For Exercism track development only | ||
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>) | ||
elseif(EXERCISM_TEST_SUITE) | ||
# The Exercism test suite is being run, the Docker image already | ||
# includes a pre-built version of Catch. | ||
find_package(Catch2 REQUIRED) | ||
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h) | ||
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain) | ||
# When Catch is installed system wide we need to include a different | ||
# header, we need this define to use the correct one. | ||
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE) | ||
else() | ||
# Build executable from sources and headers | ||
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp) | ||
endif() | ||
|
||
set_target_properties(${exercise} PROPERTIES | ||
CXX_STANDARD 17 | ||
CXX_STANDARD_REQUIRED OFF | ||
CXX_EXTENSIONS OFF | ||
) | ||
|
||
set(CMAKE_BUILD_TYPE Debug) | ||
|
||
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)") | ||
set_target_properties(${exercise} PROPERTIES | ||
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror" | ||
) | ||
endif() | ||
|
||
# Configure to run all the tests? | ||
if(${EXERCISM_RUN_ALL_TESTS}) | ||
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS) | ||
endif() | ||
|
||
# Tell MSVC not to warn us about unchecked iterators in debug builds | ||
if(${MSVC}) | ||
set_target_properties(${exercise} PROPERTIES | ||
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS) | ||
endif() | ||
|
||
# Run the tests on every build | ||
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "list_ops.h" | ||
|
||
namespace list_ops { | ||
|
||
} // namespace list_ops |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
namespace list_ops { | ||
|
||
} // namespace list_ops |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it is stated in the instructions, we should not reiterate that in an additional amendment, right?
For the example I even skipped .size() for the length. But as a mentor, I think I would let it pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whatever you think makes the most sense 🤷 It's fine to let it pass as a mentor, but I would argue that just shelling out to existing functions does not really teach the student much.