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.
enumerate returns index as well as value,here he is creating an index list where values are indexes of first list and its indexes are old lists values. in his code yo can see a line "pos +=1" its actually not required.code will even work without this ,
In yor code you are using index function ,the problem with index function is if you having an array like this list1=[1,2,3,2,4] and you use a for loop to print indexs of list1 like this. < for i in list1:
print(list1.index(i))
>
the index list should be [0,1,2,1,4] here the function performing leniar search kind process.so it record only first occurance.that is the problem.
here in his code he created a index list.and he take it as a reference,and along with each swap the index list is also update. in your code try to make a index list,it will help you
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
enumerate returns index as well as value,here he is creating an index list where values are indexes of first list and its indexes are old lists values. in his code yo can see a line "pos +=1" its actually not required.code will even work without this , In yor code you are using index function ,the problem with index function is if you having an array like this list1=[1,2,3,2,4] and you use a for loop to print indexs of list1 like this. < for i in list1: print(list1.index(i)) > the index list should be [0,1,2,1,4] here the function performing leniar search kind process.so it record only first occurance.that is the problem.