mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-07 11:37:36 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
"""
|
||||
author : Mayank Kumar Jha (mk9440)
|
||||
"""
|
||||
from __future__ import print_function
|
||||
from typing import List
|
||||
import time
|
||||
import matplotlib.pyplot as plt
|
||||
@ -10,7 +9,7 @@ def find_max_sub_array(A,low,high):
|
||||
if low==high:
|
||||
return low,high,A[low]
|
||||
else :
|
||||
mid=(low+high)//2
|
||||
mid=(low+high)//2
|
||||
left_low,left_high,left_sum=find_max_sub_array(A,low,mid)
|
||||
right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high)
|
||||
cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high)
|
||||
@ -30,7 +29,7 @@ def find_max_cross_sum(A,low,mid,high):
|
||||
if summ > left_sum:
|
||||
left_sum=summ
|
||||
max_left=i
|
||||
summ=0
|
||||
summ=0
|
||||
for i in range(mid+1,high+1):
|
||||
summ+=A[i]
|
||||
if summ > right_sum:
|
||||
@ -40,7 +39,7 @@ def find_max_cross_sum(A,low,mid,high):
|
||||
|
||||
def max_sub_array(nums: List[int]) -> int:
|
||||
"""
|
||||
Finds the contiguous subarray (can be empty array)
|
||||
Finds the contiguous subarray (can be empty array)
|
||||
which has the largest sum and return its sum.
|
||||
|
||||
>>> max_sub_array([-2,1,-3,4,-1,2,1,-5,4])
|
||||
@ -50,14 +49,14 @@ def max_sub_array(nums: List[int]) -> int:
|
||||
>>> max_sub_array([-1,-2,-3])
|
||||
0
|
||||
"""
|
||||
best = 0
|
||||
current = 0
|
||||
for i in nums:
|
||||
current += i
|
||||
best = 0
|
||||
current = 0
|
||||
for i in nums:
|
||||
current += i
|
||||
if current < 0:
|
||||
current = 0
|
||||
best = max(best, current)
|
||||
return best
|
||||
return best
|
||||
|
||||
if __name__=='__main__':
|
||||
inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000]
|
||||
@ -68,8 +67,8 @@ if __name__=='__main__':
|
||||
(find_max_sub_array(li,0,len(li)-1))
|
||||
end=time.time()
|
||||
tim.append(end-strt)
|
||||
print("No of Inputs Time Taken")
|
||||
for i in range(len(inputs)):
|
||||
print("No of Inputs Time Taken")
|
||||
for i in range(len(inputs)):
|
||||
print(inputs[i],'\t\t',tim[i])
|
||||
plt.plot(inputs,tim)
|
||||
plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ")
|
||||
@ -77,4 +76,4 @@ if __name__=='__main__':
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user