# Daily Python Puzzle

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

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

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

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

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

## The Bubble Sort Algorithm in Python

What is the output of this code snippet?   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))   The bubble sort does exactly what you’d expect …

## The Maximum Profit Algorithm in Python

What is the output of this code snippet?   # Profit of a single # buying low and selling high def maximumProfit(A): m = 0; for i in range(0, len(A)): for j in range (i + 1, len(A)): m = max(m, A[j] – A[i]) return m # Ethereum daily prices in Dec 2017 (\$) prices …

## The Matrix Find Algorithm in Python

What is the output of this code snippet?   def matrixFind(matrix, value): m = len(matrix) if m == 0: return False n = len(matrix) if n == 0: return False i = 0 j = n – 1 while i < m and j >= 0: if matrix[i][j] == value: return True elif matrix[i][j] < …

## The Range Function and Indexing in Python

What is the output of this code snippet?   print(range(5, 10)[-1]) print(range(0, 10, 3)) print(range(-10, -100, -30))   If this book can teach you only one thing, it is a thorough understanding of the most important Python concepts such as indexing and slicing. I can not emphasize enough how important these concepts are for your …

## The Modulo Operator in Python

What is the output of this code snippet?   for num in range(2, 8): if num % 2 == 0: continue print(num)   This puzzle prints all odd values between two (included) and eight (excluded). To achieve this, we check whether the current value num can be divided by 2 without remainder. Python, like other …

## The Fibonacci Series in Python

What is the output of this code snippet?   # Fibonacci series: a, b = 0, 1 while b < 5: print(b) a, b = b, a+b   The Fibonacci series was discovered by the Italian mathematician Leonardo Fibonacci in 1202 and even earlier by Indian mathematicians. The series appears in unexpected areas such as …

## Slice List Assignments in Python

What is the output of this code snippet?   letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] letters[1:] = [] print(letters)   In verbose programming languages such as Java, you have to iterate over a list to remove subsequent elements. In Python, a simple one-liner does that for you. Use the slice notation to …