Python algorithms practice

Mia 27 Published: 09/20/2024

Python algorithms practice

Here's a problem that I'll solve using Python:

Problem: Write an algorithm to find the maximum sum of a subarray within an array.

Let's take the following example input: [-2, 1, -3, 4, -1, 2, 1, -5, 4].

Algorithm:

Here's my approach:

Initialize two variables: max_sum to store the maximum sum and current_sum to store the current sum. Iterate through each element in the array. For each element, add it to current_sum. If this result is greater than max_sum, update max_sum. Keep track of whether we're inside a positive or negative streak by using a separate variable: inside_negative_streak (initially set to False). If the current sum becomes negative while we're not inside a negative streak, reset it to 0 and mark that we are now inside a negative streak. If the array ends, return the maximum sum found.

Code:

def max_subarray_sum(arr):

if not arr:

return 0

max_sum = current_sum = arr[0]

inside_negative_streak = False

for num in arr[1:]:

if num > 0 or (num < 0 and inside_negative_streak):

current_sum += num

else:

current_sum = num

inside_negative_streak = True

max_sum = max(max_sum, current_sum)

return max_sum

print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]))

Output: 6

In this algorithm, we start with the first element. If the sum becomes negative while we're not inside a negative streak, we reset it to 0 and mark that we are now inside a negative streak. This way, we ensure that our maximum subarray sum is found correctly.

What are the algorithms in Python?

I'm happy to help you with that!

Python has a wide range of algorithms implemented in its standard library, as well as through popular libraries like NumPy and SciPy. Here's a non-exhaustive list of some commonly used algorithms in Python:

Sorting Algorithms: Bubble Sort: sorted() function QuickSort: list.sort() method (not directly accessible but used internally) Merge Sort: Not implemented as-is, but can be achieved using the sorted() function or a custom implementation

Searching Algorithms:

Linear Search: Implementable through iterating over a list and checking for matches Binary Search: Implementable through a custom implementation or using libraries like SciPy's searchsorted() Graph Algorithms: BFS (Breadth-First Search): Implemented in networkx library as networkx.algorithms.bfs DFS (Depth-First Search): Implemented in networkx library as networkx.algorithms.dfs Dynamic Programming: Fibonacci Series: Implementable through a recursive function or using the itertools library Matrix Algorithms: Matrix Multiplication: Implemented in NumPy's np.dot() function Determinant Calculation: Implemented in SciPy's scipy.linalg.det() function String Algorithms: Rabin-Karp Algorithm (Pattern Searching): Implementable through a custom implementation or using libraries like SciPy's scipy.signal Number Theory Algorithms: Prime Number Generation: Implementable through a custom implementation or using libraries like NumPy's np.random.primerange()

Some notable Python libraries that provide various algorithms are:

NumPy (Numerical Computing) SciPy (Scientific Computing) NetworkX (Network Analysis) Pandas (Data Manipulation and Analysis) Scikit-learn (Machine Learning)

Keep in mind that this is not an exhaustive list, as there are countless algorithms implemented within Python's libraries or through custom implementations by developers.