46 exercises: 26-31

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:

2626run

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:

2727run

27.227.2run

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:

2828run

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():

2929run

Number 30 is a function that takes a dictionary and a list of words, and returns the translation of those words, using map():

3030run

And number 31 consisted of implementing the three functions:

3131run

And with that, I finished the second section!

One thought on “46 exercises: 26-31

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s