Solve bonus part of reverse-words
InterviewCake asks "How would you handle punctuation?". Without precise specs about what that entails, I'm supporting sentences ending with punctuation.
This commit is contained in:
parent
acf1b8c4f0
commit
ca6bd29ed8
1 changed files with 11 additions and 0 deletions
|
@ -10,6 +10,9 @@ def reverse(xs, i, j):
|
||||||
|
|
||||||
|
|
||||||
def reverse_words(xs):
|
def reverse_words(xs):
|
||||||
|
punctuation = None
|
||||||
|
if len(xs) > 0 and xs[-1] in ".?!":
|
||||||
|
punctuation = xs.pop()
|
||||||
reverse(xs, 0, len(xs) - 1)
|
reverse(xs, 0, len(xs) - 1)
|
||||||
i = 0
|
i = 0
|
||||||
j = i
|
j = i
|
||||||
|
@ -19,6 +22,8 @@ def reverse_words(xs):
|
||||||
reverse(xs, i, j - 1)
|
reverse(xs, i, j - 1)
|
||||||
j += 1
|
j += 1
|
||||||
i = j
|
i = j
|
||||||
|
if punctuation:
|
||||||
|
xs.append(punctuation)
|
||||||
|
|
||||||
|
|
||||||
# Tests
|
# Tests
|
||||||
|
@ -59,5 +64,11 @@ class Test(unittest.TestCase):
|
||||||
expected = list('')
|
expected = list('')
|
||||||
self.assertEqual(message, expected)
|
self.assertEqual(message, expected)
|
||||||
|
|
||||||
|
def test_bonus_support_punctuation(self):
|
||||||
|
message = list('yummy is cake bundt chocolate this!')
|
||||||
|
reverse_words(message)
|
||||||
|
expected = list('this chocolate bundt cake is yummy!')
|
||||||
|
self.assertEqual(message, expected)
|
||||||
|
|
||||||
|
|
||||||
unittest.main(verbosity=2)
|
unittest.main(verbosity=2)
|
||||||
|
|
Loading…
Reference in a new issue