about summary refs log tree commit diff
path: root/scratch
diff options
context:
space:
mode:
Diffstat (limited to 'scratch')
-rw-r--r--scratch/deepmind/part_two/reverse-words.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/scratch/deepmind/part_two/reverse-words.py b/scratch/deepmind/part_two/reverse-words.py
index d433ac594bcc..033d11244ca7 100644
--- a/scratch/deepmind/part_two/reverse-words.py
+++ b/scratch/deepmind/part_two/reverse-words.py
@@ -10,6 +10,9 @@ def reverse(xs, i, j):
 
 
 def reverse_words(xs):
+    punctuation = None
+    if len(xs) > 0 and xs[-1] in ".?!":
+        punctuation = xs.pop()
     reverse(xs, 0, len(xs) - 1)
     i = 0
     j = i
@@ -19,6 +22,8 @@ def reverse_words(xs):
         reverse(xs, i, j - 1)
         j += 1
         i = j
+    if punctuation:
+        xs.append(punctuation)
 
 
 # Tests
@@ -59,5 +64,11 @@ class Test(unittest.TestCase):
         expected = list('')
         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)