Input list: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
I know how to do it with for loop, but my assignment is to do it with while loop; which I have no idea to do. Here is the code I have so far:
JavaScript
x
14
14
1
def while_version(items):
2
a = 0
3
4
b = len(items)
5
6
r_list = []
7
8
while (a!=b):
9
10
items[a:a] = r_list[(-a)-1]
11
12
a+=1
13
return items
14
Advertisement
Answer
I would say to make the while loop act like a for loop.
JavaScript
1
9
1
firstList = [1,2,3]
2
secondList=[]
3
4
counter = len(firstList)-1
5
6
while counter >= 0:
7
secondList.append(firstList[counter])
8
counter -= 1
9