Skip to content

Commit

Permalink
added new algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanExtreme002 committed May 2, 2024
1 parent 00b6da0 commit a609c4a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fastsnake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A Python Helper CLI for Competitive Programming
"""

__version__ = "1.4.18"
__version__ = "1.4.19"

__author__ = "Jean Loui Bernard Silva de Jesus"
__github__ = "https://github.com/JeanExtreme002"
Expand Down
9 changes: 9 additions & 0 deletions fastsnake/algorithms/gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def gcd(a: int, b: int) -> int:
"""
Euclidean Algorithm for Greatest Common Divisor (GCD)
Complexity: O(log(min(a, b)))
"""
if a == 0:
return b
return gcd(b % a, a)
2 changes: 1 addition & 1 deletion fastsnake/algorithms/knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Tempo: O(N * capacity)
# Espaço: O(capacity)

def knapsack(profit, weight, capacity, n = None):
def knapsack(profit: list, weight: list, capacity: int, n = None):
"""
Returns the maximum value that can be put in a knapsack of capacity W.
Expand Down
26 changes: 26 additions & 0 deletions fastsnake/algorithms/largest_sub_array_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Python program to find maximum contiguous subarray

from math import inf
from typing import Callable, Optional

maxint = inf


def max_sub_array_sum(array):
"""
Find the sum of contiguous subarray within a one-dimensional
array of numbers that has the largest sum.
"""
max_so_far = -maxint - 1
max_ending_here = 0

for i in range(len(array)):
max_ending_here = max_ending_here + array[i]

if (max_so_far < max_ending_here):
max_so_far = max_ending_here

if max_ending_here < 0:
max_ending_here = 0

return max_so_far

0 comments on commit a609c4a

Please sign in to comment.