Q. Write a program that inputs a sequence A of numbers and creates another sequence B of number of same size as per following guidelines:

Ask the desired order of sorting.

Place first element of sequence A into sequence B and then take each element in the sequence A:


Answer =

A = eval(input("Enter a list :- "))
user = input("Enter order you want (Asc/Dsc):- ")
B = [ ]
for i in range(len(A)):
    for j in range (len(A) - 1):
        if user == "Asc" or user == "asc":
            if A [ j ]  > A [ j + 1 ] :
                A [ j ] , A [ j + 1 ] = A [ j + 1 ] , A [ j ]
        elif user == "Dsc" or user == "dsc":
            if A [ j ]  < A [ j + 1 ] :
                A [ j ] , A [ j + 1 ] = A [ j + 1 ] , A [ j ]

for i in A :
    B += [ i ]

print(B)


Output :-

Enter a list :- [2,1,8,7,6,4,3,9,5]
Enter order you want (Asc/Dsc):- Asc
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>>

Enter a list :- [2,1,8,7,6,4,3,9,5]
Enter order you want (Asc/Dsc):- dsc
[9, 8, 7, 6, 5, 4, 3, 2, 1]

>>> 

Enter a list :- [7,8,6,4,2,9,5,1,3,4,7,8,9,5,6,4,1,2,3]
Enter order you want (Asc/Dsc):- Asc
[1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]

>>>


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post