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 it to do. It sorts …

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 = [455,460,465,451,414,415,441] print(maximumProfit(prices))   This …

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[0]) 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] < value: i = i + …

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)[2]) print(range(-10, -100, -30)[1])   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 practical work. The goal of …

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 languages, uses the percentage symbol …

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 economics, mathematics, art, and nature. …

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 select a sequence of items …

Nested Lists in Python

What is the output of this code snippet?   a = ['a', 'b'] n = [1, 2] x = [a, n] print(x[1])   Many practical code projects use lists containing not only primitive but also complex data types. Examples of primitive data types are integers, strings, or floats. Examples of complex data types are customer objects, camera events, or even …

Slicing Lists Basics in Python

What is the output of this code snippet?   letters = ['a', 'b', 'c', 'd'] print(len(letters[1:-1]))   The goal of this puzzle is to deepen your understanding of the important concept of slicing. Yet, it turned out to be more a test of thoroughness than anything else. The majority of users can not solve this puzzle—one of the most common …

Replacing Default Parameters in Python

Is ask_ok(‘Howdy?’, 5) a valid function call?   def ask_ok(prompt, retries=4, reminder='Repeat!'): while True: ok = input(prompt) if ok in ('y', 'ye', 'yes'): return True if ok in ('n', 'no', 'nop', 'nope'): return False retries = retries – 1 if retries < 0: raise ValueError('invalid user response') print(reminder)   This puzzle introduces the concept of default arguments in Python. Suppose, …

Slice Assignment in Python

What is the output of this code snippet?   customers = ['Marie', 'Anne', 'Donald'] customers[2:4] = ['Barack', 'Olivia', 'Sophia'] print(customers[2:-1])   A great coder seeks the cleanest and shortest way to accomplish his goals. This puzzle demonstrates a Python trick that I found very useful: slice assignments. Suppose you work in a biotech startup on DNA sequence modeling. You maintain …

String Slicing in Python

What is the output of this code snippet?   word = "bender" print(word[1:4]) The language feature slicing does not only apply to lists, but also to strings. As both lists and strings are sequencing types, slicing is only one among several similarities. For example, you can also iterate over the characters in a string using the for loop (e.g. for …