Menu Close

Algorithm Design with Python

algorithm design python

Introduction

There are several functions, objects, patterns and data structures to use when designing algorithms. I’ve created a list of “tools” to get reminded/inspired by when you want to design an algorithm. These are in random order.

Overview

Tool / ModulePurpose in Algorithm DesignOfficial Docs
itertoolsHigh‑performance iterator building blocks: combinatorics, infinite iterators, filters, grouping, slicing.Official documentation page
functoolsHigher‑order functions, memoization (lru_cache), partial application, comparison helpers.Official documentation page
collectionsSpecialized containers: deque (fast queues), Counter, defaultdict, namedtuple, ChainMap.Official documentation page
heapqHeap‑based priority queue algorithms (min‑heap).Official documentation page
bisectBinary search and sorted list insertion.Official documentation page
mathMathematical functions, constants, factorial, gcd, trigonometry.Official documentation page
statisticsDescriptive statistics: mean, median, stdev, variance.Official documentation page
randomRandom number generation, sampling, shuffling.Official documentation page
graphlibTopological sorting for DAGs.Official documentation page
arraySpace‑efficient typed arrays for numeric data.Official documentation page
queueThread‑safe FIFO, LIFO, and priority queues.Official documentation page
time / timeitTiming, benchmarking, performance measurement.Official documentation page
Official documentation page
operatorFunctional interface to Python’s operators (for sorting keys, mapping, etc.).Official documentation page
reRegular expressions for pattern matching and text parsing.Official documentation page
Built‑in FunctionsCore tools like sorted, enumerate, zip, map, filter, any, all, sum, min, max.Official documentation page
Built‑in Data Structureslist, tuple, dict, set, frozenset, range, str — each with algorithm‑friendly methods.Official documentation page
Official documentation page

Core

FunctionDescription
len()Get the number of elements in a sequence or collection.
range()Generate sequences of integers, for loops, or iteration patterns.
enumerate()Loop with both index and value.
zip()Pair up elements from multiple iterables. Great for parallel iteration.
sorted()Return a sorted list from any iterable. Can take key functions for custom order.
min() / max()Quickly find minimum/maximum, optionally with a key function.
sum()Efficiently sum numeric iterables.
map() / filter()Functional style transformations and filtering.
any() / all()Test aggregate truthiness (Short‑circuit checks).

(For a complete list of built-in functions for Python3).

Itertools

Official documentation

Infinite Iterators

Function / IteratorExample
count()count(10) → 10 11 12 13 14 ...
cycle()cycle('ABCD') → A B C D A B C D ...
repeat()repeat(10, 3) → 10 10 10

Iterators terminating on the shortest input sequence

Function / IteratorExample
accumulate()accumulate([1,2,3,4,5]) → 1 3 6 10 15
batched()batched('ABCDEFG', n=2) → AB CD EF G
chain()chain('ABC', 'DEF') → A B C D E F
chain.from_iterable()chain.from_iterable(['ABC', 'DEF']) → A B C D E F
compress()compress('ABCDEF', [1,0,1,0,1,1]) → A C E F
dropwhile()dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8
filterfalse()filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8
groupby()groupby(['A','B','DEF'], len) → (1, A B) (3, DEF)
islice()islice('ABCDEFG', 2, None) → C D E F G
pairwise()pairwise('ABCDEFG') → AB BC CD DE EF FG
starmap()starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000
takewhile()takewhile(lambda x: x<5, [1,4,6,3,8]) → 1 4
tee()
zip_longest()zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-

Combinatoric Iterators

Function / IteratorResults
product()cartesian product, equivalent to a nested for-loop
permutations()r-length tuples, all possible orderings, no repeated elements
combinations()r-length tuples, in sorted order, no repeated elements
combinations_with_replacement()r-length tuples, in sorted order, with repeated elements

Functools

Official Documentation

