# Month: April 2018

## The Pathfinder Graph Algorithm in Python

What is the output of this puzzle?   [python] 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 …

## The Infinite While Loop in Python

WhAt is the output of this puzzle?   [python] print("Answer") while True: pass print("42") [/python]   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 …

## Dictionaries and Unpacking Arguments in Python

Which function correctly sorts the list?   [python] def f(val1=3, val2=4, val3=6): return val1 + val2 + val3 values = {"val1": 9, "val3":-1} print(f(**values)) [/python]   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 …

## The Quicksort Algorithm in Python

Which function correctly sorts the list?   [python] 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?   [python] 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]) [/python]   Recap, the Fibonacci series is the series of numbers that arises when repeatedly summing up the …

## Multi-line Strings in Python

What is the output of this code snippet?   [python] print(""" A B C """=="\nA\nB\nC\n") [/python]   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: …

## Lambda Functions in Python

What is the output of this code snippet?   [python] def make_incrementor(n): return lambda x: x + n f = make_incrementor(42) print(f(0)) print(f(1)) [/python]   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. …

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

What is the output of this code snippet?   [python] words = [‘cat’, ‘mouse’, ‘dog’] for w in words[:]: if len(w) > 3: words.insert(0, w) print(words) [/python]   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 …

## Binary Search Algorithm in Python

What is the output of this code snippet?   [python] 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, …

## Python Float Arithmetic

What is the output of this code snippet?   [python] x = 5 * 3.8 – 1 print(x) [/python]   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 …

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

What is the output of this code snippet?   [python] def concat(*args, sep="/"): return sep.join(args) print(concat("A", "B", "C", sep=",")) [/python]   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 …

## The Bubble Sort Algorithm in Python

What is the output of this code snippet?   [python] def bubblesort(lst): for passesLeft in range(len(lst)-1, 0, -1): for index in range(passesLeft): if lst[index] > lst[index + 1]: lst[index], lst[index + 1] = lst[index + 1], lst[index] return lst l=[27, 0, 71, 70, 27, 63, 90] print(bubblesort(l)) [/python]   The bubble sort does exactly what …