# Daily Python Puzzle

## How to Boost your Logic Skills (Series)? The “in” and “is” Keywords.

What is the output of this puzzle?   meal_1 = "Meat" meal_2 = "Flaxseeds" meal_3 = "Marshmallows" healthyFoods = ["Kale", "Apple", "Strawberry", "Banana", "Flaxseeds"] def isHealthy(food): return food in healthyFoods m_1 = isHealthy(meal_1) m_2 = isHealthy(meal_2) m_3 = isHealthy(meal_3) print((not m_1 or m_2) and (meal_2 is "Flaxseeds") and isHealthy("Kale"))   This puzzle shows two new …

## Boost your Logic Skills Series

What is the output of this puzzle?   # Logic Statements A = True # Temperatures are high B = True # Arctic ice is melting C = True # Sea levels are rising D = False # All regions become hot def follows(A,B): """ Returns True if 'A -> B' in words: 'B follows … ## Mastering the Python Map Function [+Video]

With Python’s map() function, you can apply a specific function to each element of an iterable. It takes two arguments: Function: Often it’s a lambda function that you can define on the fly. This is the function which you are going to apply on each element of an… Iterable: This is the iterable which you …

## The Any Function or How to Analyze Tweets in Python

What is the output of this puzzle?   wordlist = ["bitcoin", "cryptocurrency", "wallet"] crawled_text = ''' Research produced by the University of Cambridge estimates that in 2017, there are 2.9 to 5.8 million unique users using a cryptocurrency wallet, most of them using bitcoin. ''' s = crawled_text.split() res1 = True in map(lambda x: x …

## 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 = …

## How to Sort in Python with the Lambda Function as Key Argument?

This article shows how to sort in Python using the key argument that takes a function as argument. You can use the lambda function to create the function on the fly as shown in the following example: Actually, this is one of the most popular Python puzzles on the Finxter app. Before I explain the …

## The Pathfinder Graph Algorithm in Python

What is the output of this puzzle?   n = 5 # The number of vertices in the graph # The graph represented as adjancy matrix # See https://en.wikipedia.org/wiki/Adjacency_matrix G = [[1,1,0,0,0], [0,1,0,0,0], [0,0,1,0,0], [0,1,1,1,0], [1,0,0,1,1] ] # Is there a path from vertex i to vertex j? def findPath(i,j,pathLength): # The maximal length of …

## The Infinite While Loop in Python

WhAt is the output of this puzzle?   print("Answer") while True: pass print("42")   The question in this puzzle is whether the second print statement will ever be executed. The body of the while loop consists of the pass statement. This statement tells the interpreter to do nothing. Although the while loop does nothing, the …

## Dictionaries and Unpacking Arguments in Python

Which function correctly sorts the list?   def f(val1=3, val2=4, val3=6): return val1 + val2 + val3 values = {"val1": 9, "val3":-1} print(f(**values))   Programming is about using lower-level functionality to create higher-level functionality. In general, any programming language is a collection of functions that in turn build upon functions provided by the operating system. …

## The Quicksort Algorithm in Python

Which function correctly sorts the list?   def qsort1(L): if L == []: return [] return qsort1([x for x in L[1:] if x< L]) + L[0:1] \ + qsort1([x for x in L[1:] if x>=L]) def qsort2(L): if L == []: return [] return L[0:1] + qsort2([x for x in L[1:] if x< L]) \ …

## The Fibonacci Series in Python

What is the output of this code snippet?   def fibo(n): result = [] a, b = 0, 1 while a < n: result.append(a) a, b = b, a+b return result fib100 = fibo(100) print(fib100[-1]== fib100[-2]+fib100[-3])   Recap, the Fibonacci series is the series of numbers that arises when repeatedly summing up the last two …

## Multi-line Strings in Python

What is the output of this code snippet?   print(""" A B C """=="\nA\nB\nC\n")   What is going on in this puzzle? The basic idea is to show two different ways of writing the same multi-line string literal in Python. The first is the direct way to write a multi-line string in Python: As a …