FunctionDescription
cache(user_function)Simple, lightweight, unbounded function cache (memoization). Equivalent to lru_cache(maxsize=None).
cached_property(func)Decorator that turns a method into a property computed once per instance and then cached.
cmp_to_key(func)Converts an old-style comparison function to a key function for sorting.
lru_cache(user_function)
lru_cache(maxsize=128, typed=False)
Decorator for least-recently-used caching of function results.
total_ordering(cls)Class decorator that fills in missing rich comparison methods based on one or more defined ones.
partial(func, /, *args, **keywords)Returns a new callable with some arguments pre-filled.
partialmethod(func, /, *args, **keywords)Like partial(), but designed for use with methods in classes.
reduce(function, iterable[, initial])Applies a binary function cumulatively to items of an iterable, reducing it to a single value.
singledispatch(func)Turns a function into a single-dispatch generic function, allowing type-based overloading on the first argument.
singledispatchmethod(func)Like singledispatch(), but for methods; dispatches on the first non-self/cls argument.
update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)Updates a wrapper function to look like the wrapped function (metadata, attributes).
wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)Convenience decorator for update_wrapper() when defining wrapper functions.

Collections

Official Documentation

FunctionDescription
namedtuple()factory function for creating tuple subclasses with named fields
dequelist-like container with fast appends and pops on either end
ChainMapdict-like class for creating a single view of multiple mappings
Counterdict subclass for counting hashable objects
OrderedDictdict subclass that remembers the order entries were added
defaultdictdict subclass that calls a factory function to supply missing values
UserDictwrapper around dictionary objects for easier dict subclassing
UserListwrapper around list objects for easier list subclassing
UserStringwrapper around string objects for easier string subclassing

Heapq

Official Documentation

FunctionDescription
heapq.heappush(heap, item)Pushes item onto heap while maintaining the heap invariant.
heapq.heappop(heap)Pops and returns the smallest item from heap, maintaining the heap invariant.
heapq.heappushpop(heap, item)Pushes item onto heap, then pops and returns the smallest item — faster than separate push and pop calls.
heapq.heapify(x)Transforms list x into a valid heap in-place, in linear time.
heapq.heapreplace(heap, item)Pops and returns the smallest item, then pushes item onto heap — heap size stays the same.
heapq.merge(*iterables, key=None, reverse=False)Lazily merges multiple sorted inputs into a single sorted output iterator.
heapq.nlargest(n, iterable, key=None)Returns a list of the n largest elements from iterable.
heapq.nsmallest(n, iterable, key=None)Returns a list of the n smallest elements from iterable.

Bisect

Official Documentation

FunctionDescription
bisect_left(a, x, lo=0, hi=len(a), *, key=None)Finds the insertion point for x in a to maintain sorted order, placing it before any existing equal elements.
bisect_right(a, x, lo=0, hi=len(a), *, key=None) / bisect(...)Like bisect_left(), but places the insertion point after any existing equal elements.
insort_left(a, x, lo=0, hi=len(a), *, key=None)Inserts x into a in sorted order, before any existing equal elements.
insort_right(a, x, lo=0, hi=len(a), *, key=None) / insort(...)Inserts x into a in sorted order, after any existing equal elements.

Math

Official Documentation

