0

'RE-ARRANGEMENT' :What is the solution of this coding in python3.6?? I want to know urgent...

Given a list A of elements of length N, ranging from 1 to N. All elements may not be present in the array. If the element is not present then there will be -1 present in the array. Rearrange the array such that A[i] = i and if i is not present display -1 at that place. Input Format: The first line contains n numbers with each number separated by a space. Output Format: Print the elements of the list after the modification. Example: Input: -1 -1 6 1 9 3 2 -1 4 -1 Output: -1 1 2 3 4 -1 6 -1 -1 9 Explanation: The modified list contains elements such that A[i] = i.

4th Oct 2018, 8:53 PM
shivam Kumar
shivam Kumar - avatar
3 Answers
0
Please, post your try for get help on it... Dont expect that someone make YOUR assignement
4th Oct 2018, 9:31 PM
KrOW
KrOW - avatar
0
num = [int(x) for x in input().split()] ​ # Sorting list of Integers num.sort() print( " ".join( repr(e) for e in num),end=" " ) Now please help me to solve my coding completely...
4th Oct 2018, 9:56 PM
shivam Kumar
shivam Kumar - avatar
0
shivam Kumar You are sure that input have number from 1 to N? Seem 0 to N-1 to me... Anyway, after sorted the input you will have at start all -1 that represent the elements not presents in input... Because they are sorted is clear that you will compare next int from previous and see if they distance is greater than one. If its, then you have to fill with -1s .. Getting your example, your ordered input is: [-1,-1,-1,-1,1,2,3,4,6,9] Start from first valid number (then != -1) thats 1: 1) see the next number (2 in this case) 2) if they difference is one then no problem and put in output 3) This is true while you dont reach 4 because the next item to it is 6 thats is not distance 1 from 4 (6-4=2) then you have to fill all "middle" not present numbers with -1 in output (in this case you have to replace only for 5) You have to do this for all items and obliviously you have to handle also first valid item (what if it 2 and not 1?) and last item
5th Oct 2018, 7:50 AM
KrOW
KrOW - avatar