We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'lilysHomework' function below.## The function is expected to return an INTEGER.# The function accepts INTEGER_ARRAY arr as parameter.#defcountSwaps(arr,r):arr=arr.copy()target=sorted(arr,reverse=r)inds={v:ifori,vinenumerate(target)}i=0count=0whilei<len(arr)andarr!=target:whilearr[i]!=target[i]:ind=inds[arr[i]]arr[i],arr[ind]=arr[ind],arr[i]count+=1i+=1returncountdeflilysHomework(arr):#Write your code herereturnmin(countSwaps(arr,False),countSwaps(arr,True))if__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')n=int(input().strip())arr=list(map(int,input().rstrip().split()))result=lilysHomework(arr)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →
Solution in Python 3