KnowledgeBoat Logo

Computer Science

Modify your previous code so that SqLst stores the doubled numbers in ascending order.

Python

Linear Lists

1 Like

Answer

NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = []
for num in NumLst:
    SqLst.append(num * 2)

n = len(SqLst)
for i in range(n):
    for j in range(0, n-i-1):
        if SqLst[j] > SqLst[j+1]:
            SqLst[j], SqLst[j+1] = SqLst[j+1], SqLst[j]

print(SqLst)
[2, 4, 6, 10, 12, 14, 16, 18]

Answered By

2 Likes


Related Questions