def pivot2(arr,start=0,end=len(arr)):
pivot=arr[start]
swapid=start
for x in range(start+1,end):
if(pivot>arr[x]):
swapid +=1
swap(arr,swapid,x)
print(arr)
swap(arr,start,swapid)
return swapid
def swap(arr,i,j):
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
In pivot2
function I get an error that arr name not defined
and when I remove end=len(arr)
from the arguments the function works, why can't I pass len(arr)
as an argument in Python?
because you are passing arr as an argument and it isn't a variable python will try find that variable raising that error
to fix this instead of putting len(arr) in the parameters put it inside the actual function
def pivot2(arr,start=0):
end=len(arr)
pivot=arr[start]
swapid=start
for x in range(start+1,end):
if(pivot>arr[x]):
swapid +=1
swap(arr,swapid,x)
print(arr)
swap(arr,start,swapid)
return swapid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.