Map
Last updated
Last updated
"Map", "filter", and "reduce" are three commonly used in functional programming.
In Python, the built-in function takes a function and an (in this case a list) as inputs. It returns an iterator that applies the function to every item, yielding the results.
With map
, we can operate on lists without using loops and nasty stateful variables. For example:
Complete the change_bullet_style
function. It takes a document (a string) as input, and returns a single string as output. The returned string should have any lines that start with a -
character replaced with a *
character.
For example, this:
Becomes:
Examples of split and join:
The , list()
converts the map
object back into a standard list.
supports two different styles of bullet points, -
and *
. We prefer *
, so, we need a function to convert any -
bullet points to *
bullet points.
Use the built-in function to apply the provided convert_line
function to each line of the input string. Use and to split the document into a list of lines, and then join the lines back together. This should preserve the original line breaks. Don't use the .replace()
string method.