Skip to content

Algorithm Approaches

Quadstronaut edited this page Jun 11, 2026 · 2 revisions

Algorithm Approaches

PunyPython's algorithm folders each tackle one problem with multiple implementations. The goal is not to recommend one answer — it is to show the spectrum of Python idioms so you can pick the right tool for your context.

Pattern: read them side by side

Open two or three files from the same folder in your editor. Compare the implementation, then ask:

  • Which is most readable at a glance?
  • Which scales better to large inputs?
  • Which uses Python's standard library most idiomatically?
  • Which reveals the most about how the underlying algorithm works?

Arrays

Finding the largest element (Arrays/largest_element/)

File Technique Notes
largest_element_native.py Manual loop comparison Shows the base algorithm explicitly
largest_element_max_func.py max(array) Idiomatic Python — prefer this in production
largest_element_lambda.py max(array, key=lambda x: x) Demonstrates the key= parameter; the identity lambda adds nothing here, but the pattern is useful when transforming before comparing
largest_element_operator_module.py Manual loop comparison using operator.gt() Uses operator.gt(i, max) in a loop — no max() built-in, no attrgetter
largest_element_reduce_func.py functools.reduce Educational — shows how max could be built from a fold
largest_element_sort_func.py sorted(array)[-1] Correct but O(n log n) vs O(n) — a useful trade-off discussion

Rotating an array left (Arrays/rotate/)

File Technique Complexity
rotate_one_by_one.py Shift one position d times O(n × d) time
rotate_array_w_temp_array.py Copy to a temp array O(n) time, O(n) space
rotate_array_list_slicing.py arr[d:] + arr[:d] O(n) — most readable Python
rotate_array_native.py Reverse three segments O(n) time, O(1) space — the in-place classic
rotate_array_4_juggling_algorithm.py Juggling / cycle-leader O(n) time, O(1) space — uses gcd to minimize moves

Summing an array (Arrays/sum_of_array/)

File Technique Notes
sum_of_array.py Manual loop Base case
sum_of_array_sum_func.py sum(arr) Idiomatic
sum_of_array_enumerate_func.py enumerate() loop Shows enumerate — overkill for a plain sum, but the pattern transfers
sum_of_array_reduce_module.py functools.reduce Shows how sum could be built from a fold
sum_of_array_divide_n_conquer.py Recursive divide and conquer O(n) but O(log n) call stack — illustrates the pattern for problems where D&C genuinely helps

Lists

Swap first and last elements (Lists/swap_first_last/)

File Technique Notes
list_swap_2_w_temp_var.py Temp variable Mutates in place — explicit, works in any language
list_swap_2_w_tuples.py Tuple swap: a, b = b, a Pythonic — the idiomatic in-place swap
list_swap_2_w_slicing.py lst[-1:] + lst[1:-1] + lst[:1] Returns a new list — cleaner when immutability is preferred
list_swap_2_w_star_operand.py end, *middle, start = lst then [end, *middle, start] Elegant star unpacking — returns a new list

Mathmatical

Adding two numbers (Mathmatical/add_2_numbers/)

Six approaches from inline expression to recursive addition — primarily useful for contrasting function structure patterns (add_2_numbers_w_func.py) against lambda (add_2_numbers_w_lambda_func.py), against the operator module (add_2_numbers_w_operator.py), and against user input (add_2_numbers_w_input.py).

Maximum of two numbers (Mathmatical/maximum_of_2_numbers/)

Six approaches covering: if/else inside a named function, max() builtin, ternary, lambda, sort-then-index, and list comprehension — useful for comparing code golf against clarity.

Factorial (Mathmatical/factorial/)

File Technique Dependency
factorial_recursive.py Classic recursion stdlib only
factorial_math_module.py math.factorial(n) stdlib only — prefer in production
factorial_prime_factorization.py Build factorial by multiplying prime-factored components of each integer 2..n stdlib only — educational; O(n √n)
factorial_numpy_module.py numpy.prod([1..n]) Requires pip install numpy

Armstrong number check (Mathmatical/Armstrong_number_check.py)

An Armstrong (narcissistic) number equals the sum of its digits each raised to the power of the number of digits (e.g. 153 = 1³ + 5³ + 3³). The file implements this with a custom power() using recursive squaring.

Textual

Hello world (Textual/hello_world.py)

print("Hello World") — one line.

ASCII value of character (Textual/ascii_value_of_char.py)

Reads a string from stdin and prints the ord() value of every character, one per line. Demonstrates input(), len(), and iterating over a string.

Remove nth character (Textual/remove_nth_char.py)

Currently removes all occurrences of 'l' from the string "hello world" via str.replace("l", "") — no index parameter, no slicing. The filename implies nth-char removal but the implementation does not match (known mismatch). Do not expect IndexError or positional behaviour; the script is effectively a character-filter demo.