## The Maximum Profit Algorithm in Python

What is the output of this code snippet?   [python] # 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 (\$) …

## The Matrix Find Algorithm in Python

What is the output of this code snippet? The challenge is shifting from understanding syntactical to semantical code snippets and algorithms. If you thoroughly master these types of code puzzles, you will join the club of advanced coders. Thus, you open up the opportunity to work in one of the highest paid job industry in …

## The Range Function and Indexing in Python

What is the output of this code snippet?   [python] print(range(5, 10)[-1]) print(range(0, 10, 3)) print(range(-10, -100, -30)) [/python]   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 …

## The Modulo Operator in Python

What is the output of this code snippet?   [python] for num in range(2, 8): if num % 2 == 0: continue print(num) [/python]   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, …

## The Fibonacci Series in Python

What is the output of this code snippet?   [python] # Fibonacci series: a, b = 0, 1 while b < 5: print(b) a, b = b, a+b [/python]   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 …

## Slice List Assignments in Python

What is the output of this code snippet?   [python] letters = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’] letters[1:] = [] print(letters) [/python]   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 …

## Nested Lists in Python

What is the output of this code snippet?   [python] a = [‘a’, ‘b’] n = [1, 2] x = [a, n] print(x) [/python]   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 …

## Slicing Lists Basics in Python

What is the output of this code snippet?   [python] letters = [‘a’, ‘b’, ‘c’, ‘d’] print(len(letters[1:-1])) [/python]   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 …

## Replacing Default Parameters in Python

Is ask_ok(‘Howdy?’, 5) a valid function call?   [python] 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) [/python]   This puzzle introduces the …

## Slice Assignment in Python

What is the output of this code snippet?   [python] customers = [‘Marie’, ‘Anne’, ‘Donald’] customers[2:4] = [‘Barack’, ‘Olivia’, ‘Sophia’] print(customers[2:-1]) [/python]   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 …

## String Slicing in Python

What is the output of this code snippet?   [python] word = "bender" print(word[1:4]) [/python] 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 …

## Arbitrary Argument Lists in Python

What is the output of this code snippet?   [python] def f(a, *arguments): print(a) for arg in arguments: print(arg) f("A", "B", "C") [/python] This puzzle introduces a nice Python feature: arbitrary argument lists. Suppose, you want to create a function that allows an arbitrary number of arguments. An example is recognizing faces in images where …