Daily Python Puzzle

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 …

Python Join Arguments and String Concatenation Read More »

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 Maximum Profit Algorithm in Python Read More »

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 Matrix Find Algorithm in Python Read More »

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 …

The Fibonacci Series in Python Read More »

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 …

String Slicing in Python Read More »

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 …

Arbitrary Argument Lists in Python Read More »

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 …

How to Assign a Function to a Variable in Python? Read More »

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 …

Negative Indexing in Python Read More »

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 …

Recursive Power Function: Are You Stuck With This Sololearn Code? Read More »