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
Create a list SqLst that stores the doubles of elements of another list NumLst. Following code is trying to achieve this. Will this code work as desired ? What will be stored in SqLst after following code ?
NumLst = [2, 5, 1, 7, 3, 6, 8, 9] SqLst = NumLst * 2
Change the above code so that it works as stated in previous question.
Consider a list ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write code using a list comprehension that takes the list ML and makes a new list that has only the even elements of this list in it.
Write equivalent list comprehension for the following code :
target1 = [] for number in source: if number & 1: target1.append(number)