FunctionDescription
Number-theoretic functions
comb(n, k)Number of ways to choose k items from n items without repetition and without order
factorial(n)n factorial
gcd(*integers)Greatest common divisor of the integer arguments
isqrt(n)Integer square root of a nonnegative integer n
lcm(*integers)Least common multiple of the integer arguments
perm(n, k)Number of ways to choose k items from n items without repetition and with order
Floating point arithmetic
ceil(x)Ceiling of x, the smallest integer greater than or equal to x
fabs(x)Absolute value of x
floor(x)Floor of x, the largest integer less than or equal to x
fma(x, y, z)Fused multiply-add operation: (x * y) + z
fmod(x, y)Remainder of division x / y
modf(x)Fractional and integer parts of x
remainder(x, y)Remainder of x with respect to y
trunc(x)Integer part of x
Floating point manipulation functions
copysign(x, y)Magnitude (absolute value) of x with the sign of y
frexp(x)Mantissa and exponent of x
isclose(a, b, rel_tol, abs_tol)Check if the values a and b are close to each other
isfinite(x)Check if x is neither an infinity nor a NaN
isinf(x)Check if x is a positive or negative infinity
isnan(x)Check if x is a NaN (not a number)
ldexp(x, i)x * (2**i), inverse of function frexp()
nextafter(x, y, steps)Floating-point value steps steps after x towards y
ulp(x)Value of the least significant bit of x
Power, exponential and logarithmic functions
cbrt(x)Cube root of x
exp(x)e raised to the power x
exp2(x)2 raised to the power x
expm1(x)e raised to the power x, minus 1
log(x, base)Logarithm of x to the given base (e by default)
log1p(x)Natural logarithm of 1+x (base e)
log2(x)Base-2 logarithm of x
log10(x)Base-10 logarithm of x
pow(x, y)x raised to the power y
sqrt(x)Square root of x
Summation and product functions
dist(p, q)Euclidean distance between two points p and q given as an iterable of coordinates
fsum(iterable)Sum of values in the input iterable
hypot(*coordinates)Euclidean norm of an iterable of coordinates
prod(iterable, start)Product of elements in the input iterable with a start value
sumprod(p, q)Sum of products from two iterables p and q
Angular conversion
degrees(x)Convert angle x from radians to degrees
radians(x)Convert angle x from degrees to radians
Trigonometric functions
acos(x)Arc cosine of x
asin(x)Arc sine of x
atan(x)Arc tangent of x
atan2(y, x)atan(y / x)
cos(x)Cosine of x
sin(x)Sine of x
tan(x)Tangent of x
Hyperbolic functions
acosh(x)Inverse hyperbolic cosine of x
asinh(x)Inverse hyperbolic sine of x
atanh(x)Inverse hyperbolic tangent of x
cosh(x)Hyperbolic cosine of x
sinh(x)Hyperbolic sine of x
tanh(x)Hyperbolic tangent of x
Special functions
erf(x)Error function at x
erfc(x)Complementary error function at x
gamma(x)Gamma function at x
lgamma(x)Natural logarithm of the absolute value of the Gamma function at x
Constants
piπ = 3.141592…
ee = 2.718281…
tauτ = 2π = 6.283185…
infPositive infinity
nan“Not a number” (NaN)

Statistics

Official Documentation

Averages and measures of central location

FunctionDescription
mean()Arithmetic mean (“average”) of data.
fmean()Fast, floating-point arithmetic mean, with optional weighting.
geometric_mean()Geometric mean of data.
harmonic_mean()Harmonic mean of data.
kde()Estimate the probability density distribution of the data.
kde_random()Random sampling from the PDF generated by kde().
median()Median (middle value) of data.
median_low()Low median of data.
median_high()High median of data.
median_grouped()Median (50th percentile) of grouped data.
mode()Single mode (most common value) of discrete or nominal data.
multimode()List of modes (most common values) of discrete or nominal data.
quantiles()Divide data into intervals with equal probability.

Measures of spread

FunctionDescription
pstdev()Population standard deviation of data.
pvariance()Population variance of data.
stdev()Sample standard deviation of data.
variance()Sample variance of data.

Statistics for relations between two inputs

FunctionDescription
covariance()Sample covariance for two variables.
correlation()Pearson and Spearman’s correlation coefficients.
linear_regression()Slope and intercept for simple linear regression.

Strings

Official Documentation

