[pull] master from keon:master by pull[bot] · Pull Request #42 · jaydave/algorithms · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6fb00bc
Create python-app.yml
ankit167 Feb 15, 2022
c12305c
Update pytest command. (#831)
aalekhpatel07 Feb 16, 2022
a310940
Bug fix: Add None checks for the boundary values. (#832)
aalekhpatel07 Feb 16, 2022
6c54611
Added testing for the fizzbuzz algorithm. (#833)
ArnPellesGit Feb 24, 2022
617d2d5
Fix broken link in wiki (fenwick tree) (#790)
aretecode Feb 25, 2022
87eae9a
Fix flake8 issues (#836)
ankit167 Mar 2, 2022
8149be5
[Documentation] knuth_morris_pratt (#835)
peendebak Mar 2, 2022
15e5292
Fix flake8 issues (#837)
ankit167 Mar 2, 2022
3259a07
Pylint refactor (#851)
nolanderc Mar 7, 2022
6440664
Added Kadane algorithm for max_contiguous_subsequence_sum problem wit…
iabhishek15 Mar 8, 2022
451614b
added test case for test_limit (#819)
Bogyie Mar 16, 2022
5f1a786
Add num_perfect_squares #767 (#848)
mantaur Mar 20, 2022
7d70e39
Fix issue probably-meant-fstring found at https://codereview.doctor …
code-review-doctor Jun 14, 2022
980d953
Fix test_backtrack.py (#865)
Brknyi Jun 14, 2022
8aa29d7
fix: removed extra blank line between sections (#870)
hanessn1 Jul 6, 2022
e63bc4d
"delete_fixup" optimization (#873)
izveigor Jul 27, 2022
fac67d6
docs: fix simple typo, occured -> occurred (#869)
timgates42 Jul 29, 2022
52b9408
fixed typo. (#878)
patilabhay679 Nov 12, 2022
2a129ba
Fixed typos in two files (#898)
joseph-alan-jose Mar 8, 2023
fd86fd1
modified the return from both main and helper. (#886)
patilabhay679 Mar 8, 2023
e24247f
Fix search_range and add test (#868)
mojjominion Mar 8, 2023
51f93c6
add FFT and tests (#847)
ekorre1001 Mar 8, 2023
c0e5404
feat: add dynamic programming algorithm to strings.min_distance.py (#…
psalqvist Mar 8, 2023
a336ee8
Update binary_search.py (#902)
Anujeet23 Apr 4, 2023
1117ffe
Optimize longest_non_repeat.py (#914)
PIYUSH-GOSWAMI Feb 5, 2024
40c944c
Update summarize_ranges.py (#912)
Zhanccg Feb 5, 2024
e9c28af
Add Kosaraju algorithm (#910)
rubalsxngh Feb 5, 2024
cad4754
Add remove duplicates (#905)
oDqnger Feb 5, 2024
66eb36d
Fixed polynomial division (#2130)
Canderton74 Jun 27, 2025
0b04e60
Update test_array.py (missing comma at line 12) (#2047)
cpatel321 Jun 27, 2025
486fa37
Added Bead Sort Algorithm #920 (#2100)
shirleymaza Jun 27, 2025
67287d2
Added the validate bst function (#2696)
PiyushGoel0612 Oct 9, 2025
5b63e90
Added Gale-Shapley algorithm with test cases (#2013)
Carlo-Fr Oct 9, 2025
5991e05
Optimize remove_duplicates from O(n²) to O(n) time complexity (#2700)
devin-ai-integration[bot] Nov 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/python-app.yml
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ agree to abide by the [Code of Conduct](CODE_OF_CONDUCT.md).

* After that create a branch for your changes. For example:
* add_XXX if you will add new algorithms or data structures.
* fix_XXX if you will fixe a bug on a certain algorithm or data structure.
* fix_XXX if you will fix a bug on a certain algorithm or data structure.
* test_XXX if you wrote a test/s.
* doc_XXX if you added to or edited documentation.

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ If you want to uninstall algorithms, it is as simple as:
- [merge_intervals](algorithms/arrays/merge_intervals.py)
- [missing_ranges](algorithms/arrays/missing_ranges.py)
- [plus_one](algorithms/arrays/plus_one.py)
- [remove_duplicates](algorithms/arrays/remove_duplicates.py)
- [rotate](algorithms/arrays/rotate.py)
- [summarize_ranges](algorithms/arrays/summarize_ranges.py)
- [three_sum](algorithms/arrays/three_sum.py)
Expand All @@ -71,6 +72,8 @@ If you want to uninstall algorithms, it is as simple as:
- [two_sum](algorithms/arrays/two_sum.py)
- [move_zeros](algorithms/arrays/move_zeros.py)
- [n_sum](algorithms/arrays/n_sum.py)
- [greedy](algorithms/greedy/)
- [max_contiguous_subsequence_sum](algorithms/greedy/max_contiguous_subsequence_sum.py)
- [automata](algorithms/automata)
- [DFA](algorithms/automata/dfa.py)
- [backtrack](algorithms/backtrack)
Expand Down Expand Up @@ -223,6 +226,7 @@ If you want to uninstall algorithms, it is as simple as:
- [next_bigger](algorithms/maths/next_bigger.py)
- [next_perfect_square](algorithms/maths/next_perfect_square.py)
- [nth_digit](algorithms/maths/nth_digit.py)
- [num_perfect_squares](algorithms/maths/num_perfect_squares.py)
- [polynomial](algorithms/maths/polynomial.py)
- [power](algorithms/maths/power.py)
- [prime_check](algorithms/maths/prime_check.py)
Expand Down Expand Up @@ -370,7 +374,6 @@ If you want to uninstall algorithms, it is as simple as:
- [count_left_node](algorithms/tree/bst/count_left_node.py)
- [num_empty](algorithms/tree/bst/num_empty.py)
- [height](algorithms/tree/bst/height.py)
- [fenwick_tree](algorithms/tree/fenwick_tree]
- [fenwick_tree](algorithms/tree/fenwick_tree/fenwick_tree.py)
- [red_black_tree](algorithms/tree/red_black_tree)
- [red_black_tree](algorithms/tree/red_black_tree/red_black_tree.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/arrays/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
from .two_sum import *
from .limit import *
from .n_sum import *
from .remove_duplicates import *
1 change: 0 additions & 1 deletion algorithms/arrays/josephus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Output: 369485271
"""


def josephus(int_list, skip):
skip = skip - 1 # list starts with 0 index
idx = 0
Expand Down
8 changes: 8 additions & 0 deletions algorithms/arrays/limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@

# tl:dr -- array slicing by value
def limit(arr, min_lim=None, max_lim=None):
if len(arr) == 0:
return arr

if min_lim is None:
min_lim = min(arr)
if max_lim is None:
max_lim = max(arr)

return list(filter(lambda x: (min_lim <= x <= max_lim), arr))
20 changes: 19 additions & 1 deletion algorithms/arrays/longest_non_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,22 @@ def get_longest_non_repeat_v2(string):
max_len = index - start + 1
sub_string = string[start: index + 1]
used_char[char] = index
return max_len, sub_string
return max_len, sub_string

def get_longest_non_repeat_v3(string):
"""
Find the length of the longest substring
without repeating characters.
Uses window sliding approach.
Return max_len and the substring as a tuple
"""
longest_substring = ''
seen = set()
start_idx = 0
for i in range(len(string)):
while string[i] in seen:
seen.remove(string[start_idx])
start_idx += 1
seen.add(string[i])
longest_substring = max(longest_substring, string[start_idx: i+1], key=len)
return len(longest_substring), longest_substring
30 changes: 30 additions & 0 deletions algorithms/arrays/remove_duplicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
This algorithm removes any duplicates from an array and returns a new array with those duplicates
removed.

For example:

Input: [1, 1 ,1 ,2 ,2 ,3 ,4 ,4 ,"hey", "hey", "hello", True, True]
Output: [1, 2, 3, 4, 'hey', 'hello']

Time Complexity: O(n) for hashable items, O(n²) worst case for unhashable items
Space Complexity: O(n) for the seen set and result array
"""

from collections.abc import Hashable


def remove_duplicates(array):
seen = set()
new_array = []

for item in array:
if isinstance(item, Hashable):
if item not in seen:
seen.add(item)
new_array.append(item)
else:
if item not in new_array:
new_array.append(item)

return new_array
29 changes: 14 additions & 15 deletions algorithms/arrays/summarize_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
For example, given [0, 1, 2, 4, 5, 7], return [(0, 2), (4, 5), (7, 7)].
"""

from typing import List, Tuple

def summarize_ranges(array):
"""
:type array: List[int]
:rtype: List[]
"""

def summarize_ranges(array: List[int]) -> List[Tuple[int, ...]]:
res = []
if len(array) == 0:
return []
if len(array) == 1:
return [str(array[0])]
i = 0
while i < len(array):
num = array[i]
while i + 1 < len(array) and array[i + 1] - array[i] == 1:
i += 1
if array[i] != num:
res.append((num, array[i]))
return [(array[0], array[0])]
it = iter(array)
start = end = next(it)
for num in it:
if num - end == 1:
end = num
else:
res.append((num, num))
i += 1
res.append((start, end))
start = end = num
res.append((start, end))
return res
26 changes: 15 additions & 11 deletions algorithms/compression/elias.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
"""
Elias γ code or Elias gamma code is a universal code encoding positive integers.
It is used most commonly when coding integers whose upper-bound cannot be determined beforehand.
Elias δ code or Elias delta code is a universal code encoding the positive integers,
Elias γ code or Elias gamma code is a universal code
encoding positive integers.
It is used most commonly when coding integers whose
upper-bound cannot be determined beforehand.
Elias δ code or Elias delta code is a universal code
encoding the positive integers,
that includes Elias γ code when calculating.
Both were developed by Peter Elias.

"""
from math import log,ceil
from math import log

log2 = lambda x: log(x,2)
log2 = lambda x: log(x, 2)

# Calculates the binary number
def binary(x,l=1):
def binary(x, l=1):
fmt = '{0:0%db}' % l
return fmt.format(x)

Expand All @@ -21,20 +24,21 @@ def unary(x):

def elias_generic(lencoding, x):
"""
The compressed data is calculated in two parts.
The first part is the unary number of 1 + ⌊log2(x)⌋.
The compressed data is calculated in two parts.
The first part is the unary number of 1 + ⌊log2(x)⌋.
The second part is the binary number of x - 2^(⌊log2(x)⌋).
For the final result we add these two parts.
"""
if x == 0: return '0'
if x == 0:
return '0'

first_part = 1 + int(log2(x))

a = x - 2**(int(log2(x)))

k = int(log2(x))

return lencoding(first_part) + binary(a,k)
return lencoding(first_part) + binary(a, k)

def elias_gamma(x):
"""
Expand All @@ -46,4 +50,4 @@ def elias_delta(x):
"""
For the first part we put the elias_g of the number.
"""
return elias_generic(elias_gamma,x)
return elias_generic(elias_gamma, x)
2 changes: 1 addition & 1 deletion algorithms/dp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
from .word_break import *
from .int_divide import *
from .k_factor import *
from .planting_trees import *
from .planting_trees import *
24 changes: 14 additions & 10 deletions algorithms/dp/climbing_stairs.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
"""
You are climbing a stair case.
It takes n steps to reach to the top.
It takes `steps` number of steps to reach to the top.

Each time you can either climb 1 or 2 steps.
In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.
Note: Given argument `steps` will be a positive integer.
"""


# O(n) space

def climb_stairs(n):
def climb_stairs(steps):
"""
:type n: int
:type steps: int
:rtype: int
"""
arr = [1, 1]
for _ in range(1, n):
for _ in range(1, steps):
arr.append(arr[-1] + arr[-2])
return arr[-1]


# the above function can be optimized as:
# O(1) space

def climb_stairs_optimized(n):
a = b = 1
for _ in range(n):
a, b = b, a + b
return a
def climb_stairs_optimized(steps):
"""
:type steps: int
:rtype: int
"""
a_steps = b_steps = 1
for _ in range(steps):
a_steps, b_steps = b_steps, a_steps + b_steps
return a_steps
38 changes: 22 additions & 16 deletions algorithms/dp/coin_change.py
Loading