I made a reverse enumerate without the use of the reversed() function. I want the string and the numbers to be printed in reverse. However, when testing, I get an AssertionError stating that the lists do not match.
def my_own_enumerate(a):
for i in range(len(a)-1, -1, -1):
return i, a[i]
class Test(unittest.TestCase):
def test_my_own_enumerate(self):
self.assertEqual([(0, "m"), (1, "e"), (2, "!")], list(my_own_enumerate("me!")))
my_own_enumerate
is meant to return a reversed enumeration (as you stated yourself) so I'm not sure why you assert its output with a non-reversed enumeration.
The loop in my_own_enumerate
returns after the first iteration, so it will always return only the last character and its index. Instead, try
def my_own_enumerate(a): return [(i, a[i]) for i in range(len(a) - 1, -1, -1)]
Then
print(my_own_enumerate('me!'))
# [(2, '!'), (1, 'e'), (0, 'm')]
And
class Test(unittest.TestCase):
def test_my_own_enumerate(self):
self.assertEqual([(2, '!'), (1, 'e'), (0, 'm')], my_own_enumerate("me!"))
Does not fail.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.