FunctionDescription
len(s)Returns the length (number of characters) of string s.
str(obj)Converts an object to its string representation.
repr(obj)Returns a developer‑friendly string representation (often with quotes/escapes).
.lower()Returns a lowercase version of the string.
.upper()Returns an uppercase version of the string.
.casefold()Aggressive lowercasing for caseless matching.
.capitalize()Capitalizes the first character, lowercases the rest.
.title()Capitalizes the first letter of each word.
.swapcase()Swaps uppercase to lowercase and vice versa.
.istitle()Checks if string is in title case.
.islower()Checks if all cased characters are lowercase.
.isupper()Checks if all cased characters are uppercase.
.isalpha()Checks if all characters are alphabetic.
.isalnum()Checks if all characters are alphanumeric.
.isdecimal()Checks if all characters are decimal characters.
.isdigit()Checks if all characters are digits.
.isnumeric()Checks if all characters are numeric characters.
.isidentifier()Checks if string is a valid Python identifier.
.isprintable()Checks if all characters are printable or string is empty.
.isspace()Checks if all characters are whitespace.
.find(sub) / .rfind(sub)Returns lowest/highest index of substring, or ‑1 if not found.
.index(sub) / .rindex(sub)Like find, but raises ValueError if not found.
.count(sub)Counts non‑overlapping occurrences of substring.
.startswith(prefix)Checks if string starts with prefix.
.endswith(suffix)Checks if string ends with suffix.
.replace(old, new[, count])Returns a copy with old replaced by new.
.strip([chars]) / .lstrip() / .rstrip()Remove leading/trailing characters (default: whitespace).
.split([sep[, maxsplit]]) / .rsplit()Split into a list by separator.
.splitlines([keepends])Split at line breaks.
.join(iterable)Join elements with the string as a separator.
.format(*args, **kwargs)Format string with placeholders {}.
.format_map(mapping)Like .format() but uses mapping directly.
.expandtabs(tabsize=8)Replace tabs with spaces.
.zfill(width)Pad string on the left with zeros to given width.
.center(width[, fillchar])Center align with optional fill character.
.ljust(width[, fillchar]) / .rjust(width[, fillchar])Left/right align.
.encode(encoding="utf-8")Encode string to bytes.
.translate(table)Apply character mapping table.
.maketrans(x, y[, z])Create mapping table for .translate().
string.ascii_lettersConstant: lowercase + uppercase ASCII letters.
string.ascii_lowercaseConstant: lowercase ASCII letters.
string.ascii_uppercaseConstant: uppercase ASCII letters.
string.digitsConstant: '0123456789'.
string.hexdigitsConstant: Hexadecimal digits.
string.octdigitsConstant: Octal digits.
string.punctuationConstant: All ASCII punctuation chars.
string.printableConstant: Digits, letters, punctuation, whitespace.
string.whitespaceConstant: All ASCII whitespace chars.
string.capwords(s, sep=None)Capitalizes each word in s.
string.FormatterClass for customizable {} string formatting.
string.Template$placeholder string substitution class.
re.search(pattern, s)Regex search for first match in s.
re.findall(pattern, s)Return all matches of regex in s.
re.sub(pattern, repl, s)Replace regex matches with repl.
textwrap.fill(s, width)Wrap text to given width.
ord(ch) / chr(code)Unicode code point ⇄ character conversion

Random

Official Documentation

FunctionDescription
Bookkeeping
seed(a=None, version=2)Initialize the random number generator with a seed.
getstate()Return the current internal state of the generator.
setstate(state)Restore the generator’s state from a previous getstate() call.
Bytes
randbytes(n)Return n random bytes (not for cryptographic use).
Integers
randrange(start, stop[, step])Return a randomly selected element from the given range.
randint(a, b)Return a random integer N such that a <= N <= b.
getrandbits(k)Return an integer with k random bits.
Sequences
choice(seq)Return a random element from a non-empty sequence.
choices(population, weights=None, *, cum_weights=None, k=1)Return a list of k elements chosen with replacement, optionally weighted.
shuffle(x)Shuffle a sequence in place.
sample(population, k, *, counts=None)Return k unique elements chosen without replacement.
Discrete Distributions
binomialvariate(n=1, p=0.5)Return number of successes in n trials with success probability p.
Real-valued Distributions
random()Return a float in the range [0.0, 1.0).
uniform(a, b)Return a float N such that a <= N <= b (or reversed if b < a).
triangular(low, high, mode)Return a float between low and high with a given mode.
betavariate(alpha, beta)Beta distribution in range [0, 1].
expovariate(lambd=1.0)Exponential distribution with rate parameter lambd.
gammavariate(alpha, beta)Gamma distribution with shape alpha and scale beta.
gauss(mu=0.0, sigma=1.0)Gaussian distribution (faster but less thread-safe).
lognormvariate(mu, sigma)Log-normal distribution.
normalvariate(mu=0.0, sigma=1.0)Normal (Gaussian) distribution.
vonmisesvariate(mu, kappa)Von Mises distribution for angles.
paretovariate(alpha)Pareto distribution.
weibullvariate(alpha, beta)Weibull distribution.
Alternative Generators
Random([seed])Class implementing the default PRNG; can be subclassed.
SystemRandom()Class using os.urandom() for system-based randomness.

