sample_id
stringlengths
21
196
text
stringlengths
105
936k
metadata
dict
category
stringclasses
6 values
keon/algorithms:algorithms/bit_manipulation/swap_pair.py
""" Swap Pair Swap odd and even bits of an integer using bitmask operations. Bit 0 is swapped with bit 1, bit 2 with bit 3, and so on. Reference: https://en.wikipedia.org/wiki/Bit_manipulation Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations def swap_pair(number: int) -> int: ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/bit_manipulation/swap_pair.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/common/graph.py
"""Graph type shared across all graph algorithms. This module provides the universal Graph used by every graph algorithm in this library. Using a single shared type means you can compose algorithms: build a graph, run BFS to check connectivity, then run Dijkstra for shortest paths — all on the same object. """ from _...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/common/graph.py", "license": "MIT License", "lines": 79, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/common/list_node.py
"""Singly linked list node shared across all linked list algorithms. This module provides the universal ListNode used by every linked list algorithm in this library. Using a single shared type means you can compose algorithms: merge two lists, reverse the result, check if it's a palindrome. """ from __future__ import...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/common/list_node.py", "license": "MIT License", "lines": 23, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/common/tree_node.py
"""Binary tree node shared across all tree algorithms. This module provides the universal TreeNode used by every tree algorithm in this library. Using a single shared type means you can compose algorithms freely: build a BST, invert it, then traverse it. """ from __future__ import annotations from dataclasses import...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/common/tree_node.py", "license": "MIT License", "lines": 24, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/b_tree.py
""" B-Tree A self-balancing tree data structure optimized for disk operations. Each node (except root) contains at least t-1 keys and at most 2t-1 keys, where t is the minimum degree. The tree grows upward from the root. Reference: https://en.wikipedia.org/wiki/B-tree Complexity: Time: O(log n) for search, inse...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/b_tree.py", "license": "MIT License", "lines": 303, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/fenwick_tree.py
""" Fenwick Tree / Binary Indexed Tree Consider we have an array arr[0 . . . n-1]. We would like to 1. Compute the sum of the first i elements. 2. Modify the value of a specified element of the array arr[i] = x where 0 <= i <= n-1. A simple solution is to run a loop from 0 to i-1 and calculate the sum of the eleme...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/fenwick_tree.py", "license": "MIT License", "lines": 67, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/graph.py
""" Graph Data Structures Reusable classes for representing nodes, directed edges and directed graphs. These can be shared across graph algorithms. """ from __future__ import annotations class Node: """A node (vertex) in a graph.""" def __init__(self, name: str) -> None: self.name = name @stat...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/graph.py", "license": "MIT License", "lines": 97, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/data_structures/hash_table.py
""" Hash Table (Open Addressing) Hash map implementation using open addressing with linear probing for collision resolution. Includes a resizable variant that doubles capacity when the load factor reaches two-thirds. Reference: https://en.wikipedia.org/wiki/Open_addressing Complexity: Time: O(1) average for put...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/hash_table.py", "license": "MIT License", "lines": 139, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/heap.py
r""" Binary Heap A min heap is a complete binary tree where each node is smaller than its children. The root is the minimum element. Uses an array representation with index 0 as a sentinel. Reference: https://en.wikipedia.org/wiki/Binary_heap Complexity: Time: O(log n) for insert and remove_min Space: O(n) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/heap.py", "license": "MIT License", "lines": 117, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/linked_list.py
""" Linked List Node Definitions Basic node classes for singly and doubly linked lists, serving as foundational building blocks for linked list algorithms. Reference: https://en.wikipedia.org/wiki/Linked_list Complexity: Time: O(1) for node creation Space: O(1) per node """ from __future__ import annotatio...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/linked_list.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/priority_queue.py
""" Priority Queue (Linear Array) A priority queue implementation using a sorted linear array. Elements are inserted in order so that extraction of the minimum is O(1). Reference: https://en.wikipedia.org/wiki/Priority_queue Complexity: Time: O(n) for push, O(1) for pop Space: O(n) """ from __future__ impo...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/priority_queue.py", "license": "MIT License", "lines": 85, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/queue.py
""" Queue Abstract Data Type Implementations of the queue ADT using both a fixed-size array and a linked list. Both support enqueue, dequeue, peek, is_empty, len, and iter. Reference: https://en.wikipedia.org/wiki/Queue_(abstract_data_type) Complexity: Time: O(1) for enqueue/dequeue/peek (amortized for ArrayQue...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/queue.py", "license": "MIT License", "lines": 161, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/segment_tree.py
""" Segment_tree creates a segment tree with a given array and function, allowing queries to be done later in log(N) time function takes 2 values and returns a same type value """ class SegmentTree: def __init__(self, arr, function): self.segment = [0 for x in range(3 * len(arr) + 3)] self.arr = a...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/segment_tree.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/data_structures/separate_chaining_hash_table.py
""" Separate Chaining Hash Table Hash table implementation using separate chaining (linked lists) for collision resolution. Reference: https://en.wikipedia.org/wiki/Hash_table#Separate_chaining Complexity: Time: O(1) average for put/get/del, O(n) worst case Space: O(n) """ from __future__ import annotation...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/separate_chaining_hash_table.py", "license": "MIT License", "lines": 113, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/stack.py
""" Stack Abstract Data Type Implementations of the stack ADT using both a fixed-size array and a linked list. Both support push, pop, peek, is_empty, len, iter, and str. Reference: https://en.wikipedia.org/wiki/Stack_(abstract_data_type) Complexity: Time: O(1) for push/pop/peek (amortized for ArrayStack) S...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/stack.py", "license": "MIT License", "lines": 150, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/data_structures/union_find.py
""" Union-Find (Disjoint Set) Data Structure A Union-Find data structure supporting add, find (root), and unite operations. Uses union by size and path compression for near-constant amortized time. Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure Complexity: Time: O(alpha(n)) amortized per o...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/data_structures/union_find.py", "license": "MIT License", "lines": 60, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/buy_sell_stock.py
""" Best Time to Buy and Sell Stock Given an array of stock prices, find the maximum profit from a single buy-sell transaction (buy before sell). Reference: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Complexity: max_profit_naive: Time: O(n^2) Space: O(1) max_profit_optimi...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/buy_sell_stock.py", "license": "MIT License", "lines": 48, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/climbing_stairs.py
""" Climbing Stairs Count the number of distinct ways to climb a staircase of n steps, where each move is either 1 or 2 steps. Reference: https://leetcode.com/problems/climbing-stairs/ Complexity: climb_stairs: Time: O(n) Space: O(n) climb_stairs_optimized: Time: O(n) Space:...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/climbing_stairs.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/coin_change.py
""" Coin Change (Number of Ways) Given a value and a set of coin denominations, count how many distinct combinations of coins sum to the given value. Reference: https://leetcode.com/problems/coin-change-ii/ Complexity: Time: O(n * m) where n is the value and m is the number of coins Space: O(n) """ from _...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/coin_change.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/combination_sum.py
""" Combination Sum IV Given an array of distinct positive integers and a target, find the number of possible combinations (order matters) that add up to the target. Reference: https://leetcode.com/problems/combination-sum-iv/ Complexity: combination_sum_topdown: Time: O(target * n) Space: O(tar...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/combination_sum.py", "license": "MIT License", "lines": 63, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/edit_distance.py
""" Edit Distance (Levenshtein Distance) Find the minimum number of insertions, deletions, and substitutions required to transform one word into another. Reference: https://en.wikipedia.org/wiki/Levenshtein_distance Complexity: Time: O(m * n) where m, n are the lengths of the two words Space: O(m * n) """ ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/edit_distance.py", "license": "MIT License", "lines": 38, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/egg_drop.py
""" Egg Drop Problem Given K eggs and a building with N floors, determine the minimum number of moves needed to find the critical floor F in the worst case. Reference: https://en.wikipedia.org/wiki/Dynamic_programming#Egg_dropping_puzzle Complexity: Time: O(n * k^2) Space: O(n * k) """ from __future__ impo...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/egg_drop.py", "license": "MIT License", "lines": 38, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/fib.py
""" Fibonacci Number Compute the n-th Fibonacci number using three different approaches: recursive, list-based DP, and iterative. Reference: https://en.wikipedia.org/wiki/Fibonacci_number Complexity: fib_recursive: Time: O(2^n) Space: O(n) (call stack) fib_list: Time: O(n) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/fib.py", "license": "MIT License", "lines": 67, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/hosoya_triangle.py
""" Hosoya Triangle The Hosoya triangle (originally Fibonacci triangle) is a triangular arrangement of numbers where each entry is the sum of two entries above it. Reference: https://en.wikipedia.org/wiki/Hosoya%27s_triangle Complexity: Time: O(n^3) (naive recursive per entry) Space: O(n) (call stack de...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/hosoya_triangle.py", "license": "MIT License", "lines": 45, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/house_robber.py
""" House Robber Determine the maximum amount of money that can be robbed from a row of houses without robbing two adjacent houses. Reference: https://leetcode.com/problems/house-robber/ Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def house_robber(houses: list[int]) -> int: ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/house_robber.py", "license": "MIT License", "lines": 24, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/int_divide.py
""" Integer Partition Count the number of ways a positive integer can be represented as a sum of positive integers (order does not matter). Reference: https://en.wikipedia.org/wiki/Partition_(number_theory) Complexity: Time: O(n^2) Space: O(n^2) """ from __future__ import annotations def int_divide(decom...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/int_divide.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/job_scheduling.py
""" Weighted Job Scheduling Given a set of jobs with start times, finish times, and profits, find the maximum profit subset such that no two jobs overlap. Reference: https://en.wikipedia.org/wiki/Job-shop_scheduling Complexity: Time: O(n^2) Space: O(n) """ from __future__ import annotations class Job: ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/job_scheduling.py", "license": "MIT License", "lines": 57, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/k_factor.py
""" K-Factor of a String The K factor of a string is the number of times 'abba' appears as a substring. Given a length and a k_factor, count the number of strings of that length whose K factor equals k_factor. Reference: https://en.wikipedia.org/wiki/Dynamic_programming Complexity: Time: O(length^2) Space: ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/k_factor.py", "license": "MIT License", "lines": 72, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/dynamic_programming/knapsack.py
""" 0/1 Knapsack Problem Given items with values and weights, and a knapsack capacity, find the maximum total value that fits in the knapsack. Reference: https://en.wikipedia.org/wiki/Knapsack_problem Complexity: Time: O(n * m) where n is the number of items and m is the capacity Space: O(m) """ from __fu...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/knapsack.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/longest_common_subsequence.py
""" Longest Common Subsequence Find the length of the longest subsequence common to two strings. Reference: https://en.wikipedia.org/wiki/Longest_common_subsequence Complexity: Time: O(m * n) Space: O(m * n) """ from __future__ import annotations def longest_common_subsequence(s_1: str, s_2: str) -> int:...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/longest_common_subsequence.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/longest_increasing.py
""" Longest Increasing Subsequence Find the length of the longest strictly increasing subsequence in an array. Reference: https://en.wikipedia.org/wiki/Longest_increasing_subsequence Complexity: longest_increasing_subsequence: Time: O(n^2) Space: O(n) longest_increasing_subsequence_optimized...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/longest_increasing.py", "license": "MIT License", "lines": 111, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/dynamic_programming/matrix_chain_order.py
""" Matrix Chain Multiplication Find the optimal parenthesization of a chain of matrices to minimize the total number of scalar multiplications. Reference: https://en.wikipedia.org/wiki/Matrix_chain_multiplication Complexity: Time: O(n^3) Space: O(n^2) """ from __future__ import annotations _INF = float("...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/matrix_chain_order.py", "license": "MIT License", "lines": 40, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/max_product_subarray.py
""" Maximum Product Subarray Find the contiguous subarray within an array that has the largest product. Reference: https://leetcode.com/problems/maximum-product-subarray/ Complexity: max_product: Time: O(n) Space: O(1) subarray_with_max_product: Time: O(n) Space: O(1) """ f...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/max_product_subarray.py", "license": "MIT License", "lines": 63, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/max_subarray.py
""" Maximum Subarray (Kadane's Algorithm) Find the contiguous subarray with the largest sum. Reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def max_subarray(array: list[int]) -> int: """Find the maximum sum o...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/max_subarray.py", "license": "MIT License", "lines": 24, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/min_cost_path.py
""" Minimum Cost Path Find the minimum cost to travel from station 0 to station N-1 given a cost matrix where cost[i][j] is the price of going from station i to station j (for i < j). Reference: https://en.wikipedia.org/wiki/Shortest_path_problem Complexity: Time: O(n^2) Space: O(n) """ from __future__ imp...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/min_cost_path.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/num_decodings.py
""" Decode Ways Given an encoded message of digits, count the total number of ways to decode it where 'A' = 1, 'B' = 2, ..., 'Z' = 26. Reference: https://leetcode.com/problems/decode-ways/ Complexity: Time: O(n) Space: O(1) for num_decodings, O(n) for num_decodings2 """ from __future__ import annotations ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/num_decodings.py", "license": "MIT License", "lines": 60, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/planting_trees.py
""" Planting Trees Given an even number of trees along one side of a road, calculate the minimum total distance to move them into valid positions on both sides at even intervals. Reference: https://en.wikipedia.org/wiki/Dynamic_programming Complexity: Time: O(n^2) Space: O(n^2) """ from __future__ import a...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/planting_trees.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/regex_matching.py
""" Regular Expression Matching Implement regular expression matching with support for '.' (matches any single character) and '*' (matches zero or more of the preceding element). Reference: https://leetcode.com/problems/regular-expression-matching/ Complexity: Time: O(m * n) Space: O(m * n) """ from __futu...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/regex_matching.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/rod_cut.py
""" Rod Cutting Problem Given a rod of length n and a list of prices for each piece length, determine the maximum revenue obtainable by cutting and selling the pieces. Reference: https://en.wikipedia.org/wiki/Cutting_stock_problem Complexity: Time: O(n^2) Space: O(n) """ from __future__ import annotations ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/rod_cut.py", "license": "MIT License", "lines": 29, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/dynamic_programming/word_break.py
""" Word Break Given a string and a dictionary of words, determine whether the string can be segmented into a sequence of dictionary words. Reference: https://leetcode.com/problems/word-break/ Complexity: Time: O(n^2) Space: O(n) """ from __future__ import annotations def word_break(word: str, word_dict:...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/dynamic_programming/word_break.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/all_factors.py
""" Factor Combinations Given an integer n, return all possible combinations of its factors (excluding 1 and n itself in the factorisation). Reference: https://leetcode.com/problems/factor-combinations/ Complexity: Time: O(n^(1/2) * log n) (approximate, depends on factor density) Space: O(log n) """ from ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/all_factors.py", "license": "MIT License", "lines": 79, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/count_islands_bfs.py
""" Count Islands (BFS) Given a 2D grid of 1s (land) and 0s (water), count the number of islands using breadth-first search. An island is a group of adjacent lands connected horizontally or vertically. Reference: https://leetcode.com/problems/number-of-islands/ Complexity: Time: O(M * N) Space: O(M * N) ""...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/count_islands_bfs.py", "license": "MIT License", "lines": 44, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/count_islands_dfs.py
""" Count Islands (DFS) Given a 2D grid of 1s (land) and 0s (water), count the number of islands using depth-first search. Reference: https://leetcode.com/problems/number-of-islands/ Complexity: Time: O(M * N) Space: O(M * N) recursion stack in worst case """ from __future__ import annotations def num_is...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/count_islands_dfs.py", "license": "MIT License", "lines": 43, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/count_islands_unionfind.py
""" Count Islands via Union-Find Uses the Union-Find (Disjoint Set) data structure to solve the "Number of Islands" problem. After each addLand operation, counts distinct connected components of land cells. Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure Complexity: Time: O(m * alpha(m)) wh...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/count_islands_unionfind.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/maze_search_bfs.py
""" Maze Search (BFS) Find the minimum number of steps from the top-left corner to the bottom-right corner of a grid. Only cells with value 1 may be traversed. Returns -1 if no path exists. Complexity: Time: O(M * N) Space: O(M * N) """ from __future__ import annotations from collections import deque de...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/maze_search_bfs.py", "license": "MIT License", "lines": 47, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/maze_search_dfs.py
""" Maze Search (DFS) Find the shortest path from the top-left corner to the bottom-right corner of a grid using depth-first search with backtracking. Only cells with value 1 may be traversed. Returns -1 if no path exists. Complexity: Time: O(4^(M*N)) worst case (backtracking) Space: O(M * N) """ from __f...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/maze_search_dfs.py", "license": "MIT License", "lines": 57, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/pacific_atlantic.py
""" Pacific Atlantic Water Flow Given an m*n matrix of heights, find all cells from which water can flow to both the Pacific (top / left edges) and Atlantic (bottom / right edges) oceans. Reference: https://leetcode.com/problems/pacific-atlantic-water-flow/ Complexity: Time: O(M * N) Space: O(M * N) """ fr...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/pacific_atlantic.py", "license": "MIT License", "lines": 65, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/shortest_distance_from_all_buildings.py
""" Shortest Distance from All Buildings Given a 2D grid with buildings (1), empty land (0) and obstacles (2), find the empty land with the smallest total distance to all buildings. Reference: https://leetcode.com/problems/shortest-distance-from-all-buildings/ Complexity: Time: O(B * M * N) where B is the numb...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/shortest_distance_from_all_buildings.py", "license": "MIT License", "lines": 63, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/sudoku_solver.py
""" Sudoku Solver (DFS / Backtracking) Solves a Sudoku puzzle using constraint propagation and depth-first search with backtracking, starting from the cell with the fewest possible values. Reference: https://leetcode.com/problems/sudoku-solver/ Complexity: Time: O(9^(empty cells)) worst case Space: O(N^2) "...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/sudoku_solver.py", "license": "MIT License", "lines": 121, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/graph/topological_sort_bfs.py
""" Topological Sort (Kahn's Algorithm / BFS) Computes a topological ordering of a directed acyclic graph. Raises ValueError when a cycle is detected. Reference: https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm Complexity: Time: O(V + E) Space: O(V + E) """ from __future__ i...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/topological_sort_bfs.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/topological_sort_dfs.py
""" Topological Sort Topological sort produces a linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge (u, v), vertex u comes before v. Two implementations are provided: one recursive (DFS-based) and one iterative. Reference: https://en.wikipedia.org/wiki/Topological_sorting...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/topological_sort_dfs.py", "license": "MIT License", "lines": 91, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/graph/walls_and_gates.py
""" Walls and Gates Fill each empty room (INF) with the distance to its nearest gate (0). Walls are represented by -1. Reference: https://leetcode.com/problems/walls-and-gates/ Complexity: Time: O(M * N) Space: O(M * N) recursion stack """ from __future__ import annotations def walls_and_gates(rooms: lis...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/walls_and_gates.py", "license": "MIT License", "lines": 39, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/word_ladder.py
""" Word Ladder (Bidirectional BFS) Given two words and a dictionary, find the length of the shortest transformation sequence where only one letter changes at each step and every intermediate word must exist in the dictionary. Reference: https://leetcode.com/problems/word-ladder/ Complexity: Time: O(N * L^2) w...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/word_ladder.py", "license": "MIT License", "lines": 61, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/add_two_numbers.py
""" Add Two Numbers (Linked List) Given two non-empty linked lists representing two non-negative integers with digits stored in reverse order, add the two numbers and return the sum as a linked list. Reference: https://leetcode.com/problems/add-two-numbers/ Complexity: Time: O(max(m, n)) Space: O(max(m, n))...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/add_two_numbers.py", "license": "MIT License", "lines": 84, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/copy_random_pointer.py
""" Copy List with Random Pointer Given a linked list where each node contains an additional random pointer that could point to any node in the list or null, return a deep copy of the list. Reference: https://leetcode.com/problems/copy-list-with-random-pointer/ Complexity: Time: O(n) Space: O(n) """ from _...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/copy_random_pointer.py", "license": "MIT License", "lines": 65, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/delete_node.py
""" Delete Node in a Linked List Given only access to a node (not the tail) in a singly linked list, delete that node by copying the next node's value and skipping over it. Reference: https://leetcode.com/problems/delete-node-in-a-linked-list/ Complexity: Time: O(1) Space: O(1) """ from __future__ import a...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/delete_node.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/first_cyclic_node.py
""" First Cyclic Node Given a linked list, find the first node of a cycle in it using Floyd's cycle-finding algorithm (Tortoise and Hare). Reference: https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations class Nod...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/first_cyclic_node.py", "license": "MIT License", "lines": 38, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/intersection.py
""" Intersection of Two Linked Lists Given two singly linked lists that converge at some node, find and return the intersecting node. The node identity (not value) is the unique identifier. Reference: https://leetcode.com/problems/intersection-of-two-linked-lists/ Complexity: Time: O(m + n) Space: O(1) """ ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/intersection.py", "license": "MIT License", "lines": 58, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/linked_list/is_cyclic.py
""" Linked List Cycle Detection Given a linked list, determine if it has a cycle using Floyd's Tortoise and Hare algorithm without extra space. Reference: https://leetcode.com/problems/linked-list-cycle/ Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations class Node: def __init_...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/is_cyclic.py", "license": "MIT License", "lines": 38, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/is_palindrome.py
""" Palindrome Linked List Determine whether a singly linked list is a palindrome. Three approaches are provided: reverse-half, stack-based, and dictionary-based. Reference: https://leetcode.com/problems/palindrome-linked-list/ Complexity (reverse-half): Time: O(n) Space: O(1) """ from __future__ import an...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/is_palindrome.py", "license": "MIT License", "lines": 100, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/linked_list/is_sorted.py
""" Is Sorted Linked List Given a linked list, determine whether the list is sorted in non-decreasing order. An empty list is considered sorted. Reference: https://en.wikipedia.org/wiki/Linked_list Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def is_sorted(head: object | None...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/is_sorted.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/kth_to_last.py
""" Kth to Last Element Find the kth to last element of a singly linked list. Three approaches are provided: eval-based, dictionary-based, and two-pointer iterative. Reference: https://en.wikipedia.org/wiki/Linked_list Complexity (two-pointer): Time: O(n) Space: O(1) """ from __future__ import annotations ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/kth_to_last.py", "license": "MIT License", "lines": 85, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/merge_two_list.py
""" Merge Two Sorted Lists Merge two sorted linked lists into a single sorted list by splicing together the nodes of the two input lists. Reference: https://leetcode.com/problems/merge-two-sorted-lists/ Complexity: Time: O(m + n) Space: O(1) iterative, O(m + n) recursive """ from __future__ import annotati...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/merge_two_list.py", "license": "MIT License", "lines": 61, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/partition.py
""" Partition Linked List Partition a linked list around a value x so that all nodes with values less than x come before nodes with values greater than or equal to x. Reference: https://leetcode.com/problems/partition-list/ Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations class N...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/partition.py", "license": "MIT License", "lines": 48, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/remove_duplicates.py
""" Remove Duplicates from Linked List Remove duplicate values from an unsorted linked list. Two approaches are provided: hash-set-based (O(n) time, O(n) space) and runner technique (O(n^2) time, O(1) space). Reference: https://en.wikipedia.org/wiki/Linked_list Complexity (hash set): Time: O(n) Space: O(n) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/remove_duplicates.py", "license": "MIT License", "lines": 60, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/remove_range.py
""" Remove Range from Linked List Given a linked list and a start and end index, remove the elements at those indexes (inclusive) from the list. Reference: https://en.wikipedia.org/wiki/Linked_list Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def remove_range(head: object | N...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/remove_range.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/reverse.py
""" Reverse Linked List Reverse a singly linked list. Both iterative and recursive solutions are provided. Reference: https://leetcode.com/problems/reverse-linked-list/ Complexity: Time: O(n) Space: O(1) iterative, O(n) recursive """ from __future__ import annotations def reverse_list(head: object | None...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/reverse.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/rotate_list.py
""" Rotate List Given a linked list, rotate the list to the right by k places, where k is non-negative. Reference: https://leetcode.com/problems/rotate-list/ Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations def rotate_right(head: object | None, k: int) -> object | None: """Ro...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/rotate_list.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/linked_list/swap_in_pairs.py
""" Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return the new head. Only node links are changed, not node values. Reference: https://leetcode.com/problems/swap-nodes-in-pairs/ Complexity: Time: O(n) Space: O(1) """ from __future__ import annotations class Node: def __i...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/linked_list/swap_in_pairs.py", "license": "MIT License", "lines": 39, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/base_conversion.py
""" Integer Base Conversion Convert integers between arbitrary bases (2-36). Supports conversion from integer to string representation in a given base, and vice versa. Reference: https://en.wikipedia.org/wiki/Positional_notation Complexity: Time: O(log_base(num)) for both directions Space: O(log_base(num)) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/base_conversion.py", "license": "MIT License", "lines": 62, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/chinese_remainder_theorem.py
""" Chinese Remainder Theorem Solves a system of simultaneous congruences using the Chinese Remainder Theorem. Given pairwise coprime moduli, finds the smallest positive integer satisfying all congruences. Reference: https://en.wikipedia.org/wiki/Chinese_remainder_theorem Complexity: Time: O(n * m) where n is t...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/chinese_remainder_theorem.py", "license": "MIT License", "lines": 57, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/combination.py
""" Combinations (nCr) Calculate the number of ways to choose r items from n items (binomial coefficient) using recursive and memoized approaches. Reference: https://en.wikipedia.org/wiki/Combination Complexity: Time: O(2^n) naive recursive, O(n*r) memoized Space: O(n) recursive stack, O(n*r) memoized """ ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/combination.py", "license": "MIT License", "lines": 45, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/cosine_similarity.py
""" Cosine Similarity Calculate the cosine similarity between two vectors, which measures the cosine of the angle between them. Values range from -1 (opposite) to 1 (identical direction). Reference: https://en.wikipedia.org/wiki/Cosine_similarity Complexity: Time: O(n) Space: O(1) """ from __future__ impor...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/cosine_similarity.py", "license": "MIT License", "lines": 51, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/decimal_to_binary_ip.py
""" Decimal to Binary IP Conversion Convert an IP address from dotted-decimal notation to its binary representation. Reference: https://en.wikipedia.org/wiki/IP_address Complexity: Time: O(1) (fixed 4 octets, 8 bits each) Space: O(1) """ from __future__ import annotations def decimal_to_binary_util(val: ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/decimal_to_binary_ip.py", "license": "MIT License", "lines": 45, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/diffie_hellman_key_exchange.py
""" Diffie-Hellman Key Exchange Implements the Diffie-Hellman key exchange protocol, which enables two parties to establish a shared secret over an insecure channel using discrete logarithm properties. Reference: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange Complexity: Time: O(p) for primit...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/diffie_hellman_key_exchange.py", "license": "MIT License", "lines": 165, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/distance_between_two_points.py
""" Distance Between Two Points in 2D Space Calculate the Euclidean distance between two points using the distance formula derived from the Pythagorean theorem. Reference: https://en.wikipedia.org/wiki/Euclidean_distance Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations from math i...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/distance_between_two_points.py", "license": "MIT License", "lines": 27, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/euler_totient.py
""" Euler's Totient Function Compute Euler's totient function phi(n), which counts the number of integers from 1 to n inclusive that are coprime to n. Reference: https://en.wikipedia.org/wiki/Euler%27s_totient_function Complexity: Time: O(sqrt(n)) Space: O(1) """ from __future__ import annotations def eu...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/euler_totient.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/extended_gcd.py
""" Extended Euclidean Algorithm Find coefficients s and t (Bezout's identity) such that: num1 * s + num2 * t = gcd(num1, num2). Reference: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm Complexity: Time: O(log(min(num1, num2))) Space: O(1) """ from __future__ import annotations def extended_...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/extended_gcd.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/factorial.py
""" Factorial Compute the factorial of a non-negative integer, with optional modular arithmetic support. Reference: https://en.wikipedia.org/wiki/Factorial Complexity: Time: O(n) Space: O(1) iterative, O(n) recursive """ from __future__ import annotations def factorial(n: int, mod: int | None = None) -> ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/factorial.py", "license": "MIT License", "lines": 60, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/fft.py
""" Fast Fourier Transform (Cooley-Tukey) Compute the Discrete Fourier Transform of a sequence using the Cooley-Tukey radix-2 decimation-in-time algorithm. Input length must be a power of 2. Reference: https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm Complexity: Time: O(n log n) Space: O(n l...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/fft.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/find_order_simple.py
""" Multiplicative Order Find the multiplicative order of a modulo n, which is the smallest positive integer k such that a^k = 1 (mod n). Requires gcd(a, n) = 1. Reference: https://en.wikipedia.org/wiki/Multiplicative_order Complexity: Time: O(n log n) Space: O(1) """ from __future__ import annotations im...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/find_order_simple.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/find_primitive_root_simple.py
""" Primitive Root Finder Find all primitive roots of a positive integer n. A primitive root modulo n is an integer whose multiplicative order modulo n equals Euler's totient of n. Reference: https://en.wikipedia.org/wiki/Primitive_root_modulo_n Complexity: Time: O(n^2 log n) Space: O(n) """ from __future_...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/find_primitive_root_simple.py", "license": "MIT License", "lines": 67, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/gcd.py
""" Greatest Common Divisor and Least Common Multiple Compute the GCD and LCM of two integers using Euclid's algorithm and a bitwise variant. Reference: https://en.wikipedia.org/wiki/Euclidean_algorithm Complexity: Time: O(log(min(a, b))) for gcd, O(log(min(a, b))) for lcm Space: O(1) """ from __future__ i...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/gcd.py", "license": "MIT License", "lines": 86, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/generate_strobogrammtic.py
""" Generate Strobogrammatic Numbers A strobogrammatic number looks the same when rotated 180 degrees. Generate all strobogrammatic numbers of a given length or count them within a range. Reference: https://en.wikipedia.org/wiki/Strobogrammatic_number Complexity: Time: O(5^(n/2)) for generation Space: O(5^(...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/generate_strobogrammtic.py", "license": "MIT License", "lines": 89, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/hailstone.py
""" Hailstone Sequence (Collatz Conjecture) Generate the hailstone sequence starting from n: if n is even, next is n/2; if n is odd, next is 3n + 1. The sequence ends when it reaches 1. Reference: https://en.wikipedia.org/wiki/Collatz_conjecture Complexity: Time: O(unknown) - conjectured to always terminate ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/hailstone.py", "license": "MIT License", "lines": 27, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/is_strobogrammatic.py
""" Strobogrammatic Number Check Determine whether a number (as a string) is strobogrammatic, meaning it looks the same when rotated 180 degrees. Reference: https://en.wikipedia.org/wiki/Strobogrammatic_number Complexity: Time: O(n) where n is the length of the number string Space: O(1) for is_strobogrammat...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/is_strobogrammatic.py", "license": "MIT License", "lines": 44, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/krishnamurthy_number.py
""" Krishnamurthy Number A Krishnamurthy number is a number whose sum of the factorials of its digits equals the number itself (e.g., 145 = 1! + 4! + 5!). Reference: https://en.wikipedia.org/wiki/Factorion Complexity: Time: O(d * m) where d is number of digits and m is max digit value Space: O(1) """ from ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/krishnamurthy_number.py", "license": "MIT License", "lines": 42, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/magic_number.py
""" Magic Number A magic number is a number where recursively summing its digits eventually yields 1. For example, 199 -> 1+9+9=19 -> 1+9=10 -> 1+0=1. Reference: https://en.wikipedia.org/wiki/Digital_root Complexity: Time: O(log n) amortized Space: O(1) """ from __future__ import annotations def magic_nu...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/magic_number.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/modular_exponential.py
""" Modular Exponentiation Compute (base ^ exponent) % mod efficiently using binary exponentiation (repeated squaring). Reference: https://en.wikipedia.org/wiki/Modular_exponentiation Complexity: Time: O(log exponent) Space: O(1) """ from __future__ import annotations def modular_exponential(base: int, e...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/modular_exponential.py", "license": "MIT License", "lines": 34, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/modular_inverse.py
""" Modular Multiplicative Inverse Find x such that a * x = 1 (mod m) using the Extended Euclidean Algorithm. Requires a and m to be coprime. Reference: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse Complexity: Time: O(log(min(a, m))) Space: O(1) """ from __future__ import annotations def ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/modular_inverse.py", "license": "MIT License", "lines": 44, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/next_bigger.py
""" Next Bigger Number with Same Digits Given a number, find the next higher number that uses the exact same set of digits. This is equivalent to finding the next permutation. Reference: https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order Complexity: Time: O(n) where n is the number of d...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/next_bigger.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/next_perfect_square.py
""" Next Perfect Square Given a number, find the next perfect square if the input is itself a perfect square. Otherwise, return -1. Reference: https://en.wikipedia.org/wiki/Square_number Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations def find_next_square(sq: float) -> float: ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/next_perfect_square.py", "license": "MIT License", "lines": 40, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/nth_digit.py
""" Find the Nth Digit Find the nth digit in the infinite sequence 1, 2, 3, ..., 9, 10, 11, 12, ... by determining which number contains it and extracting the specific digit. Reference: https://en.wikipedia.org/wiki/Positional_notation Complexity: Time: O(log n) Space: O(log n) for string conversion """ fr...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/nth_digit.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/num_digits.py
""" Number of Digits Count the number of digits in an integer using logarithmic computation for O(1) time complexity. Reference: https://en.wikipedia.org/wiki/Logarithm Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations import math def num_digits(n: int) -> int: """Count the n...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/num_digits.py", "license": "MIT License", "lines": 29, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/num_perfect_squares.py
""" Minimum Perfect Squares Sum Determine the minimum number of perfect squares that sum to a given integer. By Lagrange's four-square theorem, the answer is always between 1 and 4. Reference: https://en.wikipedia.org/wiki/Lagrange%27s_four-square_theorem Complexity: Time: O(sqrt(n)) Space: O(1) """ from _...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/num_perfect_squares.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/polynomial.py
""" Polynomial and Monomial Arithmetic A symbolic algebra system for polynomials and monomials supporting addition, subtraction, multiplication, division, substitution, and polynomial long division with Fraction-based exact arithmetic. Reference: https://en.wikipedia.org/wiki/Polynomial Complexity: Time: Varies...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/polynomial.py", "license": "MIT License", "lines": 574, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/math/power.py
""" Binary Exponentiation Compute a^n efficiently using binary exponentiation (exponentiation by squaring), with optional modular arithmetic. Reference: https://en.wikipedia.org/wiki/Exponentiation_by_squaring Complexity: Time: O(log n) Space: O(1) iterative, O(log n) recursive """ from __future__ import a...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/power.py", "license": "MIT License", "lines": 58, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/prime_check.py
""" Primality Test Check whether a given integer is prime using trial division with 6k +/- 1 optimization. Reference: https://en.wikipedia.org/wiki/Primality_test Complexity: Time: O(sqrt(n)) Space: O(1) """ from __future__ import annotations def prime_check(n: int) -> bool: """Check whether n is a p...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/prime_check.py", "license": "MIT License", "lines": 34, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/primes_sieve_of_eratosthenes.py
""" Sieve of Eratosthenes Generate all prime numbers less than n using an optimized sieve that skips even numbers. Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes Complexity: Time: O(n log log n) Space: O(n) """ from __future__ import annotations def get_primes(n: int) -> list[int]: ""...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/primes_sieve_of_eratosthenes.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/pythagoras.py
""" Pythagorean Theorem Given the lengths of two sides of a right-angled triangle, compute the length of the third side using the Pythagorean theorem. Reference: https://en.wikipedia.org/wiki/Pythagorean_theorem Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations def pythagoras( ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/pythagoras.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation