-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevlistinplace.py
More file actions
41 lines (31 loc) · 799 Bytes
/
revlistinplace.py
File metadata and controls
41 lines (31 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Reverse list in place.
You cannot do this with reversed(), .reverse(), or list slice
assignment!
>>> lst = []
>>> rev_list_in_place(lst)
>>> lst
[]
>>> lst = ['a']
>>> rev_list_in_place(lst)
>>> lst
['a']
>>> lst = [1, 2, 3]
>>> rev_list_in_place(lst)
>>> lst
[3, 2, 1]
>>> lst = [1, 2, 3, 4]
>>> rev_list_in_place(lst)
>>> lst
[4, 3, 2, 1]
"""
def rev_list_in_place(lst):
"""Reverse list in place.
You cannot do this with reversed(), .reverse(), or list slice
assignment!
"""
for i in range(len(lst)/2):
lst[i], lst[-(i + 1)] = lst[-(i + 1)], lst[i]
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASSED. YOU'RE THE BEST!\n"