Set Operations, Lambda Functions, and Who Dominates Social Networks

What is the output of this puzzle?

# popular instagram accounts
# (millions followers)
inst = {“@instagram”: 232,
“@selenagomez”: 133,
“@victoriassecret”: 59,
“@cristiano”: 120,
“@beyonce”: 111,
“@nike”: 76}

# popular twitter accounts
# (millions followers)
twit = {“@cristiano”: 69,
“@barackobama”: 100,
“@ladygaga”: 77,
“@selenagomez”: 56,
“@realdonaldtrump”: 48}

inst_names = set(filter(lambda x: inst[x]>60, inst.keys()))
twit_names = set(filter(lambda x: twit[x]>60, twit.keys()))

superstars = inst_names.intersection(twit_names)

You will use or have already used the concepts introduced in this puzzle. They are elementary pieces of knowledge for any Python programmer. There are three basic concepts in the puzzle.

First, we have the two dictionaries mapping an account name to the number of followers. For example, Cristiano Ronaldo (key: “@cristiano”) has 120 million Instagram followers. In contrast to lists, dictionaries allow fast data access. You can retrieve each item with only one operation without having to iterate over the whole data structure. As the coder says, a dictionary access has constant runtime complexity.

Second, the filter function returns a new sequence of which each item matches a defined characteristic. The filter function takes two arguments. The first argument is a function that returns a boolean value True or False. True, if a sequence element should be included, and False otherwise. The second argument is the sequence to be filtered.

Third, intersecting two sets s1 and s2 returns a new set that contains elements that are in both sets s1 and s2. If you need more input on all the 13 most important Python set operations, study this article with video tutorial. It’ll be worth every second of your time because you’ll become a better coder in the process.

The only star that has more than 60 million Instagram AND twitter followers is Cristiano Ronaldo.

Are you a master coder?
Test your skills now!

Related Video



Leave a Comment

Your email address will not be published. Required fields are marked *