Reference library

Algorithms & data structures

Classic patterns — search, sort, stacks, queues, and practical complexity-aware code.

5 matches
Algorithms & data structures easy

Generate Pascal's Triangle Rows in Python

Builds Pascal's triangle as a list of rows, where each inner value is the sum of the two values above it.

pascal-triangle dynamic-programming algorithms
Python
def generate_pascals_triangle(rows):
    triangle = []
    for row_num in range(rows):
        row = [1] * (row_num + 1)
        for col in range(1, row_num):
            row[col] = triangle[row_num - 1][col - 1] + triangle[row_num - 1][col]
        triangle.append(row)
    return triangle

if __name__ == "__main__":
…
14 0 Open
Algorithms & data structures easy

How to Build a Coordinate Grid with Nested Loops in Python

Generate a 2D list of (row, col) coordinate pairs using nested loops and return the grid structure.

coordinate grid nested loops 2d list
Python
def build_coordinate_grid(rows, cols):
    """Build a 2D grid of (row, col) coordinates using nested loops."""
    grid = []
    for r in range(rows):
        row = []
        for c in range(cols):
            row.append((r, c))
        grid.append(row)
    return grid


if __name__ == "__main__":
    grid = build_coo…
15 0 Open
Algorithms & data structures easy

How to Generate a Geometric Progression List in Python

This Python function builds a list of n terms in a geometric progression, starting with a given first term and multiplying by a constant ratio at each step.

geometric-progression sequence algorithms
Python
def geometric_progression(first_term, ratio, count):
    """
    Generate a list of 'count' terms in a geometric progression
    starting with 'first_term' and multiplied by 'ratio' each step.
    """
    progression = []
    current = first_term
    for _ in range(count):
        progression.append(current)
        c…
13 0 Open
Algorithms & data structures easy

How to Map Strings to Uppercase in Python

Loops through a list of strings and builds a new list with each string converted to uppercase.

string loop uppercase
Python
strings = ["hello", "world", "python", "skillset"]

uppercased = []
for s in strings:
    uppercased.append(s.upper())

print(uppercased)
15 0 Open
Algorithms & data structures medium

Implement Insert Delete GetRandom O(1) in Python

Build a RandomizedSet class that supports insert, delete, and get_random in average O(1) time using a list and a dictionary mapping values to indices.

randomized-set o1-lookup hash-map
Python
import random

class RandomizedSet:
    def __init__(self):
        self.values = []
        self.index_map = {}

    def insert(self, val):
        if val in self.index_map:
            return False
        self.index_map[val] = len(self.values)
        self.values.append(val)
        return True

    def delete(self…
12 0 Open

Browse by section

Each section groups closely related Python snippets.

Algorithms & data structures — Python code examples

What you will find here

This page collects algorithms & data structures snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.