How to Sort in Python with the Lambda Function

What is the output of this puzzle?   pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] # lexicographical sorting (ascending) pairs.sort(key=lambda pair: pair[1]) print(pairs[0][1])   The high Elo indicates that only experienced Python coders can solve this puzzle. There are two barriers to overcome. First, the lambda function seems to be an abstract concept. Yet, it is only …

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 a path without a cycle …

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 interpreter is trapped forever because …

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. You must master the art …

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[0]]) + L[0:1] \ + qsort1([x for x in L[1:] if x>=L[0]]) def qsort2(L): if L == []: return [] return L[0:1] + qsort2([x for x in L[1:] if x< L[0]]) \ + qsort2([x for x in …

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 numbers starting from 0 and …

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 string with multiple code lines …

Lambda Functions in Python

What is the output of this code snippet?   def make_incrementor(n): return lambda x: x + n f = make_incrementor(42) print(f(0)) print(f(1))   This puzzle introduces an advanced language feature: lambda functions. Lambda functions are rooted in the mathematical area of lambda calculus. One of the pioneers of this area was Alonzo Church. He introduced lambda functions in 1936 even …

How to Modify a Sequence While Iterating over It (in Python)?

What is the output of this code snippet?   words = ['cat', 'mouse', 'dog'] for w in words[:]: if len(w) > 3: words.insert(0, w) print(words[0])   How to modify a sequence while iterating over it? For example, you want to prepare a data set of house prices for a machine learning algorithm to predict the market prices of new houses. …

Binary Search Algorithm in Python

What is the output of this code snippet?   def bsearch(l, value): lo, hi = 0, len(l)-1 while lo <= hi: mid = (lo + hi) // 2 if l[mid] < value: lo = mid + 1 elif value < l[mid]: hi = mid – 1 else: return mid return -1 l = [0, 1, 2, 3, 4, 5, 6] …

Python Float Arithmetic

What is the output of this code snippet?   x = 5 * 3.8 – 1 print(x)   This puzzle has only one challenge. But this challenge is so hard that only 7% of all Finxters can overcome it: floating point operators. I guess that most Finxters think that we are only looking for the result of the computation here. …

Arbitrary Argument Lists, String Concatenation, and the Join Function in Python

What is the output of this code snippet?   def concat(*args, sep="/"): return sep.join(args) print(concat("A", "B", "C", sep=","))   String concatenation is the process of creating a string by appending string arguments. The given function takes an arbitrary number of string arguments as specified by the *args keyword. The parameter sep declares the separator string to clue together two strings. …