In [2]:
def list_of_chars(chars):
if not chars:
return chars
length = len(chars)
for i in range(length/2):
chars[i], chars[length - (i+1)] = chars[length - (i+1)], chars[i]
return chars
def list_of_chars1(chars):
if not chars:
return chars
chars = list(chars)
return [chars[i] for i in range(len(chars)-1, -1, -1)]
def list_of_chars4(list_chars):
if not list_chars:
return list_chars
rchars = []
i = len(list_chars) - 1
while i > -1:
rchars.append(list_chars[i])
i -= 1
return rchars
def list_of_chars2(chars):
return chars[::-1]
def list_of_chars3(chars):
if not chars:
return chars
return list(reversed(chars))
In [3]:
a = list("soyle dim tam")
print str(list_of_chars(a))
In [95]:
from nose.tools import assert_equal
assert_equal(list_of_chars(None), None)
assert_equal(list_of_chars(['']), [''])
assert_equal(list_of_chars(
['f', 'o', 'o', ' ', 'b', 'a', 'r']),
['r', 'a', 'b', ' ', 'o', 'o', 'f'])
print('Success: test_reverse')
In [7]:
a = "ferferf"
In [8]:
print a
In [ ]: