Python problem on end sorting | SoloLearn: Learn to code for FREE!

+3

# Python problem on end sorting

Can someone please help me understand the following question? I have uploaded my code. but it's showing errors. thanx Given a list A of N distinct integer numbers, you can sort the list by moving an element to the end of the list. Find the minimum number of moves required to sort the list using this method in ascending order.  Input Format: The first line of the input contains N distinct integers of list A separated by a space. Output Format Print the minimum number of moves required to sort the elements. Example: Input: 1 3 2 4 5 Output: 3 Explanation: In the first move, we move 3 to the end of the list. In the second move, we move 4 to the end of the list, and finally, in the third movement, we move 5 to the end. https://code.sololearn.com/cJfWwD08Qb9e/?ref=app

3/7/2019 2:27:19 PM

Nilutpol Kashyap

+4

You are not using l1 only l so your while loop is infinite because l will always have elements. Your algorithm doesn't match your requirements. https://code.sololearn.com/cm8Sj0xwOUyx

+3

https://code.sololearn.com/cToLaSvlUbf2/?ref=app

+1

John Wells sir can you please guide me??

+1

First up, your while loop is infinite. while l: means loop while the (in this case) list l is not empty. Since l is never empty, it never stops looping. Secondly, the algorithm in your code is incorrect. Since all elements are compared to your first element, and moved to the end if they are less than your first element, there is no way for the first element to be moved to the end if it is not the smallest. So that will also create an endless loop.

+1

John Wells thanks a lot sir

+1

Anna thanx a lot mam