Data structures

Official Documentation

TypeMutable?Ordered?Allows Duplicates?Description
listGeneral‑purpose, ordered collection. For sequences, stacks, queues (with deque for speed).
tupleFixed‑size, ordered grouping. Good for records, dictionary keys, function returns.
dict(insertion order preserved)Keys: ❌Key‑value mapping. Fast lookups, flexible keys (immutable types).
setUnordered collection of unique elements. For membership tests, set algebra.
frozensetImmutable set, usable as a dictionary key or set element.
strText sequences. Technically a sequence of Unicode code points.
rangeMemory‑efficient integer sequence, often for looping.
bytes / bytearraybytes: ❌
bytearray: ✅
Sequences of raw bytes for binary data, file I/O, networking.

Patterns and Methods

PatternUsePython Tools
RecursionNatural for divide‑and‑conquer problems.Define function that calls itself; manage base cases carefully.
List comprehensionsConcise, often faster than loops.[f(x) for x in items if cond(x)]
Generator functionsLazily produce items; save memory.yield keyword; pair with itertools.
Set operationsFast membership & deduplication..union(), .intersection(), .difference().
String methodsEssential for parsing/text processing..split(), .join(), .strip().

When to choose what

NeedBest Choice
Fast random access + frequent mutationlist
Fixed, unchanging groupingtuple
Key–value lookupsdict
Unique elements onlyset / frozenset
FIFO or LIFO with speedcollections.deque
Count/frequency of elementscollections.Counter
Maintain sorted order efficientlysorted() for snapshots1, bisect for incremental inserts2
Work with binary databytes / bytearray / array.array
Immutable set‑like behaviorfrozenset
Map missing keys to default valuescollections.defaultdict
Preserve insertion order (pre‑3.7 guarantee)collections.OrderedDict
Combine multiple mappings into one viewcollections.ChainMap
Thread‑safe FIFO queuequeue.Queue
Thread‑safe stackqueue.LifoQueue
Thread‑safe priority queuequeue.PriorityQueue
Priority queue (non‑thread‑safe, lightweight)heapq (list‑based)
  1. Snapshots: return a new list ↩︎
  2. Adding new data while keeping it sorted ↩︎

Questions to ask yourself

  • What exactly is the problem statement? Can I restate it in my own words?
  • What are the inputs? Type, format, constraints, example values.
  • What should the output be? Format, type, exact requirements.
  • What are the constraints? Time limits, memory limits, input sizes, special cases.
  • Do I fully understand all terms and edge conditions? If not, what needs clarification?
  • What data structures could model the problem best? List, dict, set, heap, deque, tree, graph, custom class.
  • What category does this problem fall into? Sorting, searching, graph traversal, Dynamic Programming, greedy, simulation, math, string processing
  • What algorithmic strategies might fit? Brute force, divide‑and‑conquer, memoization, Breadth First Search, Depth First Serach, binary search, sliding window, two‑pointers.
  • How will I handle edge cases? Empty input, max/min values, duplicate data, invalid data.
  • Will it scale? Estimate complexity (Big O).

Then:

  • How would I solve this with pseudo-code? Map logic without syntax distractions.
  • Write solution. In any suitable language.
  • Test solution. Including edge cases.

Conclusion

This concludes the many lists of Python functions and tools to aid in designing algorithms. A list of good books about algorithms can be found here.

Related Posts