The Pathfinder Graph Algorithm in Python

Knowing the basics is what sets apart the great from the intermediate coders. In other words, a simple and effective way to grow your skills is to learn the basics of computer science. Python Pathfinder Algorithm In this tutorial, you’ll learn about the pathfinder algorithm that recursively determines whether there’s a direct or indirect path … Read more

The Quicksort Algorithm in Python

For a coder, understanding the Quicksort algorithm is like knowing the secret of 42—either you get it, or you don’t belong to the club. If you don’t know either, let’s work on the first one! The Quicksort Algorithm — A Python Implementation The algorithm is a variant of the popular Quicksort algorithm. The function qsort … Read more

Python Multi-Line Strings

Challenge: How to create a multi-line string in Python? In this tutorial, I’ll show you four methods to create and maintain multi-line strings in Python. The most Pythonic ones are the first two methods with triple single quotes ”’ … ”’ or triple double quotes “”” … “”” that wrap a string across multiple lines. … Read more

Lambda Calculus in Python

This tutorial 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 lambda functions in 1936 even before the appearance of the first computers. Lambda functions exist in a wide range of languages for functional … Read more

Python — How to Modify a Sequence While Iterating over It?

Modifying a sequence while iterating over it can cause undesired behavior due to the way the iterator is build. To avoid this problem, a simple solution is to iterate over a copy of the list. For example, you’ll obtain a copy of list_1 by using the slice notation with default values list_1[:]. Because you iterate … Read more

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 … 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 … 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 … Read more

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 … Read more