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 whichpredicate(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 applyingfunction(item)
on all items);foldl
(given a function, a list, and initial accumulator, fold (reduce) each item into the accumulator from the left usingfunction(accumulator, item)
);foldr
(given a function, a list, and an initial accumulator, fold (reduce) each item into the accumulator from the right usingfunction(item, accumulator)
);reverse
(given a list, return a list with all the original items, but in reversed order);
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
raise Exception
, you should write:
raise Exception("Meaningful message indicating the source of the error")
To run the tests, run pytest list_ops_test.py
Alternatively, you can tell Python to run the pytest module:
python -m pytest list_ops_test.py
-v
: enable verbose output-x
: stop running tests on first failure--ff
: run failures from previous test before running other test cases
For other options, see python -m pytest -h
Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/list-ops
directory.
You can find your Exercism workspace by running exercism debug
and looking for the line that starts with Workspace
.
For more detailed information about running tests, code style and linting, please see Running the Tests.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.