In this post I’m going to show the section of higher order functions and list comprehensions of the 46 exercises.
To solve all six, I had to learn about the functions reduce(), map(), filter() and about lambda, as well as list comprehensions. I read about all this in the following pages:
In number 26 I wrote a function that, using reduce(), finds the maximum number in a list:
Number 27 is a function that takes a list of words and returns a list of their lengths. I had to do it in three different ways: with a for-loop, with map() and with list comprehensions.
The first one I had done it in exercise 14. Here are the other two:
Notice how in the last one, the list included an integer, and the program just skipped it. The function isinstance() checks that the element is a string, and if it is, then the length is applied.
Number 28 is a function that takes a list of words and returns the length of the longest one:
Number 29 is a function that takes a list of words and an integer, and returns a list of the words that are longer than the given number, using filter():
Number 30 is a function that takes a dictionary and a list of words, and returns the translation of those words, using map():
And number 31 consisted of implementing the three functions:
And with that, I finished the second section!