Home > other >  how to write it in efficient way
how to write it in efficient way

Time:09-19

Accept four integers as input and write a program to print these integers in non-decreasing order.

The input will be four integers in four lines. The output should be a single line with all the integers separated by a single space in non-decreasing order.

Note: There is no space after the fourth integer.

a = int(input())
b = int(input())
c = int(input())
d = int(input())

if (a<=b and a<=c and a<=d):
    if (b<=c and b<=d):
        if(c<=d):
            print(a,b,c,d)
        else:
            print(a,b,d,c)
    elif(c<=b and c<=d):
        if(b<=d):
            print(a,c,b,d)
        else:
            print(a,c,d,b)
    else:
        if(b<=c):
            print(a,d,b,c)
        else:
            print(a,d,c,b)
elif(b<=a and b<=c and b<=d):
    if(a<=c and a<=d):
        if(c<=d):
            print(b,a,c,d)
        else:
            print(b,a,d,c)
    elif(c<=a and c<=d):
        if(a<=d):
            print(b,c,a,d)
        else:
            print(b,c,d,a)
    else:
        if(a<=c):
            print(b,d,a,c)
        else:
            print(b,d,c,a)
elif(c<=a and c<=b and c<=d):
    if(a<=b and a<=d):
        if(a<=d):
            print(c,a,b,d)
        else:
            print(c,a,d,b)
    elif(b<=a and b<=d):
        if(a<=d):
            print(c,b,a,d)
        else:
            print(c,b,d,a)
    else:
        if(a<=b):
            print(c,d,a,b)
        else:
            print(c,d,b,a)
else:
    if(a<=b and a<=c):
        if(b<=c):
            print(d,a.b,c)
        else:
            print(d,a,c,b)
    elif(b<=a and b<=c):
        if(a<=c):
            print(d,b,a,c)
        else:
            print(d,b,c,a)
    else:
        if(a<=b):
            print(d,c,a,b)
        else:
            print(d,c,b,a)

CodePudding user response:

print(" ".join(str(i) for i in sorted([a, b, c, d])))

or

print(" ".join(map(str, sorted([a, b, c, d]))))

CodePudding user response:

if you input in one line space seperated integers

sorted(list(map(int, input().split())))

if you input each integer in new line

sorted([int(input()) for _ in range(4)])

CodePudding user response:

print("Enter the numbers separated by comma ','")
numbers=input() #getting the numbers in single line
numbers=numbers.split(",") #seprating the numbers
numbers=map(int,numbers) #getting the integer values
numbers=list(numbers) #converting to list
numbers.sort() #sorting the number in increasing order
print(numbers)

you can also use this:

print("Enter the numbers separated by space")
numbers=input() #getting the numbers in single line
numbers=numbers.split() #seprating the numbers
numbers=map(int,numbers) #getting the integer values
numbers=list(numbers) #converting to list
numbers.sort() #sorting the number in increasing order
print(numbers)
  • Related