Python Forum

Full Version: What's wrong
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What's wrong with this code?
n = int(input())
m = [int(i) for i in input().split()]
l = []
for i in range(n):
    if m[i] != -1:
        l.append(m[i])
l.sort()
for i in range(n):
    if(m[i] != -1) and m[i] in l: 
        m[i] = l[0]
        del l[0]
for i in range(n):
    print(m[i], end = ' ')
It's supposed to rearrange the inputs the increasing manner, except for -1's.

My input:
4
949 -1 3957 949
My output:
949 -1 949 949
How did that happen?
First of all, learn to use meaningful names, single letter are meaningless to another programmer.
Next, Add text to input statements indicating what is expected.
example:
n = int(input('Enter number of items: ')
m = [int(i) for i in input('Enter the items : ').split()]
First statement:
n = int(input())
is not needed, and no need to terminate with -1 use instead:
for i in range(len(m)):
this code:
for i in range(n):
    if m[i] != -1:
        l.append(m[i])
is totally unnecessary, you already have the values in m
finally, you show your input, and actual output, but not your desired output which is important