Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Code Incorrectness #10

Open
Suhavi opened this issue May 14, 2024 · 2 comments
Open

Python Code Incorrectness #10

Suhavi opened this issue May 14, 2024 · 2 comments

Comments

@Suhavi
Copy link

Suhavi commented May 14, 2024

an example - solution for max sum subarray, chapter 2

O(n^3) solution

best = 0
for i in arr:
for j in arr:
sum = 0
for k in arr:
sum += k
best = max(best, sum)
print(best)

real code -

best = 0
for i in range(0,len(arr)):
for j in range(0,len(arr)):
sum = 0
for k i range(i,j):
sum += k
best = max(best, sum

print(best)

@Suhavi
Copy link
Author

Suhavi commented May 14, 2024

incorrect binary search too -
def binary_search(arr, elem, prim_index=0):
mid = int(len(arr) / 2)
mid_elem = arr[mid]
if mid_elem == elem:
return mid + prim_index
if mid_elem > elem:
return binary_search(arr[:mid], elem, prim_index)
if mid_elem < elem:
return binary_search(arr[mid:], elem, prim_index + mid)

absent - iterative method

@abhishekgahlot
Copy link
Owner

Thanks for suggesting, could you raise a pr ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants