2014-08-12 13:33:40 -04:00
|
|
|
# test the builtin reverse() function
|
2017-02-14 16:57:56 -05:00
|
|
|
try:
|
|
|
|
reversed
|
|
|
|
except:
|
|
|
|
print("SKIP")
|
2017-06-10 13:03:01 -04:00
|
|
|
raise SystemExit
|
2014-08-12 13:33:40 -04:00
|
|
|
|
|
|
|
# list
|
|
|
|
print(list(reversed([])))
|
|
|
|
print(list(reversed([1])))
|
|
|
|
print(list(reversed([1, 2, 3])))
|
|
|
|
|
|
|
|
# tuple
|
|
|
|
print(list(reversed(())))
|
|
|
|
print(list(reversed((1, 2, 3))))
|
|
|
|
|
|
|
|
# string
|
|
|
|
for c in reversed('ab'):
|
|
|
|
print(c)
|
|
|
|
|
|
|
|
# bytes
|
|
|
|
for b in reversed(b'1234'):
|
|
|
|
print(b)
|
|
|
|
|
|
|
|
# range
|
2014-08-12 14:41:18 -04:00
|
|
|
for i in reversed(range(3)):
|
|
|
|
print(i)
|
2014-08-12 13:33:40 -04:00
|
|
|
|
|
|
|
# user object
|
|
|
|
class A:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def __len__(self):
|
|
|
|
return 3
|
|
|
|
def __getitem__(self, pos):
|
|
|
|
return pos + 1
|
|
|
|
for a in reversed(A()):
|
|
|
|
print(a)
|
2015-01-20 19:19:42 -05:00
|
|
|
|
|
|
|
# user object with __reversed__
|
|
|
|
class B:
|
|
|
|
def __reversed__(self):
|
|
|
|
return [1, 2, 3]
|
|
|
|
print(reversed(B()))
|