sample_id
stringlengths
21
196
text
stringlengths
105
936k
metadata
dict
category
stringclasses
6 values
keon/algorithms:algorithms/math/rabin_miller.py
""" Rabin-Miller Primality Test A probabilistic primality test where returning False guarantees the number is composite, and returning True means the number is probably prime with a 4^(-k) chance of error. Reference: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Complexity: Time: O(k * log^2...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/rabin_miller.py", "license": "MIT License", "lines": 61, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/recursive_binomial_coefficient.py
""" Recursive Binomial Coefficient Calculate the binomial coefficient C(n, k) using a recursive formula with the identity C(n, k) = (n/k) * C(n-1, k-1). Reference: https://en.wikipedia.org/wiki/Binomial_coefficient Complexity: Time: O(k) Space: O(k) recursive stack """ from __future__ import annotations ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/recursive_binomial_coefficient.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/rsa.py
""" RSA Encryption Algorithm Implements RSA key generation, encryption, and decryption. RSA uses separate public and private keys where ((x^e)^d) % n == x % n. Reference: https://en.wikipedia.org/wiki/RSA_(cryptosystem) Complexity: Time: O(k^3) for key generation (k = bit length) Space: O(k) """ from __fut...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/rsa.py", "license": "MIT License", "lines": 95, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/sqrt_precision_factor.py
""" Square Root by Newton's Method Compute the square root of a positive number using Newton's method (Babylonian method) with a configurable precision factor. Reference: https://en.wikipedia.org/wiki/Newton%27s_method#Square_root Complexity: Time: O(log(1/epsilon)) iterations for convergence Space: O(1) ""...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/sqrt_precision_factor.py", "license": "MIT License", "lines": 25, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/summing_digits.py
""" Summing Digits Power Find all integers in a range where each digit raised to its positional power (1-indexed) sums to the number itself (e.g., 89 = 8^1 + 9^2). Reference: https://en.wikipedia.org/wiki/Perfect_digit-to-digit_invariant Complexity: Time: O((high - low) * d) where d is the number of digits ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/summing_digits.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/surface_area_of_torus.py
""" Surface Area of a Torus Calculate the surface area of a torus given its major and minor radii using the formula A = 4 * pi^2 * R * r. Reference: https://en.wikipedia.org/wiki/Torus Complexity: Time: O(1) Space: O(1) """ from __future__ import annotations from math import pi def surface_area_of_torus...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/surface_area_of_torus.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/math/symmetry_group_cycle_index.py
""" Symmetry Group Cycle Index Compute the cycle index polynomial of the symmetric group S_n and use it to count distinct configurations of grids under row/column permutations via Burnside's lemma. Reference: https://en.wikipedia.org/wiki/Cycle_index#Symmetric_group_Sn Complexity: Time: O(n!) for cycle index co...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/math/symmetry_group_cycle_index.py", "license": "MIT License", "lines": 109, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/queue/max_sliding_window.py
""" Max Sliding Window (Deque-based) Given an array and a window size k, find the maximum element in each sliding window using a monotonic deque that stores indices of elements in decreasing order of their values. Reference: https://leetcode.com/problems/sliding-window-maximum/ Complexity: Time: O(n) Space:...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/queue/max_sliding_window.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/queue/moving_average.py
""" Moving Average from Data Stream Calculate the moving average of integers in a sliding window of fixed size using a bounded deque. Reference: https://leetcode.com/problems/moving-average-from-data-stream/ Complexity: Time: O(1) per call to next Space: O(size) """ from __future__ import annotations from...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/queue/moving_average.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/queue/reconstruct_queue.py
""" Reconstruct Queue by Height Given a list of people described by (height, k) pairs where k is the number of taller-or-equal people in front, reconstruct the queue by sorting and inserting. Reference: https://leetcode.com/problems/queue-reconstruction-by-height/ Complexity: Time: O(n^2) Space: O(n) """ f...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/queue/reconstruct_queue.py", "license": "MIT License", "lines": 26, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/queue/zigzagiterator.py
""" Zigzag Iterator Interleave elements from two lists in a zigzag fashion. Elements are yielded alternately from each list until both are exhausted. Reference: https://leetcode.com/problems/zigzag-iterator/ Complexity: Time: O(n) total across all next() calls Space: O(n) """ from __future__ import annotat...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/queue/zigzagiterator.py", "license": "MIT License", "lines": 42, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/binary_search.py
""" Binary Search Search for an element in a sorted array by repeatedly dividing the search interval in half. Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm Complexity: Time: O(1) best / O(log n) average / O(log n) worst Space: O(1) iterative, O(log n) recursive """ from __future__ import...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/binary_search.py", "license": "MIT License", "lines": 57, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/find_min_rotate.py
""" Find Minimum in Rotated Sorted Array Find the minimum element in a sorted array that has been rotated at some unknown pivot. Assumes no duplicates exist in the array. Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm Complexity: Time: O(1) best / O(log n) average / O(log n) worst Space: O...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/find_min_rotate.py", "license": "MIT License", "lines": 49, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/first_occurrence.py
""" First Occurrence Find the index of the first occurrence of a target value in a sorted array using binary search. Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm Complexity: Time: O(1) best / O(log n) average / O(log n) worst Space: O(1) """ from __future__ import annotations def firs...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/first_occurrence.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/generalized_binary_search.py
""" Generalized Binary Search Find the smallest value in a numeric range for which a monotonic boolean predicate evaluates to True. Instead of searching for a specific value in an array, this version accepts an arbitrary predicate, allowing the same binary search logic to be reused across many problem domains. Refer...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/generalized_binary_search.py", "license": "MIT License", "lines": 49, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/interpolation_search.py
""" Interpolation Search Search for a target value in a uniformly distributed sorted array by estimating the position of the target using linear interpolation. Reference: https://en.wikipedia.org/wiki/Interpolation_search Complexity: Time: O(1) best / O(log log n) average / O(n) worst Space: O(1) """ from ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/interpolation_search.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/jump_search.py
""" Jump Search Search for a target value in a sorted array by jumping ahead in fixed-size blocks and then performing a linear search within the identified block. Reference: https://en.wikipedia.org/wiki/Jump_search Complexity: Time: O(1) best / O(sqrt n) average / O(sqrt n) worst Space: O(1) """ from __fu...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/jump_search.py", "license": "MIT License", "lines": 40, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/last_occurrence.py
""" Last Occurrence Find the index of the last occurrence of a target value in a sorted array using binary search. Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm Complexity: Time: O(1) best / O(log n) average / O(log n) worst Space: O(1) """ from __future__ import annotations def last_o...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/last_occurrence.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/linear_search.py
""" Linear Search Search for a target value in an array by checking every element sequentially. The array does not need to be sorted. Reference: https://en.wikipedia.org/wiki/Linear_search Complexity: Time: O(1) best / O(n) average / O(n) worst Space: O(1) """ from __future__ import annotations def linea...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/linear_search.py", "license": "MIT License", "lines": 27, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/next_greatest_letter.py
""" Next Greatest Letter Given a sorted list of lowercase letters and a target letter, find the smallest letter in the list that is larger than the target. Letters wrap around, so if the target is greater than or equal to the last letter the answer is the first letter. Reference: https://leetcode.com/problems/find-sm...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/next_greatest_letter.py", "license": "MIT License", "lines": 70, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/search_insert.py
""" Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm Complexity: Time: O(1) best / O(log n) average / O(log n) worst ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/search_insert.py", "license": "MIT License", "lines": 38, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/search_range.py
""" Search Range Given a sorted array of integers and a target value, find the starting and ending positions of the target. Returns [-1, -1] if the target is not found. Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm Complexity: Time: O(log n + k) where k is the number of occurrences in the wo...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/search_range.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/search_rotate.py
""" Search in Rotated Sorted Array Search for a target value in an array that was sorted in ascending order and then rotated at some unknown pivot. One half of the array is always in sorted order; we identify that half and decide which side to search. Reference: https://en.wikipedia.org/wiki/Binary_search_algorithm ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/search_rotate.py", "license": "MIT License", "lines": 72, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/ternary_search.py
""" Ternary Search Search for a target value in a sorted array by dividing the search range into three equal parts instead of two. At each step two midpoints are computed and the search range is narrowed to one third. Reference: https://en.wikipedia.org/wiki/Ternary_search Complexity: Time: O(1) best / O(log3 ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/ternary_search.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/searching/two_sum.py
""" Two Sum Given a sorted array of integers and a target sum, find the 1-based indices of the two numbers that add up to the target. Three approaches are provided: binary search, hash table, and two pointers. Reference: https://en.wikipedia.org/wiki/Subset_sum_problem Complexity: two_sum -- O(n log n) time, ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/searching/two_sum.py", "license": "MIT License", "lines": 82, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/bead_sort.py
""" Bead Sort Bead sort (also known as gravity sort) simulates how beads settle under gravity on an abacus. It only works with non-negative integers. Reference: https://en.wikipedia.org/wiki/Bead_sort Complexity: Time: O(n) best / O(n * max_value) average / O(n * max_value) worst Space: O(n * max_value) ""...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/bead_sort.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/bitonic_sort.py
""" Bitonic Sort Bitonic sort is a comparison-based sorting algorithm designed for parallel execution. This implementation is sequential. The input size must be a power of two. Reference: https://en.wikipedia.org/wiki/Bitonic_sorter Complexity: Time: O(n log^2 n) best / O(n log^2 n) average / O(n log^2 n) wor...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/bitonic_sort.py", "license": "MIT License", "lines": 48, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/bogo_sort.py
""" Bogo Sort Bogo sort repeatedly shuffles the array at random until it happens to be sorted. It is extremely inefficient and used only for educational purposes. Reference: https://en.wikipedia.org/wiki/Bogosort Complexity: Time: O(n) best / O(n * n!) average / O(infinity) worst Space: O(1) """ from __fu...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/bogo_sort.py", "license": "MIT License", "lines": 29, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/bubble_sort.py
""" Bubble Sort Bubble sort repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. Reference: https://en.wikipedia.org/wiki/Bubble_sort Complexity: Time: O(n) best / O(n^2) average / O(n^2) worst Space: O(1) """ from __future__ import annotations def ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/bubble_sort.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/bucket_sort.py
""" Bucket Sort Bucket sort distributes elements into a number of buckets, sorts each bucket individually (here using insertion sort), and then concatenates all buckets. Reference: https://en.wikipedia.org/wiki/Bucket_sort Complexity: Time: O(n + k) best / O(n + k) average / O(n^2) worst Space: O(n + k) """...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/bucket_sort.py", "license": "MIT License", "lines": 41, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/cocktail_shaker_sort.py
""" Cocktail Shaker Sort Cocktail shaker sort is a variation of bubble sort that traverses the list alternately from left-to-right and right-to-left. Reference: https://en.wikipedia.org/wiki/Cocktail_shaker_sort Complexity: Time: O(n) best / O(n^2) average / O(n^2) worst Space: O(1) """ from __future__ imp...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/cocktail_shaker_sort.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/comb_sort.py
""" Comb Sort Comb sort improves on bubble sort by using a gap sequence that shrinks by a factor of approximately 1.3 on each pass, eliminating small values near the end of the list (known as "turtles") more quickly. Reference: https://en.wikipedia.org/wiki/Comb_sort Complexity: Time: O(n log n) best / O(n^2) a...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/comb_sort.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/counting_sort.py
""" Counting Sort Counting sort counts the occurrences of each value and uses cumulative counts to place each element in its correct position. It supports negative integers by shifting values internally. Reference: https://en.wikipedia.org/wiki/Counting_sort Complexity: Time: O(n + k) best / O(n + k) average /...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/counting_sort.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/cycle_sort.py
""" Cycle Sort Cycle sort decomposes the permutation into cycles and rotates each cycle to produce a sorted result. It minimises the number of writes to the array, making it useful when writes are expensive. Reference: https://en.wikipedia.org/wiki/Cycle_sort Complexity: Time: O(n^2) best / O(n^2) average / O(...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/cycle_sort.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/exchange_sort.py
""" Exchange Sort Exchange sort compares every pair of elements and swaps them if they are out of order. It is conceptually similar to bubble sort. Reference: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort Complexity: Time: O(n^2) best / O(n^2) average / O(n^2) worst Space: O(1) """ from __...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/exchange_sort.py", "license": "MIT License", "lines": 26, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/gnome_sort.py
""" Gnome Sort Gnome sort moves an element toward the front of the list until it finds an element that is smaller or equal, then steps forward again. It is similar to insertion sort but uses swaps instead of shifts. Reference: https://en.wikipedia.org/wiki/Gnome_sort Complexity: Time: O(n) best / O(n^2) averag...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/gnome_sort.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/heap_sort.py
""" Heap Sort Heap sort builds a heap from the data and repeatedly extracts the extreme element to produce a sorted array. Two variants are provided: max-heap sort and min-heap sort. Reference: https://en.wikipedia.org/wiki/Heapsort Complexity: Time: O(n log n) best / O(n log n) average / O(n log n) worst ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/heap_sort.py", "license": "MIT License", "lines": 73, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/insertion_sort.py
""" Insertion Sort Insertion sort builds the sorted list one element at a time by repeatedly picking the next element and inserting it into its correct position. Reference: https://en.wikipedia.org/wiki/Insertion_sort Complexity: Time: O(n) best / O(n^2) average / O(n^2) worst Space: O(1) """ from __future...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/insertion_sort.py", "license": "MIT License", "lines": 28, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/meeting_rooms.py
""" Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1, e1], [s2, e2], ...] (si < ei), determine if a person could attend all meetings (i.e. no two meetings overlap). Reference: https://leetcode.com/problems/meeting-rooms/ Complexity: Time: O(n log n) best / O(n log n)...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/meeting_rooms.py", "license": "MIT License", "lines": 26, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/merge_sort.py
""" Merge Sort Merge sort divides the array in half, recursively sorts each half, and then merges the two sorted halves back together. Reference: https://en.wikipedia.org/wiki/Merge_sort Complexity: Time: O(n log n) best / O(n log n) average / O(n log n) worst Space: O(n) """ from __future__ import annotat...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/merge_sort.py", "license": "MIT License", "lines": 47, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/pancake_sort.py
""" Pancake Sort Pancake sort sorts an array by repeatedly finding the maximum element in the unsorted portion, flipping it to the front, and then flipping the entire unsorted portion so the maximum lands at the end. Reference: https://en.wikipedia.org/wiki/Pancake_sorting Complexity: Time: O(n) best / O(n^2) a...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/pancake_sort.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/pigeonhole_sort.py
""" Pigeonhole Sort Pigeonhole sort is suitable for sorting lists where the number of elements and the range of possible key values are approximately equal. Reference: https://en.wikipedia.org/wiki/Pigeonhole_sort Complexity: Time: O(n + range) best / O(n + range) average / O(n + range) worst Space: O(range...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/pigeonhole_sort.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/quick_sort.py
""" Quick Sort Quick sort selects a pivot element, partitions the array around the pivot, and recursively sorts the two partitions. Reference: https://en.wikipedia.org/wiki/Quicksort Complexity: Time: O(n log n) best / O(n log n) average / O(n^2) worst Space: O(log n) """ from __future__ import annotations...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/quick_sort.py", "license": "MIT License", "lines": 40, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/radix_sort.py
""" Radix Sort Radix sort processes digits from least significant to most significant, distributing elements into buckets for each digit and collecting them back in order. Reference: https://en.wikipedia.org/wiki/Radix_sort Complexity: Time: O(n * k) best / O(n * k) average / O(n * k) worst Space: O(n + k) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/radix_sort.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/selection_sort.py
""" Selection Sort Selection sort repeatedly selects the smallest element from the unsorted portion and moves it to the end of the sorted portion. Reference: https://en.wikipedia.org/wiki/Selection_sort Complexity: Time: O(n^2) best / O(n^2) average / O(n^2) worst Space: O(1) """ from __future__ import ann...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/selection_sort.py", "license": "MIT License", "lines": 27, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/shell_sort.py
""" Shell Sort Shell sort is a generalisation of insertion sort that allows the exchange of elements that are far apart. The gap between compared elements is gradually reduced until it becomes 1, at which point the algorithm behaves like a standard insertion sort. Reference: https://en.wikipedia.org/wiki/Shellsort ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/shell_sort.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/sort_colors.py
""" Sort Colors (Dutch National Flag) Given an array with n objects colored red, white, or blue (represented by 0, 1, and 2), sort them in-place so that objects of the same color are adjacent, with the colors in order red, white, blue. Reference: https://leetcode.com/problems/sort-colors/ Complexity: Time: O(n)...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/sort_colors.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/stooge_sort.py
""" Stooge Sort Stooge sort is a recursive sorting algorithm notable for its unusually bad time complexity. It works by recursively sorting the first 2/3, then the last 2/3, and then the first 2/3 again. Reference: https://en.wikipedia.org/wiki/Stooge_sort Complexity: Time: O(n^2.709) best / O(n^2.709) average...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/stooge_sort.py", "license": "MIT License", "lines": 34, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/sorting/wiggle_sort.py
""" Wiggle Sort Given an unsorted array, reorder it in-place such that nums[0] < nums[1] > nums[2] < nums[3] ... Reference: https://leetcode.com/problems/wiggle-sort/ Complexity: Time: O(n) best / O(n) average / O(n) worst Space: O(1) """ from __future__ import annotations def wiggle_sort(array: list[int...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/sorting/wiggle_sort.py", "license": "MIT License", "lines": 24, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/add_binary.py
""" Add Binary Given two binary strings, return their sum as a binary string. Reference: https://leetcode.com/problems/add-binary/ Complexity: Time: O(max(m, n)) where m, n are lengths of the two strings Space: O(max(m, n)) """ from __future__ import annotations def add_binary(first: str, second: str) ->...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/add_binary.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/atbash_cipher.py
""" Atbash Cipher Atbash cipher maps each letter of the alphabet to its reverse. The first letter 'a' maps to 'z', 'b' maps to 'y', and so on. Reference: https://en.wikipedia.org/wiki/Atbash Complexity: Time: O(n) where n is the length of the input string Space: O(n) """ from __future__ import annotations ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/atbash_cipher.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/breaking_bad.py
""" Breaking Bad Symbol Matching Given an array of words and an array of symbols, display each word with its matched symbol surrounded by square brackets. If a word matches more than one symbol, choose the one with the longest length. Reference: https://en.wikipedia.org/wiki/Trie Complexity: Time: O(n * m) for ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/breaking_bad.py", "license": "MIT License", "lines": 99, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/caesar_cipher.py
""" Caesar Cipher Caesar's cipher shifts each letter by a fixed number of positions in the alphabet. Letters wrap around when they pass the end of the alphabet. Reference: https://en.wikipedia.org/wiki/Caesar_cipher Complexity: Time: O(n) where n is the length of the input string Space: O(n) """ from __fut...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/caesar_cipher.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/check_pangram.py
""" Check Pangram Checks whether a given string is a pangram, meaning it contains every letter of the English alphabet at least once. Reference: https://en.wikipedia.org/wiki/Pangram Complexity: Time: O(n) where n is the length of the input string Space: O(1) """ from __future__ import annotations def ch...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/check_pangram.py", "license": "MIT License", "lines": 22, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/contain_string.py
""" Contain String (strStr) Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Reference: https://leetcode.com/problems/implement-strstr/ Complexity: Time: O(n * m) worst case, where n = len(haystack), m = len(needle) Space: O(1) """ from __future__ imp...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/contain_string.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/count_binary_substring.py
""" Count Binary Substrings Count the number of non-empty contiguous substrings that have the same number of 0s and 1s, where all 0s and all 1s are grouped consecutively. Reference: https://leetcode.com/problems/count-binary-substrings/ Complexity: Time: O(n) where n is the length of the string Space: O(1) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/count_binary_substring.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/decode_string.py
""" Decode String Given an encoded string, return its decoded string. The encoding rule is k[encoded_string], where the encoded_string inside the brackets is repeated exactly k times. Reference: https://leetcode.com/problems/decode-string/ Complexity: Time: O(n * max_k) where n is the length of the string S...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/decode_string.py", "license": "MIT License", "lines": 37, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/delete_reoccurring.py
""" Delete Reoccurring Characters Given a string, delete any reoccurring characters and return the new string containing only the first occurrence of each character. Reference: https://en.wikipedia.org/wiki/Duplicate_removal Complexity: Time: O(n) where n is the length of the string Space: O(n) """ from __...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/delete_reoccurring.py", "license": "MIT License", "lines": 27, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/domain_extractor.py
""" Domain Name Extractor Given a URL as a string, parse out just the domain name and return it. Uses only the .split() built-in function without regex or urlparse. Reference: https://en.wikipedia.org/wiki/Domain_name Complexity: Time: O(n) where n is the length of the URL Space: O(n) """ from __future__ i...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/domain_extractor.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/encode_decode.py
""" Encode and Decode Strings Design an algorithm to encode a list of strings to a single string, and decode it back to the original list of strings. Reference: https://leetcode.com/problems/encode-and-decode-strings/ Complexity: Time: O(n) for both encode and decode Space: O(n) """ from __future__ import ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/encode_decode.py", "license": "MIT License", "lines": 42, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/first_unique_char.py
""" First Unique Character in a String Given a string, find the first non-repeating character and return its index. If no unique character exists, return -1. Reference: https://leetcode.com/problems/first-unique-character-in-a-string/ Complexity: Time: O(n^2) worst case due to nested comparisons Space: O(n)...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/first_unique_char.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/fizzbuzz.py
""" FizzBuzz Return an array of numbers from 1 to N, replacing multiples of 3 with 'Fizz', multiples of 5 with 'Buzz', and multiples of both with 'FizzBuzz'. Reference: https://en.wikipedia.org/wiki/Fizz_buzz Complexity: Time: O(n) Space: O(n) """ from __future__ import annotations def fizzbuzz(number: i...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/fizzbuzz.py", "license": "MIT License", "lines": 59, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/group_anagrams.py
""" Group Anagrams Given an array of strings, group anagrams together. Anagrams are words that contain the same letters in a different order. Reference: https://leetcode.com/problems/group-anagrams/ Complexity: Time: O(n * k log k) where n is the number of strings and k is max length Space: O(n * k) """ fr...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/group_anagrams.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/int_to_roman.py
""" Integer to Roman Numeral Given an integer, convert it to a Roman numeral string. Input is guaranteed to be within the range from 1 to 3999. Reference: https://en.wikipedia.org/wiki/Roman_numerals Complexity: Time: O(1) since the input range is bounded Space: O(1) """ from __future__ import annotations ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/int_to_roman.py", "license": "MIT License", "lines": 30, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/is_palindrome.py
""" Is Palindrome Determine if a string is a palindrome, considering only alphanumeric characters and ignoring cases. Multiple approaches are provided. Reference: https://en.wikipedia.org/wiki/Palindrome Complexity: Time: O(n) for all variations Space: O(n) for variations that create new strings, O(1) for t...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/is_palindrome.py", "license": "MIT License", "lines": 112, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/is_rotated.py
""" Is Rotated String Given two strings, determine if the second is a rotated version of the first. Two approaches are provided: concatenation check and brute force. Reference: https://leetcode.com/problems/rotate-string/ Complexity: Time: O(n) for concatenation approach, O(n^2) for brute force Space: O(n) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/is_rotated.py", "license": "MIT License", "lines": 47, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/judge_circle.py
""" Judge Route Circle Given a sequence of robot moves (R, L, U, D), determine whether the robot returns to its starting position after completing all moves. Reference: https://leetcode.com/problems/robot-return-to-origin/ Complexity: Time: O(n) where n is the number of moves Space: O(1) """ from __future_...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/judge_circle.py", "license": "MIT License", "lines": 29, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/knuth_morris_pratt.py
""" Knuth-Morris-Pratt String Search Given two sequences (text and pattern), return the list of start indexes in text that match the pattern using the KMP algorithm. Reference: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm Complexity: Time: O(n + m) where n = len(text), m = len(patt...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/knuth_morris_pratt.py", "license": "MIT License", "lines": 43, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/license_number.py
""" License Key Formatting Given a license key string and a group size k, reformat the key so that each group contains exactly k characters, separated by dashes. Reference: https://leetcode.com/problems/license-key-formatting/ Complexity: Time: O(n) where n is the length of the key Space: O(n) """ from __f...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/license_number.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
keon/algorithms:algorithms/string/longest_common_prefix.py
""" Longest Common Prefix Find the longest common prefix string amongst an array of strings. Three approaches: horizontal scanning, vertical scanning, and divide and conquer. Reference: https://leetcode.com/problems/longest-common-prefix/ Complexity: Time: O(S) where S is the sum of all characters in all string...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/longest_common_prefix.py", "license": "MIT License", "lines": 87, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/longest_palindromic_substring.py
""" Longest Palindromic Substring Given a string, find the longest palindromic substring using Manacher's algorithm, which runs in linear time. Reference: https://en.wikipedia.org/wiki/Longest_palindromic_substring Complexity: Time: O(n) using Manacher's algorithm Space: O(n) """ from __future__ import ann...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/longest_palindromic_substring.py", "license": "MIT License", "lines": 52, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/string/make_sentence.py
""" Make Sentence For a given string and dictionary, count how many sentences can be formed from the string such that all words are contained in the dictionary. Reference: https://en.wikipedia.org/wiki/Word_break_problem Complexity: Time: O(2^n) worst case due to recursive exploration Space: O(n) recursion ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/make_sentence.py", "license": "MIT License", "lines": 33, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/merge_string_checker.py
""" Merge String Checker Determine if a given string can be formed by interleaving two other strings, preserving the character order from each part. Reference: https://leetcode.com/problems/interleaving-string/ Complexity: Time: O(2^n) worst case for recursive, similar for iterative Space: O(n) for recursio...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/merge_string_checker.py", "license": "MIT License", "lines": 56, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/min_distance.py
""" Minimum Edit Distance (Delete Operation) Given two words, find the minimum number of steps required to make them the same, where each step deletes one character from either string. Reference: https://leetcode.com/problems/delete-operation-for-two-strings/ Complexity: Time: O(2^n) for recursive LCS, O(m * n)...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/min_distance.py", "license": "MIT License", "lines": 68, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/multiply_strings.py
""" Multiply Strings Given two non-negative integers represented as strings, return their product as a string without using built-in BigInteger or direct integer conversion. Reference: https://leetcode.com/problems/multiply-strings/ Complexity: Time: O(m * n) where m, n are the lengths of the two numbers Sp...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/multiply_strings.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/one_edit_distance.py
""" One Edit Distance Given two strings, determine if they are exactly one edit distance apart. An edit is an insertion, deletion, or replacement of a single character. Reference: https://leetcode.com/problems/one-edit-distance/ Complexity: Time: O(n) where n is the length of the shorter string Space: O(n) ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/one_edit_distance.py", "license": "MIT License", "lines": 56, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/panagram.py
""" Panagram Checker Check whether a given string is a panagram (a sentence using every letter of the English alphabet at least once). Reference: https://en.wikipedia.org/wiki/Pangram Complexity: Time: O(n) where n is the length of the string Space: O(1) since the letter set is bounded at 26 """ from __fut...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/panagram.py", "license": "MIT License", "lines": 25, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/rabin_karp.py
""" Rabin-Karp String Search A string searching algorithm that uses hashing to find a pattern in text. Uses a rolling hash to efficiently compare the pattern hash with substrings. Reference: https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm Complexity: Time: O(n + m) average, O(n * m) worst case Sp...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/rabin_karp.py", "license": "MIT License", "lines": 65, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/repeat_string.py
""" Repeated String Match Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of the result. Return -1 if impossible. Reference: https://leetcode.com/problems/repeated-string-match/ Complexity: Time: O(n * m) where n = len(A), m = len(B) Space: O(n * (...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/repeat_string.py", "license": "MIT License", "lines": 31, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/repeat_substring.py
""" Repeated Substring Pattern Given a non-empty string, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Reference: https://leetcode.com/problems/repeated-substring-pattern/ Complexity: Time: O(n) for the string containment check Space: O(n...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/repeat_substring.py", "license": "MIT License", "lines": 22, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/reverse_string.py
""" Reverse String Reverse a string using four different approaches: recursive, iterative, pythonic (using reversed), and ultra-pythonic (using slicing). Reference: https://en.wikipedia.org/wiki/String_(computer_science)#Reversal Complexity: Time: O(n) for all approaches Space: O(n) for all approaches, O(lo...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/reverse_string.py", "license": "MIT License", "lines": 67, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/reverse_vowel.py
""" Reverse Vowels of a String Given a string, reverse only the vowels while keeping all other characters in their original positions. Reference: https://leetcode.com/problems/reverse-vowels-of-a-string/ Complexity: Time: O(n) where n is the length of the string Space: O(n) for the character list """ from ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/reverse_vowel.py", "license": "MIT License", "lines": 34, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/reverse_words.py
""" Reverse Words in a String Given a string, reverse the order of words. Leading and trailing spaces are trimmed and words are separated by single spaces. Reference: https://leetcode.com/problems/reverse-words-in-a-string/ Complexity: Time: O(n) where n is the length of the string Space: O(n) """ from __f...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/reverse_words.py", "license": "MIT License", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/roman_to_int.py
""" Roman Numeral to Integer Given a Roman numeral string, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Reference: https://en.wikipedia.org/wiki/Roman_numerals Complexity: Time: O(n) where n is the length of the Roman numeral string Space: O(1) """ from __future__ im...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/roman_to_int.py", "license": "MIT License", "lines": 36, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/rotate.py
""" Rotate String Given a string and an integer k, return the string rotated by k positions to the left. Two approaches are provided. Reference: https://en.wikipedia.org/wiki/Circular_shift Complexity: Time: O(n) where n is the length of the string Space: O(n) """ from __future__ import annotations def r...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/rotate.py", "license": "MIT License", "lines": 39, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/strip_url_params.py
""" Strip URL Parameters Remove duplicate query string parameters from a URL and optionally remove specified parameters. Three approaches of increasing Pythonic style. Reference: https://en.wikipedia.org/wiki/Query_string Complexity: Time: O(n) where n is the length of the URL Space: O(n) """ from __future...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/strip_url_params.py", "license": "MIT License", "lines": 124, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/string/strong_password.py
""" Strong Password Checker Given a password string, determine the minimum number of characters that must be added to make it strong. A strong password has at least 6 characters, a digit, a lowercase letter, an uppercase letter, and a special character. Reference: https://www.hackerrank.com/challenges/strong-password...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/strong_password.py", "license": "MIT License", "lines": 32, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/text_justification.py
""" Text Justification Given an array of words and a max width, format the text such that each line has exactly max_width characters and is fully justified. Extra spaces are distributed as evenly as possible with left slots getting more. Reference: https://leetcode.com/problems/text-justification/ Complexity: Ti...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/text_justification.py", "license": "MIT License", "lines": 71, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keon/algorithms:algorithms/string/unique_morse.py
""" Unique Morse Code Representations Given a list of words, determine the number of unique Morse code transformations among all the words. Reference: https://leetcode.com/problems/unique-morse-code-words/ Complexity: Time: O(n * k) where n is the number of words, k is average word length Space: O(n) """ f...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/unique_morse.py", "license": "MIT License", "lines": 69, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple
keon/algorithms:algorithms/string/validate_coordinates.py
""" Validate Coordinates Validate if given parameters are valid geographical coordinates. Latitude must be between -90 and 90, longitude between -180 and 180. Reference: https://en.wikipedia.org/wiki/Geographic_coordinate_system Complexity: Time: O(n) where n is the length of the coordinate string Space: O(...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/validate_coordinates.py", "license": "MIT License", "lines": 66, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/string/word_squares.py
""" Word Squares Given a set of words (without duplicates), find all word squares that can be built from them. A word square reads the same horizontally and vertically. Reference: https://leetcode.com/problems/word-squares/ Complexity: Time: O(n * 26^L) where n is the number of words, L is word length Space...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/string/word_squares.py", "license": "MIT License", "lines": 39, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:tests/test_backtracking.py
import unittest from algorithms.backtracking import ( add_operators, anagram, array_sum_combinations, combination_sum, find_words, generate_abbreviations, generate_parenthesis_v1, generate_parenthesis_v2, get_factors, letter_combinations, palindromic_substrings, pattern_...
{ "repo_id": "keon/algorithms", "file_path": "tests/test_backtracking.py", "license": "MIT License", "lines": 434, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
keon/algorithms:algorithms/graph/a_star.py
""" A* (A-star) Search Algorithm Finds the shortest path in a weighted graph using a heuristic function. Reference: https://en.wikipedia.org/wiki/A*_search_algorithm Complexity: Time: O(E log V) with a binary heap Space: O(V) """ from __future__ import annotations import heapq from collections.abc import ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/a_star.py", "license": "MIT License", "lines": 47, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/graph/kahns_algorithm.py
""" Kahn's Algorithm (Topological Sort via BFS) Computes a topological ordering of a directed acyclic graph using an in-degree based BFS approach. Reference: https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm Complexity: Time: O(V + E) Space: O(V) """ from __future__ import annotations fro...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/graph/kahns_algorithm.py", "license": "MIT License", "lines": 45, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keon/algorithms:algorithms/greedy/gale_shapley.py
""" Gale-Shapley Stable Matching Solves the stable matching (stable marriage) problem. Given N men and N women with ranked preferences, produces a stable matching where no pair would prefer each other over their current partners. Reference: https://en.wikipedia.org/wiki/Gale%E2%80%93Shapley_algorithm Complexity: ...
{ "repo_id": "keon/algorithms", "file_path": "algorithms/greedy/gale_shapley.py", "license": "MIT License", "lines": 46, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
documentation
keras-team/keras:keras/src/export/saved_model_export_archive.py
"""Base class for SavedModel export archive.""" from keras.src import backend from keras.src import layers from keras.src import tree from keras.src.export.export_utils import make_tf_tensor_spec from keras.src.utils.module_utils import tensorflow as tf class SavedModelExportArchive: """Base class for SavedModel...
{ "repo_id": "keras-team/keras", "file_path": "keras/src/export/saved_model_export_archive.py", "license": "Apache License 2.0", "lines": 338, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keras-team/keras:integration_tests/pytorch_export_test.py
""" Integration tests for PyTorch model export with dynamic shapes. Tests the complete fix for GitHub issue #22102 where models with AveragePooling2D → Conv2D → Reshape failed to export with dynamic shapes. The fixes enable: 1. torch.export with dynamic shapes 2. ONNX export with dynamic shapes 3. TorchScript tracing...
{ "repo_id": "keras-team/keras", "file_path": "integration_tests/pytorch_export_test.py", "license": "Apache License 2.0", "lines": 253, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
keras-team/keras:keras/src/optimizers/schedule_free_adamw.py
from keras.src import ops from keras.src.api_export import keras_export from keras.src.optimizers import optimizer @keras_export(["keras.optimizers.ScheduleFreeAdamW"]) class ScheduleFreeAdamW(optimizer.Optimizer): """Optimizer that implements the Schedule-Free AdamW algorithm. Schedule-Free learning is a me...
{ "repo_id": "keras-team/keras", "file_path": "keras/src/optimizers/schedule_free_adamw.py", "license": "Apache License 2.0", "lines": 165, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_complex
keras-team/keras:keras/src/optimizers/schedule_free_adamw_test.py
import numpy as np import pytest import keras from keras.src import backend from keras.src import ops from keras.src import testing from keras.src.optimizers.schedule_free_adamw import ScheduleFreeAdamW class ScheduleFreeAdamWTest(testing.TestCase): def test_config(self): optimizer = ScheduleFreeAdamW( ...
{ "repo_id": "keras-team/keras", "file_path": "keras/src/optimizers/schedule_free_adamw_test.py", "license": "Apache License 2.0", "lines": 90, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
keras-team/keras:benchmarks/layer_benchmark/random_rotation_benchmark.py
"""Benchmark RandomRotation layer.""" from absl import app from absl import flags from benchmarks.layer_benchmark.base_benchmark import LayerBenchmark FLAGS = flags.FLAGS def benchmark_random_rotation( num_samples, batch_size, jit_compile=True, ): layer_name = "RandomRotation" init_args = {"fac...
{ "repo_id": "keras-team/keras", "file_path": "benchmarks/layer_benchmark/random_rotation_benchmark.py", "license": "Apache License 2.0", "lines": 48, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
function_simple