# Daily Python Puzzle

## The Binary Search Algorithm in Python

Challenge: How to find a given value in a sorted list? Example: Say, you have a sorted list: Your goal is to find the index of the element 103 in the list. Do you have to check all elements to do this? Well, only if you used the … Naive List Search Algorithm A naive …

## Python Join Arguments and String Concatenation

Problem: Write a function that joins an arbitrary number of string arguments with a given separator. Example: Given the string arguments “A”, “B”, and “C” and the string separator “-“. Join them to the concatenated string “A-B-C”. Solution: The following code creates a Python function concat() that takes an arbitrary number of arguments, packs them …

## The Bubble Sort Algorithm in Python

The bubble sort does exactly what you’d expect it to do. It sorts an input list by treating each element as a bubble that climbs up the list. Each bubble rises as long as it is greater than the list elements. If a list element is smaller or equal to a list element, the bubble …

## The Maximum Profit Algorithm in Python

This article presents an algorithmic problem with practical value for stock market analysis. For instance, suppose you are trading the cryptocurrency Ethereum. How much profit in dollars can you make by buying low and selling high based on historical data? Maximum Profit Basic Algorithm The max profit algorithm calculates the maximum profit you’d obtain by …

## The Matrix Find Algorithm in Python

Challenge: How to find an element in a sorted matrix where row and column values increase monotonically? What is a matrix? A matrix is a table of values consisting of rows and columns. Here, we represent the matrix as a list of integer lists. Hence, we can access matrix values with the indexing and slicing …

## The Fibonacci Series in 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 such as economics, mathematics, art, and nature. Algorithm Sketch In the following, we give a simple algorithm to calculate the Fibonacci numbers. The series starts with the Fibonacci numbers zero …

## Python Default Arguments

This tutorial introduces the concept of default arguments in Python. A default argument is a function argument that takes on a default value if you don’t pass an explicit value for when calling the function. For example, the function definition def f(x=0): <body> allows you to call it with or without the optional argument x—valid …

## String Slicing in Python

String slicing is a concept to carve out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values (start=0, stop=len(string), step=1). For example, the …

## Arbitrary Argument Lists in Python

An arbitrary argument list is a Python feature to call a function with an arbitrary number of arguments. It’s based on the asterisk “unpacking” operator *. To catch an arbitrary number of function arguments in a tuple args, use the asterisk syntax *args within your function definition. For example, the function def f(*args): … allows …

## How to Assign a Function to a Variable in Python?

Challenge: Given is function f. How to assign the function to variable g, so that you can call g() and it runs function f()? Your desired output is function f‘s output: How to accomplish this in the most Pythonic way? Overview: We examine two methods to accomplish this challenge. You can run them in our …

## Negative Indexing in Python

Negative Indexing for Strings You can index single characters in strings using the bracket notation. The first character has index 0, the second index 1, and so on. Did you ever want to access the last element in the string? Counting the indices can be a real pain for long strings with more than 8-10 …

## Recursive Power Function: Are You Stuck With This Sololearn Code?

If you learn Python with the excellent Sololearn app, you may find yourself with this code snippet: What’s the output of this code snippet? And, most importantly, how does it work? This short guide will tell you! The code creates a function that returns x^y. It leverages the important programming concept of recursion: it calls …